This is a quick how-to to clear out older files in a directory.
find /path/to/files/ -type f -mtime +7 -exec rm -rf {} \;
Explanation of the arguments:
find: | the command that will search for the files |
/path/to/files/: | the top level directory to start searching |
-type f: | so we don’t remove directories, only files |
-mtime +7: | files older than ’7′ days. Change to ‘+14′ to delete files older than 2 weeks |
-exec: | what to do with the files we find |
rm -rf: | remove them recursively, force |
{}: | this represents each file we find |
\;: | the end of the exec |
Now you can schedule this by adding a crontab entry to run every night at 2am:
0 2 * * * /bin/find /path/to/files/ -type f -mtime +7 -exec rm -rf {} \;