Two ways to delete files based on age in Linux

I have found that there are two different ways to clean up a folder, by removing older files. One methods removes the files or folders based on their age, regardless of how many there are. I use this for example to delete backup folders, beyond their age.

Remove using tail

This method assumes that there is one file for every day, for example for backups. The command below removes all files order than 30 days. It is important there is only file per day, because else the tail results would not be correct. The advantage here is that if the backup fails, the older backup files will not be removed because of their age.

ls -1c ./ | tail -n +30 | xargs rm -rf

Remove using find

This command selects all files with a modification time older than 2 days and removes them.

0 15 * * * find ~/files/* -mtime +2 -exec rm {} \;