Cron job to delete files older than x days?
I'm currently using this cron job to delete stalled temp files in a particular location. How can I improve this to only delete files older than x days (e.g., 30 days)?
0 4 * * 0 /bin/rm -fv /home/*/tmp/Cpanel_*
1 Answer
You can use find:
find /home/*/tmp/Cpanel_*' -type f \! -newermt "month ago" -deletefind can be tricky, so do your homework reading about it and test well. The common practice is to test with a command that doesn't act on found targets, like ls or echo. find has some builtin commands you can use for testing. So you can start with this:
find /home/*/tmp/Cpanel_*' -type f \! -newermt "month ago" -ls 8