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

加入系统 计划程序中,自动清理垃圾文件,如不需要的日志文件。
支持匹配路径 匹配文件名  多久没有访问的自动清理

#用于各系统清理文件脚本,filepath  reg_filename fileatime 
#author Foyon0806@gmail.com 
#date   2013-8-22 14:51:52
#site www.jbxue.com
#!/bin/sh
if [ $# -eq 0 ];then
    echo "Usage: sh auto_clear_file.sh clear_filepath clear_regfilename filecreatetime"
    echo "eg: sh auto_clear_file.sh /tmp/log/  user_log -7day"
    exit
fi
filepath=$1
regfilename=$2


if [ "-$3" = "-" ];then
    filectime=`date -d -7day '+ %s'`
else
    filectime=`date -d $3 '+ %s'`
fi
log=`ls $filepath | grep $regfilename`
echo $log
for file in ${log}
do
    echo $file
    fileatime=`stat -c %X ${filepath}${file}`
    if [ ${fileatime} -lt ${filectime} ]; then
        opt=`rm -f ${filepath}${file}`
        echo $opt
    fi  
done

转载于:https://my.oschina.net/softsky/blog/156260

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值