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 {} \;).
/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!!
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 🙂
are you sure the one in ur comment runs everyday at 6am? isn’t it supposed to be 0 6… ?
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!
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 ?
It seems your “find” program is in a different path.
Try
which find
to see the exact path.I am trying this in Cpanel, how to try “which find” ?
You need to type this into a terminal window. I suggest connecting to your server via SSH.
Thank you mate, this solve my need for periodically delete cache files of Opencart.