服务器自动删除文件的shell脚本


服务器自动删除文件的shell脚本
 
加入系统 计划程序中,自动清理垃圾文件,如不需要的日志文件。 
支持匹配路径 匹配文件名  多久没有访问的自动清理 
 
01
#用于各系统清理文件脚本,filepath  reg_filename fileatime
02
#author Foyon0806@gmail.com
03
#date   2013-8-22 14:51:52
04
#site www.jbxue.com
05
#!/bin/sh
06
if [ $# -eq 0 ];then
07
    echo "Usage: sh auto_clear_file.sh clear_filepath clear_regfilename filecreatetime"
08
    echo "eg: sh auto_clear_file.sh /tmp/log/  user_log -7day"
09
    exit
10
fi
11
filepath=$1
12
regfilename=$2
13
 
14
 
15
if [ "-$3" = "-" ];then
16
    filectime=`date -d -7day '+ %s'`
17
else
18
    filectime=`date -d $3 '+ %s'`
19
fi
20
log=`ls $filepath | grep $regfilename`
21
echo $log
22
for file in ${log}
23
do
24
    echo $file
25
    fileatime=`stat -c %X ${filepath}${file}`
26
    if [ ${fileatime} -lt ${filectime} ]; then
27
        opt=`rm -f ${filepath}${file}`
28
        echo $opt
29
    fi 
30
done
 

相关内容

    暂无相关文章