linux 定时任务- 清理文件

1. 编写命令

删除/opt/jenkins/test*/builds/ 目录下面 9天以前 名称为 [1-9]* 的目录:
find /opt/jenkins/test*/builds/ -type d -mtime +9 -name “[1-9]*” -exec rm -rf {} ;
-type d:表示目录
find命令:https://www.cnblogs.com/weijiangbao/p/7653588.html
匹配符:https://www.cnblogs.com/newcaoguo/p/5981005.html

2. 创建脚本

创建脚本:
touch /opt/bin/auto-del-9-days-ago-file.sh
编辑脚本:
vi /opt/bin/auto-del-9-days-ago-file.sh
脚本内容:

#!/bin/sh
find /opt/jenkins/test*/builds/ -type d -mtime +9 -name "[1-9]*" -exec rm -rf {} \;

3. 分配可执行权限

chmod +x /opt/bin/auto-del-9-days-ago-file.sh
给所有用户分配可执行权限(等同于:chmod a+x /opt/bin/auto-del-9-days-ago-file.sh)
级别: u( 用户) g(用户组) o(其他) a(所有)
chmod 命令详解 : https://www.cnblogs.com/Berryxiong/p/6193866.html

4. 定时任务

4.1 编辑crontab服务文件: crontab -e

内容:每天2点10分执行
*(分) *(时) *(天) *(月) *(星期)

10 2 * * * /opt/bin/auto-del-9-days-ago-file.sh

4.2 查看定时任务:crontab -l

猜你喜欢

转载自blog.csdn.net/besto229/article/details/90599371