Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 551
Harry Avatar asked Mar 17 '14 18:03

Harry


People also ask

How do I clean up log files?

del *.log /a /s /q /f Step 1: Also run Command Prompt as administrator. Step 2: Type wevtutil el and press Enter to list all the logs. Step 3: TYpe wevtutil cl + the name of the log you want to delete and press Enter to remove the log file.

How do you clear a log file in Linux?

The safest method to empty a log file in Linux is by using the truncate command. Truncate command is used to shrink or extend the size of each FILE to the specified size. Where -s is used to set or adjust the file size by SIZE bytes.

Do cron jobs have logs?

Most versions of cron will log when jobs run and whether there are any errors with your crontab. They do not log cron job output or exit statuses.


1 Answers

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 .

like image 90
Up_One Avatar answered Sep 28 '22 20:09

Up_One