Unix find remove python style
wordpress meta
title: 'Unix Find and Remove Python Style'
date: '2012-11-26T21:07:06-06:00'
status: publish
permalink: /unix-find-remove-python-style
author: admin
excerpt: ''
type: post
id: 130
category:
- Bash
- Python
tag: []
post_format: []
title: 'Unix Find and Remove Python Style'
date: '2012-11-26T21:07:06-06:00'
status: publish
permalink: /unix-find-remove-python-style
author: admin
excerpt: ''
type: post
id: 130
category:
- Bash
- Python
tag: []
post_format: []
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:
try:
print "modified: %s" % time.ctime(timestamp),
removeDir=os.path.join(r,dir)
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
pass
else:
print " -> success"