由于测试环境流水线产出的测试包太多,很占内存,考虑定期清理。
首先是找到指定目录的文件,以日期为分割线,删除五天前的文件,再配置定时执行脚本即可。
find 路径 -mtime +天数 -type f -name "文件名" -exec rm -rf {} ;
find /tmp -mtime +30 -type f -name "*" -exec rm -rf {} \;
/tmp --设置查找的目录;
-mtime +30 --设置修改时间为30天前;
-type f --设置查找的类型为文件;其中f为文件,
d则为文件夹
-name "*" --设置文件名称,可以使用通配符;
-exec rm -rf --查找完毕后执行删除操作;
{} \
[root@localhost ~]# touch auto_delete_pkg.sh
[root@localhost ~]# vi auto_delete_acheck_pkg.sh
#/bin/bash
find /tmp;
find /tmp -mtime +5 -type f -name "*" -exec rm -rf {} \;
[root@localhost ~]# chmod 777 auto_delete_pkg.sh
[root@localhost ~]# ll
total 2
drwxr-xr-x. 2 root root 6 Aug 7 16:11 11
-rwxrwxrwx. 1 root root 99 Aug 7 16:24 auto_delete_pkg.sh
使用crontab定时执行脚本
[root@localhost ~]# crontab -e
crontab: installing new crontab
0 0 * * * auto_delete_pkg.sh > /data/log/auto_delete.log 2>&1
将要删除的文件重定向到/data/log/auto_delete.log
每天零点自动执行 其中:
第一个*号表示时间中的 分钟 取值范围:0-59
第二个*号表示时间中的 小时 取值范围:0-23
第三个*号表示一个月中的第几天,取值范围:1-31
第四个*号表示一年中的第几个月,取值范围:1-12
第五个*号表示一个星期中的第几天,以星期天开始依次的取值为0~7,0、7都表示星期天