Periodically delete files per cronjob

Another entry in my virtual “braindump” category, so I don’t have to look it up somewhere else all the time.

If you want to periodically delete files in a specific directory the unix program find comes to the rescue. In my specific case I needed to delete all the movie files from a surveillance camera that where older than 30 days.

This is the crontab entry to accomplish that:
0 0 * * * /usr/bin/find /videos/ -type f -name '*.avi' -mtime +30 -exec rm {} \;
(everything in one line)

The code runs the program /usr/bin/find every day at midnight (0 0 * * *). It checks the directory called /videos for everything that is a file (-type f), has a name that ends in .avi (-name '*.avi') and is created 30 days ago or before that (-mtime +30).
The command then executes rm (remove) on all those files (-exec rm {} \;).


  1. /users/gcrmgr/core is the file to which i wanna delete every morning 6.00am. what will be the code for this. as i am new for unix. kindly help boss!!

  2. hi,
    what you have to do is set up a new cronjob to accomplish that.

    – type in “crontab -e -u root” to edit root’s cron entries.
    – paste in the following line: “0 0 * * * rm /users/gcrmgr/core”

    even easier than the process I described in my blog post 🙂

  3. You’re right! I was focusing more on the command that cron needs to run, than on the time setup.

    So the correct answer to bishram keshri’s question is:
    “0 6 * * * rm /users/gcrmgr/core”

    Thanks, Damu!

  4. I tried the command
    0 0 * * * /usr/bin/find /path_to_my_folder/ -type f -name ‘_Generic.jpg’ -mtime +1 -exec rm {} \;

    but I got an error:
    /usr/local/cpanel/bin/jailshell: /usr/local/bin/find: No such file or directory

    Where is my mistake ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.