user965884 user965884 - 1 year ago 120
Linux Question

Remove log files using cron job

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 Source

Use wildcard . And just put it in your crontab use the corntab -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 .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download