使用Shell脚本删除/清空日志文件

时间:2021-07-14 18:25:30

话不多少,直接上代码:

#!/bin/bash
workdir=("/home/Tax_Punish_Ret/log_txt") #可填写多个路径, 用空格隔开
# 查找日志文件
for wdir in ${workdir[@]}
do
echo filepath is $wdir
if [ $wdir = ${workdir[0]} ] ;then
fileStr=`find $wdir/*.log`
echo files is $fileStr
else
fileStr=`find $wdir -type d`
echo filee is $fileStr
fi
# 删除/清空日志文件
for dir in $fileStr
do
echo file name is $dir
# 清空不删除日志文件
echo ''>$dir
# 删除日志文件
#find $dir -name '*log*' -and -mtime +7 -type f | xargs rm
if [ $? -eq 0 ];then
echo $date delete $dir success!
else
echo $date delete $dir FAILD!
fi
done
done

crontab 执行定时脚本:

* * * * * /bin/bash /home/ubuntu/deletelog.sh >> /home/ubuntu/deletelog.txt 2>&1&