Is there a simple way to locate directories which only contains files older than a certain date and then remove the directories?
I can find old files easily enough, but if a directory has more than one file in it and one of the files is new, then I need to not touch the entire directory. It's only where all the files are old that I want it removed.
On Wed, Dec 22, 2010 at 11:18:40AM +0000, Mark Rogers wrote:
Is there a simple way to locate directories which only contains files older than a certain date and then remove the directories?
I can find old files easily enough, but if a directory has more than one file in it and one of the files is new, then I need to not touch the entire directory. It's only where all the files are old that I want it removed.
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.
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.
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.
On 22/12/10 14:54, Chris G wrote:
Well here's a starter, completely untested, I just used my standard skeleton and added obvious bits.
Thanks for that, I'm going to have a play and see if next year isn't my year of Python!
Off the top of my head I can't remember and I'm at a winpoo computer right now, but this can be down with find;
find /a/dir* -mtime +7 -exec rm {} ;
This will delete all files in a directory that are 7 days old, obviously 'man find' and 'man rm' are going to help. Since you want to delete the containing folder also perhaps nest this in a loop, with an outter loop deleting the folder if its empty because the inner loop cleared it out because the files inside met your age criteria...Dangerous, but I like it! :D