Files
be.ems/tools/misc/rmexpiredfile.sh
2023-11-08 16:51:44 +08:00

49 lines
1.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
export LANG="zh_CN.UTF-8"
# ---------------------------------------------------------------------
# 定时清理反馈结果的日志文件夹 仅保留最近2周记录
# 文件夹格式yyyy-mm-dd
# Author : Madr
# Date : 2020年10月15日16:57:22
# ---------------------------------------------------------------------
#15天以前日期
dayAgo=$(date -d"15 day ago" +%Y-%m-%d)
#日志文件路径
logPath=/home/gsidc/app/nhis/log/
#记录操作日志
printFile=/home/gsidc/app/nhis/remove_nhis.log
echo '当前时间' `date +%Y-%m-%d` `date +%T` >> ${printFile}
echo '10天以前' ${dayAgo} >> ${printFile}
echo '开始删除,删除的目录列表如下' >> ${printFile}
dayAgoStr=${dayAgo}" 00:00:00"
dayAgoTime=`date -d "$dayAgoStr" +%s`
for dir in $(ls $logPath)
do
if [ ! $dir ];
then
echo 'dir为空,安全起见,本次任务终止!'>> ${printFile}
exit
else
#仅删除目录文件
if [ -d $logPath$dir ] ;
then
#因为文件夹名称为日期格式(yyyy-mm-dd), 转为时间戳进行比较
dirStr=${dir}" 00:00:00"
dirTime=`date -d "$dirStr" +%s`
if [ $dirTime -lt $dayAgoTime ];
then
echo ${logPath}"$dir" >> ${printFile}
#删除指定日期之前的目录
rm -rf ${logPath}"$dir"
fi
else
echo $logPath$dir '不是文件夹,不删除'>> ${printFile}
fi
fi
done
echo '删除完成!'>> ${printFile}