Chinaunix首页 | 论坛 | 博客
  • 博客访问: 438035
  • 博文数量: 96
  • 博客积分: 4594
  • 博客等级: 上校
  • 技术积分: 1130
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-30 19:56
文章分类
文章存档

2012年(8)

2011年(49)

2010年(18)

2009年(21)

分类: LINUX

2011-03-31 08:01:57

#!/bin/bash
DumpFile=/dump-file/
File=`echo $line|awk '{print $2}'`
aa=$(echo $1|grep "^-")
DumpLog="`whoami`-`date +%m-%d-%H-%M`.rm-dump"

###

function TestDir(){

Test_Dir=$(echo $line|grep "/") #test if is a directory.
Test_Dir2=$(echo $line |awk '{print $2}'|grep "^/") # Test if the prameter starts with "/".
Test_Dir3=$(echo $line |awk '{print $2}'|grep "^/tmp") #
Dir2=$(echo $line |awk -F/ '{print $2}') #Extract the prameter after the first "/".
Dir=$(echo $line |awk '{print $2}'|awk -F/ '{print $1}') ##Extract the prameter before the first "/".
D1=`date +%F_%H:%M:%S.bak`
Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`
DumpLog="`whoami`-`date +%m-%d-%H-%M`.rm-dump"

        if [[ $Test_Dir != "" ]] ;then #If parameters include directory
                if [[ $Test_Dir2 != "" ]] ;then #If parameter starts with "/",as formate /a/b/c/
                   if [[ $Test_Dir3 != "" ]];then # this test is for to repaire less command's error.
                        rm.bak -rf `echo $line|awk '{print $2}'`
                   else
                        #echo $line "start with /"
                        mkdir $DumpFile$Dir2"_"$D1 2>/tmp/$DumpLog # Create directory as format /dump-file/(first directory after "/")
                        
                        mv `echo $line|awk '{print $2}'` $DumpFile$Dir2"_"$D1
                        echo $DumpFile$Dir2"_"$D1
                   fi
                else #Means that parameter starts without "/" ,as format a/b/c
                        mkdir $DumpFile$Dir$D1 #Create directory as format /dump-file/(first directory before "/")
                        mv `echo $line|awk '{print $2}'` $DumpFile$Dir$D1
                        echo $line "starts before /"
                fi
        else
                Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`
                mv `echo $line|awk '{print $2}'` $DumpFile$Date
        fi

}
###
###
if [[ "$1" != "" ]] ;then #If the frist parameter is not empty.

        if [[ $aa != "$1" ]];then #If the first parameter not start with "-".
                du -s "$@" 2>/tmp/$DumpLog |while read line
                do
                        size=`echo $line | awk '$1 >2000000{print $1}'`
                        if [ "$size" > "1900000" ];then
                                #echo "Lager than 2Mb"
                                rm.bak -rf `echo $line|awk '{print $2}'`
                        else
                                TestDir
                        fi
                done

        else
                if [[ "$2" != ""  ]]; then
                       until [ "$2" == ""  ]
                       do
                                 #echo "function is running"
                                 du -s $2 2> /tmp/$DumpLog |while read line
                                 do
                                        size=`echo $line | awk '$1 >2000000{print $1}'`
                                        if [ "$size" > "1900000" ];then
                                                #echo "Lager than 2Mb"
                                                rm.bak -rf $2
                                        else
                                                TestDir

                                        fi
                                        shift
                                 done
                                 shift
                        done
                else
                        echo -e "No file detected.\nTry 'rm --help' for more information. "
                fi
        fi
else
        echo -e "rm: missing operand \nTry 'rm --help' for more information."
fi
阅读(1250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~