shell定期清除文件

#!/bin/bash
# 删除tmp目录下7天未访问的文件
upload_tmp='/root/tmp'
expire_day=7

function ergodic() {
    for file in `ls $1` 
    do  
        if [ -d $1"/"$file ]
        then
            ergodic $1"/"$file  
        else
            newFile=$1"/"$file
            accessTime=`stat -c %X ${newFile}`
            curTime=`date +%s`
            diff=`expr ${curTime} - ${accessTime}`
            diffDay=`awk -v num=${diff} 'BEGIN{printf "%.2f\n",(num/60/60/24)}'`
            # 除以小数
            ret=`awk -v num1=${diffDay} -v num2=${expire_day} 'BEGIN{print(num1>num2)?"0":"1"}'`
            # 小数比较
            if [ $ret -eq 0 ] 
            then
                # echo ${newFile}
                \rm -rf ${newFile}
            fi  
        fi  
    done  
}

ergodic ${upload_tmp}

标签: none