linux使用shellscripts刪除檔案

Linux使用ShellScripts刪除檔案

先建立Shell Scripts檔案

1
$ touch clearLog.sh

對.sh檔案編輯

1
$ vim clearLog.sh

按INSERT來新增內容

1
2
3
4
5
6
7
8
9

for i in {10..100};
do
#It's for log name with the format like "2019-01-01" & "20190101".
check_date=`date -d "-$i day" "+%Y-%m-%d"`
check_date1=`date -d "-$i day" "+%Y%m%d"`
find /usr/local/uat_tomcat/logs/ -name "*$check_date*" -exec rm -f {} ;
find /usr/local/uat_tomcat/logs/ -name "*$check_date1*" -exec rm -f {} ;
done

存檔指令

1
:wq

用crontab設定排程

1
2
3
4
5
編輯
$ crontab -e
新增內容
#每天05:00A.M.執行
0 5 * * * sh /root/clearLog.sh