Auto delete

#!bin/sh
# Dependents: sh, ls, date, awk, expr
# Pass test on ubuntu 10.04
#############################################
# auto-delete file path
file_path=~/doc
# interval of delete time. default 1 day
del_interval_hours=24
# time limit. default 3 days
limit_hours=72
# interval of check time. default 10 min
sleep_seconds=600
#############################################


limit_seconds=`expr ${limit_hours} \* 3600`

GetCreateSecondsDiff()
{
	date_string_date=`ls -l --time=ctime $1 | awk '{print $6}'`
	date_string_time=`ls -l --time=ctime $1 | awk '{print $7}'`
	create_seconds=`date -d "$date_string_date $date_string_time" +%s`
	now_seconds=`date "+%s"`
	diff_seconds=`expr ${now_seconds} - ${create_seconds}`
	echo ${diff_seconds}
}

GetModifySecondsDiff()
{
	date_string_date=`ls -l $1 | awk '{print $6}'`
	date_string_time=`ls -l $1 | awk '{print $7}'`
	modify_seconds=`date -d "$date_string_date $date_string_time" +%s`
	now_seconds=`date "+%s"`
	diff_seconds=`expr ${now_seconds} - ${modify_seconds}`
	echo ${diff_seconds}
}

DoDeleteFile()
{
	echo "delete $1"
	rm -f $1
}

CheckAndAutoDelete()
{
	for FILE in `find ${file_path} -name "*"`; do
		if [ -f ${FILE} ]; then
			modify_diff=`GetModifySecondsDiff ${FILE}`
			if [ ${modify_diff} -gt ${limit_seconds} ]; then
				DoDeleteFile ${FILE}
			fi
		fi
	done
}

next_del_time=`date -d "+${del_interval_hours}hour" "+%Y-%m-%d %H:%M"`
next_del_time_seconds=`date -d "${next_del_time}" +%s`
while true; do
	now_seconds=`date +%s`
	if [ ${now_seconds} -lt ${next_del_time_seconds} ]; then
		CheckAndAutoDelete
		next_del_time=`date -d "+${del_interval_hours}hour" "+%Y-%m-%d %H:%M"`
		next_del_time_seconds=`date -d "${next_del_time}" +%s`
	fi
	sleep ${sleep_seconds}
done


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值