#!/bin/bash
# Program:
# This program is used to delete photo for outlink servers.
# History:
# 2007/08/23 chentao 1.1
#---------------------------------------------------------------
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
photo_lists=/usr/local/crontab/file_list
photo_path=/var/photo
photo_directory=$(find $photo_path -maxdepth 1 -type d | sed '1d' | tee /usr/local/crontab/total_directory)
today=$(date +%Y-%m-%d)
interval_time=7
echo "------------------------------------------------------------>" >> /var/log/del_log
echo "del start $(date +[%Y/%m/%d/%H:%M:%S])" >> /var/log/del_log
for directory in $photo_directory
do
echo $directory
find $directory -type f > $photo_lists
for file in $(cat $photo_lists)
do
last_access_time=$(stat $file | tail -3 | grep Access | awk '{print $2}')
today_s=$(date --date="$today" +%s)
old_s=$(date --date="$last_access_time" +%s)
interval_total_s=$(($today_s-$old_s))
interval_day=$(($interval_total_s/60/60/24))
echo "$interval_day"
if [ "$interval_day" -gt $interval_time ]; then
echo "delete file $file"
rm -rf $file
fi
done
done
echo "del end $(date +[%Y/%m/%d/%H:%M:%S])" >> /var/log/del_log
最近2天又研究一下这段代码的效率,在一个偶然的时间发现了find命令的强大功能,直接可以找出7天前的文件,所以将这个脚本优化一下,代码如下:
#!/bin/bash
# Program:
# This program is used to delete photo for outlink servers.
# History:
# 2007/08/23 chentao 1.1
#---------------------------------------------------------------
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
photo_path=/opt/photo
photo_directory=$(find $photo_path -maxdepth 1 -type d | sed '1d' | tee /usr/loc
al/crontab/total_directory)
echo "------------------------------------------------------------>" >> /var/log
/del_log
echo "del start $(date +[%Y/%m/%d/%H:%M:%S])" >> /var/log/del_log
for directory in $photo_directory
do
echo $directory
find $directory -atime +15 -exec rm -rf {} \;
done
echo "del end $(date +[%Y/%m/%d/%H:%M:%S])" >> /var/log/del_log
2个脚本比较一下,果然优化后的脚本效率高很多!
阅读(2072) | 评论(0) | 转发(1) |