How to delete files older than X days on linux

Posted by Magnetic_dud Monday, January 16, 2012 8:50:00 AM Categories: Linux

Sometimes can happen that you want to keep only the files of the last X days in a directory.

On Linux you can do that with a single command line:

find /path/where-are-the/files* -mtime +7 -exec rm {} \;

How it works:

-mtime indicates the days to keep. In the example, having written +7, we only keep files from the last week.

-exec indicates the command to execute in the event that a match is found (ie: the files older than a week), and in this case we pass rm, which eliminates them