Monday, July 22, 2013

Delete files older than x days/minutes in Linux

Sometimes we need to delete older than x-days. In Linux we can use following command

find /path/ -mtime +5 -exec rm {} \;

The above command will delete all file older than 5 days.

If we need to remove all files older than 10 minutes, apply the following command

find /path/ -mmin +10 -exec rm {} \;

The above command will delete all files older than 5 minutes.

If you don't want to delete files but see the list, use ls instead of rm.

find /path/ -mtime +5 -exec ls {} \;

The above command will list all files older than 5 days.

Note that there are spaces between rm, {}, and \;