Remove log files using cron job

Harry picture Harry · Mar 17, 2014 · Viewed 52.4k times · Source

Hi. I want to remove all log files from the last 7 days from a folder, but leave all the other files. Can I use the below command? How do you specify that it just delete the files with .log extension?

 find  /path/to/file -mtime +7 -exec rm -f {} \; 

Do I need to write this command into some file, or can I just write it in command prompt and have it run automatically every day?

I have no idea how to run a cron job in linux.

Answer

Up_One picture Up_One · Mar 17, 2014

Use wildcard. And just put it in your crontab use the crontab -e option to edit your crontab jobs.
See example:

* * * * *  find  /path/to/*.log -mtime +7 -exec rm -f {} \; 

Just to increment the answer check this nice article on how to work with your crontab ! in Linux .