Saturday, March 10th, 2012, 12:23 am
Linux/Unix: Deleting Old Files in a Cron Job
EVERAL years ago I wrote about an old backup procedure of mine. Sometimes people set up a job to make a backup, but what about removing backups that are too old to matter? If a directory/file needs deleting based on age, with wildcards one could run something like:
rm ~/some_file-`date -d "7 day ago" +%d%m%Y`* rm ~/some_file-`date -d "6 day ago" +%d%m%Y`*
Or quick and dirty (risky if there’s a mixture of files in the said location):
find . -atime +7 -exec rm {} \;;
There’s nothing complicated to it. Once it’s done once, it can be modified thereafter.