Bash: Delete all files in a directory, older than n days

This simple one-liner bash command finds and deletes files in a directory older than 3 days:

find /root/backups -type f -mtime +3 -delete

It’s very useful for scheduled backups. You can set a cron job to do just this to prevent your old backups from eating up valuable space. My crontab looks like this:

#Delete backups older than 5 days
0 23 * * * find /root/backups -type f -mtime +5 -delete

Which means: At 23:00,  on all days of the month, on all months, on all days of the week, execute find on all files in the folder /root/backups, older than 5 days, and then delete the found files.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.