#!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
Auto delete
最新推荐文章于 2024-09-07 09:18:36 发布