string

string
posts - 27, comments - 177, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

安全的rm命令

Posted on 2010-05-13 00:17 djx_zh 阅读(591) 评论(0)  编辑 收藏 引用
#!/bin/bash
#Any problem, please contact with djx.zhenghua@gmail.com
#To check out the latest version, please go to https://codelibrarydzh.googlecode.com/svn/trunk
args=$@
ioption=0
roption=0
foption=0
optargs=""
trashfolder=${TRASHFOLDER:-/tmp/$USER-trash/}
mvlogf=$trashfolder/.log
RM=/bin/rm
########################### define function start ###########################
rmhelp(){
    echo Usage: rm [OPTION]... FILE...
    echo "    -f"
    echo "    -c empty the trash bin"
    echo "    -d delete the file forever"
    echo "    -u recover the file (unrm)"
    echo "  example"
    echo "     rm file0 file1      : move the file file0 file1 to trash bin"
    echo "     rm -u file0 file1   : recover the file file0 file1"
    echo "     rm -d file0         : delete the file forever, it will can not be recoverd"
    echo "     rm -c               : empty the trash bin"
    echo "     You can check the log file $trashfolder/.log"
    echo "     To use rm -u, you can do "
    echo "        alias unrm=\"rm -u\""
    echo "     To use rm -d, you can do "
    echo "        alias rmd=\"rm -d\""
}

cleanAll(){
    $RM -rf $trashfolder
}

recover(){
    for i in "$@" ;do
        if [ "-" != "${i:0:1}" ]; then
            if [ "/" != "${i:0:1}" ]; then
                mvsf=$PWD/$i
            else
                mvsf=$i
            fi
            mvdf=$trashfolder/$(echo $mvsf | tr "/" "*" )
            if [ -e $mvdf ]; then
                mv  $mvdf $mvsf
                if [ 0 -eq $? ]; then
                mv  $mvdf $mvsf
                if [ 0 -eq $? ]; then
                    echo "restore $mvdf     $mvsf" >> $mvlogf
                fi
            else
                echo $mvsf dose not been stored
            fi
        fi
    done
}
########################### define function end  #############################

for i in "$@" ;do
    if [ "-" = "${i:0:1}" ]; then
        optargs="$optargs $i"
    fi
done

while getopts 'hiIfrvcud' OPT $optargs ;do
    case $OPT in
    f)
        foption=1;;
    d)
        /bin/rm $*
        exit 0;;
    v)
        voption=1;;
    r)
        roption=1;;
    i)
        ioption=1;;
    I)
        Ioption=1;;
    u)
        recover $*;
        exit 0;;
    c)
        cleanAll; exit 0;;
    h)
        rmhelp
        exit 0;;
    esac
done

if [ ! -e $trashfolder ]; then
    mkdir -p $trashfolder
fi

for i in "$@" ;do
    if [ "-" != "${i:0:1}" ]; then
        #if [ -d $i ]; then
            #echo $i is folder
        #elif [ -e $i ]; then
            #echo $i is file
        #fi

        if [ "/" != "${i:0:1}" ]; then
            mvsf=$PWD/$i
        else
            mvsf=$i
        fi
        basedf=$(echo $mvsf |tr "/" "*" )
        #basedf=$(basename $i)
        mvdf=$trashfolder/$basedf
        if [ -e "$mvsf" ]; then
            if [ -e "$mvdf" ];then
                /bin/rm "$mvdf"
                mv -f "$mvsf" "$mvdf"
            else
                mv "$mvsf" "$mvdf"
            fi
        else
            if [ $foption != 1 ]; then
                echo "rm: cannot remove $mvsf: No such file or directory"
            fi
        fi
        if [ 0 -eq $? ]; then
            echo "store   $mvsf     $mvdf" >> $mvlogf
        fi
    fi
done



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理