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.
Joel G Mathew, known in tech circles by the pseudonym Droidzone, is an opensource and programming enthusiast.
His favorite pastime is grappling with GNU compilers, discovering newer Linux secrets, writing scripts, hacking roms, and programs (nothing illegal), reading, blogging. and testing out the latest gadgets.
When away from the tech world, Dr Joel G. Mathew is a practising ENT Surgeon, busy with surgeries and clinical practise.