计划任务实现自动清理日志的脚本
原文链接:
blog.csdn.net/weixin_42343424/article/details/80510004
实例命令 :
find /export/Logs/ -type f -name “log” -mtime +3 -exec rm -rf {} ;
将/export/Logs/目录下所有30天前带".log"的文件删除。
具体参数说明如下:
find:linux的查找命令,用户查找指定条件的文件;
/export/Logs/:想要进行清理的任意目录;
-type f代表文件 d代表目录
-mtime:标准语句写法;+30:查找30天前的文件,这里用数字代表天数;
“log”:希望查找的数据类型
-exec:固定写法;
rm -rf:强制删除文件,包括目录;
{} ; :固定写法,一对大括号+空格++;
-size 查找符合大小的文件;
(1)创建shell脚本 vi auto-del-log.sh
#!/bin/bash
find /export/Logs/ -type f -name “log” -mtime +3 -exec rm -rf {} ;
find /export/Logs/ -type f -name “log” -size +100M -exec rm -rf {} ;
(2)添加可执行权限 chmod +x auto-del-log.sh
(3)设置定时任务
crontab -e
0 1 * * * auto-del-log.sh 每天凌晨一点执行auto-del-log.sh脚本