Auto-Delete Log Files with Shell Scripts

You can create a simple Shell script to schedule the deletion of log files. Here is an example script:

#!/bin/bash

# 设置日志文件路径
LOG_DIR="/path/to/log/dir"

# 删除30天之前的日志文件
find $LOG_DIR -type f -name "*.log" -mtime +30 -exec rm {} \;

echo "日志文件清理完成"

In this script, you need to replace /path/to/log/dir with the actual path to your log files. The script uses the find command to locate all .log files that are 30 days old or older within the specified path, and then uses the rm command to delete those files.

You can save this script as a .sh file and then use tools like cron to regularly run the script in order to achieve the function of deleting log files on a schedule.

bannerAds