Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2153777
  • 博文数量: 227
  • 博客积分: 10521
  • 博客等级: 上将
  • 技术积分: 3452
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-20 14:59
个人简介

低调做人,高调做事!

文章分类

全部博文(227)

文章存档

2013年(4)

2012年(8)

2011年(16)

2010年(24)

2009年(92)

2008年(83)

分类: LINUX

2008-07-23 16:26:16

#!/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) |
给主人留下些什么吧!~~