Our web system at work is a little bit of a mess. We have a test server, but we don’t actually use it; we have Subversion, but, well, I don’t use it. There’s no enforcing mechanism to keep the SVN files and FTP in-sync and clean, so there are files on the site that aren’t in SVN. Unfortunately though, I need to use it; it keeps our changes in sync so that we don’t write over each other. The test server can be set up later.
Anyway, somehow some of the .svn folders got uploaded to FTP. There was a good 30 or so of them, scattered in subdirectories. And we’re talking a huge website; 6+ GB of content, I don’t even know how many folders, some of them run 5 or 6 deep. I needed to find all of the .svn folders and delete them.
I wrote a Python script to do it. I’m quite opposed to using Python as anything serious (I wouldn’t write an application in it) but I love how quick it is to whip up a little tool.
I think the following code is self explanatory, at least for someone who knows Python. You may need to obtain ftplib; I can’t remember. Of course, this code can work for any directory name that needs to be recursively found and deleted; just replace ‘.svn’ on the sixth line, and optionally change the output on the seventh; and, regardless of the directory name, you need to change the FTP server location and user/pass at the bottom of the script to point to your own FTP server.
Enjoy! (click ‘Read the rest of this entry’ to see the source code)
ftpRecursivelyDeleteAll.py:
from ftplib import FTP
def recurse(ftp):
print ftp.pwd()
for d in ftp.nlst():
if d == '.svn':
print ' svn!!!'
try:
ftp.cwd(d)
cleanOut(ftp)
ftp.cwd('..')
ftp.rmd(d)
except:
pass
else:
try:
ftp.cwd(d)
recurse(ftp)
ftp.cwd('..')
except:
pass # not a directory; ignore
def cleanOut(ftp):
print 'cleanout ',ftp.pwd()
for d in ftp.nlst():
try:
ftp.delete(d) # delete the file
except:
ftp.cwd(d) # it's actually a directory; clean it
cleanOut(ftp)
ftp.cwd('..')
ftp.rmd(d)
ftp = FTP('123.123.123.123')
ftp.login('USERNAME','PASSWORD')
recurse(ftp)
ftp.quit()