How can we schedule the deletion of logs older than seven days in Linux?

One option to achieve automatic deletion of log files that are seven days old is by using the default Linux tool called crontab. The specific steps are as follows:

  1. Open the terminal and enter the following command to edit the crontab file:
crontab -e
  1. To remove log files ending in .log that are seven days old from the /var/log directory, add a scheduled task to the crontab file with the following line:
0 0 * * * find /var/log -name "*.log" -mtime +7 -exec rm {} \;

This command specifies to execute a deletion command at midnight (0:00) every day. The find command searches for all log files ending in .log in the /var/log directory that are seven days old, and then executes the rm command to delete them.

  1. Save and exit the editing, crontab will automatically take effect.

This will allow you to automatically delete log files older than seven days. If the file path or deletion rules change, you can modify the command in crontab.

广告
Closing in 10 seconds
bannerAds