- #!/bin/bash
-
- # 指定目录
- directory="/u01/isi/collect_task_input/serverSplitBack"
-
- # 当前日期
- current_date=$(date +%Y-%m-%d)
-
- # 计算7天前的日期
- seven_days_ago=$(date -d "$current_date -7 days" +%Y-%m-%d)
-
- # 遍历目录中的文件,按文件名过滤并删除7天前的文件
- for file in "$directory"/*; do
- file_name=$(basename "$file")
- file_date=${file_name#*_task}
- file_date=${file_date%.dat}
- if [[ "$file_date" < "$seven_days_ago" ]]; then
- rm -f "$file"
- echo "Deleted file: $file"
- fi
- done

- 通过设置定时任务(比如使用 crontab)在每天定时执行该脚本。下面是一个示例的 crontab 任务设置,将脚本设置为每天凌晨1点执行:
-
- 0 1 * * * /bin/bash /path/to/delete_old_files.sh