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