Delete Files Over A Certain Age
We can use the find command to carry out actions on a selection of files. For example:
find /path/to/files* -mtime +5 -exec rm {} \;
How it works
The first argument to the find command, /path/to/files/*, specifies the files to act upon, and may be a directory or single file, or a shell glob, possibly matching multiple files, as in the example above.
The second argument -mtime, is used to specify the age in days of the files to search for. If you enter +5, it will find files older than 5 days.
The third argument, -exec, allows you to run a command such as rm, and the {} will be replaced by the actual filename.
The \; at the end is required to end the command.
A safer way to run this command would be:
mkdir /tmp/myoldfiles
find /path/to/files* -mtime +5 -exec mv {} /tmp/myoldfiles \;
This will move the files concerned to the temporary directory. If a mistake has been made, the files can easily be moved back, otherwise the temporary directory may be deleted in the usual way.


