Riaan's SysAdmin Blog

My tips, howtos, gotchas, snippets and stuff. Use at your own risk!


Unix Find and Remove Python Style

No doubt the Unix find and remove command comes in very useful when cleaning up large folders.  However find can quickly bump into the old "/usr/bin/find: Argument list too long" problem.

For reference here is a command that works well.  Except of course when too many files or directories involved.

find /usr/local/forms/*/output -name "*.html" -mtime +4 -exec rm {} \;

There is of course other ways to get this done with find, but I like python so I resorted to python as the example show below.

Here is an example that worked for me:

#!/usr/bin/env python
##  Adjust the humandays variable.  Set to 2000 days until we feel more comfortable.

import os
import glob
import time
import shutil

humandays = 2000
computerdays = 86400*humandays
now = time.time()
inputDirs = glob.glob('/usr/local/forms/*/input')

print "Script running on %s " % time.ctime(now)
print "using physical path /usr/local/forms/*/input and only removing directories older than %s days." % (humandays)

for inputDir in inputDirs:
  for r,d,f in os.walk(inputDir):
    for dir in d:
         timestamp = os.path.getmtime(os.path.join(r,dir))
         if now-computerdays > timestamp:
		  print "modified: %s" % time.ctime(timestamp),
                  print "  remove ",removeDir,
                  ## Better be 100% sure before you uncomment the rmtree line!
                  ## shutil.rmtree(removeDir)
                  ## Better be 100% sure before you uncomment the rmtree line!
             except Exception,e:
                  print e
                  print " -> success"


Bio Info for Riaan