|
Retain recent files
eg. an application produces a backup file every night. You only want to keep the last 5 files.
Pipe the output of the following script (retain_recent.py) into a sh, eg in a cronjob:
/YOURPATH/retain_recent.py | bash
Python code that produces 'rm fileX' statements:
#list the files according to a pattern, except for the N most recent files
import os,sys,time
fnList=[]
d='/../confluence_data/backups/'
for f in os.listdir( d ):
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(d+"/"+f)
s='%s|%s/%s|%s' % ( mtime, d, f, time.ctime(mtime))
#print s
fnList.append(s)
# retain the 5 most recent files:
nd=len(fnList)-5
c=0
for s in sorted(fnList):
(mt,fn,hd)=s.split('|')
c+=1
if (c>=nd):
print '#keeping %s (%s)' % ( fn, hd )
else:
print 'rm -v %s #deleting (%s)' % ( fn, hd )
| |