On Wed, Dec 22, 2010 at 01:25:19PM +0000, Mark Rogers wrote:
On 22/12/10 12:58, Chris G wrote:
I think that's just about the level of 'difficultness' where I move from a bash script to python. IMHO Python lends itself very well to jobs like this. Python has a ready made function that 'walks' a directory tree like find does so all you need is a bit of logic to check if all the files in a directory are older than x and, if so, delete the directory
Hmmm, learning Python is on the to-do list but the way things are at the moment I don't see it offering me a solution to this unless I can find some sample code that's pretty close - any suggestions? Might be a good way for me to get started down the Python route.
Well here's a starter, completely untested, I just used my standard skeleton and added obvious bits.
#!/usr/bin/python # # # # import os import stat import sys
old_time = 123456 # seconds since 1970 sort of time
for nm in sys.argv[1:]: # for each command line argument for srcPath, srcDirs, srcFiles in os.walk(nm): deldir = True for f in srcFiles: if os.stat(os.path.join(srcPath, f))[stat.ST_MTIME] > old_time: deldir = False break if deldir: for f in srcFiles: os.remove(os.path.join(srcPath, f)) os.rmdir(srcPath)
Well, I tested it a little. If old_time is small it doesn't remove a directory, if it's big it does! :-)
You may need to play with the os.walk() call to get it to walk up/down the directory tree in the way that you want so that things get deleted as expected. **Test it thoroughly** before you trust it!
Python has lots of simple date/time manipulation functions so you can get it to let you enter a human readable date easily enough.