#! /bin/bash d=`date +%H:%M:%S` echo "$d" lastMonth=`date -d "-1 month" +%Y-%m` echo $lastMonth #當變量a爲null或爲空字符串時則var=b #var=${a:-b} dir=/var/log/app/ #日誌中必包含的信息。[${lastMonth}-.. ..:..:..\] 對應內容 如 [2020-01-10 10:00:15] for file in `grep -r -E "\[${lastMonth}-.. ..:..:..\]" ${1:-dir}/*|awk -F ":" '{print $1}'`; do if [ -f $file ];then echo $file rm $file #for file in "$dir/$file" fi done
1.sh 該shell腳本.sh 可傳清除目錄參數(不傳dir=/var/log/app/)shell
2.可設置每個月1日0點運行定時清理 crontab 0 0 1 * * sh 該shell腳本.sh 可傳清除目錄參數(不傳dir=/var/log/app/)bash