So I have a cron setup to make backups of a folder into a tarball every hour. I would like to add into the shell script that I'm using the ability to have files deleted automagically after about three days, so that I don't have a crap ton of files.

How can I go about this? Thanks.

link|improve this question

feedback

2 Answers

up vote 7 down vote accepted

Add this line to the script (modify accordingly):

find /path/to/backup_folder -mtime +3 -exec rm {} \;

This assumes your backup tarballs and only your backup tarballs reside in that folder. You could also use the tmpwatch utility:

tmpwatch -mf /path/to/backup_folder 72
link|improve this answer
So if I just add this in (changing the folder path), it'll delete anything older than three days? – Chiggins Jan 21 '11 at 17:15
Yes, but keep in mind it is recursive. If you have any folders beneath the backup folder, add the -maxdepth 1 switch. – John T Jan 21 '11 at 17:17
If you are at all unsure, add "echo " before the rm and verify the output first. – Chris Nava Jan 21 '11 at 17:20
@Chris good suggestion! – John T Jan 21 '11 at 17:25
feedback

From my crontab on my mac:

0 13 * * * /usr/bin/find /Users/dharris/.Trash -atime +14 -mindepth 1 -maxdepth 1 -print0 | xargs -0 ls -ltd

Using -atime rather than -mtime means that if I access the file, it won't be deleted.

My version here uses +14 for two weeks delay, change to +3 for your needs.

link|improve this answer
+1 for atime use... I always do this too. If I looked at the file, it may be interesting enough to keep this around a little longer. – Rich Homolka Jan 21 '11 at 17:33
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.