#! /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