天天看點

[轉載]字元界面的資源回收筒第二版

<b>【轉載】字元界面的資源回收筒第二版</b>

以前用shell寫過一個rm但是并沒有恢複功能,前一陣cu論壇上有個網友說能不能增加一個功能就是把删除的檔案的原路徑記錄到一個檔案中,是以我又寫了這個rm增加了恢複功能,同時也能看到删除檔案的原來路徑。

rm功能:

rm -r

rm -f

rm -rf

rm -i

rm --help

以上功能和 平時用的rm指令功能基本一樣,

rm -l新增加的功能

會把所有删除的檔案顯示一個清單,前邊有行号你想恢複哪個檔案隻要按相應的行号就行,并按y确認

#!/bin/bash

#-----------------------------------------------------------------

#     filename:                                                                   

#       author: wds                                                               

#        begin:2008.1.27                                                                   

#          end:2008.2.1                                                                   

#      version: v.2      

# script address: http://blog.chinaunix.net/u1/40306/index.html

from1=$1

from2=$2

garbage=$HOME/.garbage

mvlog=$garbage/mv.log

if [ ! -e $garbage ]

 then

    mkdir -p $garbage

    chmod 777 $garbage

fi

function rand

{

a=(0 1 2 3 4 5 6 7 8 9 a b c d e A B C D E F )

for ((i=0;i&lt;7;i++));do

        echo -n ${a[$RANDOM % ${#a[*]}]}

done

}

random=$(rand)

function rm1

if [ -d "$from1" ]

   echo "rm: cannot remove '$from1/' : Is a directory"

else

   echo "`pwd`/:$from1:$random:`date`" &gt;&gt; $mvlog

   mv "$from1"  "$garbage/$from1:$random"

function more

for file in *

do

echo "`pwd`/:$file:$random:`date`" &gt;&gt; $mvlog

mv $file "$garbage/$file:$random"

done 2&gt; /dev/null

function rmi

if [ ! -d "$from2" ]

   echo -n "rm:remove regular empty file '$from2'?" ; read answer;

    if [ "$answer" = 'y' -o "$answer" = 'Y' ]

     then

        echo "`pwd`/:$from2:$random:`date`" &gt;&gt; $mvlog

         mv "$from2" "$garbage/$from2:$random"

    fi

   echo "rm: cannot remove directory '$from2': Is a directory" 

function rmf

   echo "rm: cannot remove directory '$from2': Is a directory"

function rmr

if [ -e "$from2" ]

        result=$(echo $from2 | sed 's/\///g')

        echo "`pwd`/:$result:$random:`date`" &gt;&gt; $mvlog

         mv "$result" "$garbage/$result:$random"

function rml

while :

clear

line=$(cat -n $mvlog | awk -F : '{print $1,"FileName:"$2,    "Time:"$4}')

linecount=$(cat $mvlog | wc -l)

echo -e "$line\c"

echo

echo "Please input number you want revent(line count:$linecount)--exit(e)"

read answer

 if [ "$answer" = e -o "$answer" = E ]

  then

    break

 else

    (

     echo "please input y(sure:)"

       read answer1

       if [ "$answer1" = y -o "$answer" = Y ]

        then

          address=$(sed -n "$answer""p" $mvlog | awk -F : '{print $1}')

          filename=$(sed -n "$answer""p" $mvlog | awk -F : '{print $2}')

          filerand=$(sed -n "$answer""p" $mvlog | awk -F : '{print $3}')

          fullname=$address$filename

           if [ -e "$fullname" ]

            then

               echo "The file exist!"

               sleep 1

           else

               old="$garbage/$filename:$filerand"

               new="$address$filename"

               mv "$old" "$new"

               delline=$( cat $mvlog | sed "$answer""d" | sort -o $mvlog)

               echo "update ok!!!"

           fi

       fi

    )

  fi

function help

echo "

-i)  If you wants delete some file , this function is confirm you want,the same as old rm.

-f)  If you wants delete some directory ,you can use this function ,the same as old rm.

-r)  If you wants delete some directory of file ,this function can use , the same as old rm.

-l)  This is new function,is you wants resume some file or directory you can use this function,

     first this function can list some file in you garbage , these have some number ,if you

     wants resume 1,you can input 1 and then input y to confirm.

If you want add some function or some new idear please contact me...

     author:wds

      email:[email protected]

"

case "$1"

  in

[a-z]) : ;;

[0-9]) : ;;

[A-Z]) : ;;

    ?) more ;;

    *) :;;

esac

if [ "$#" -eq 0 ]

   echo -n "rm: missing operand

Try 'rm --help' for more informaction.

if [ "$#" -eq 1 ]

   case "$from1"

      in

       -i) echo "Try 'rm --help' for more informaction."; break ;;

       -f) echo "Try 'rm --help' for more informaction."; break ;;

       -r) echo "Try 'rm --help' for more informaction."; break ;;

       -l) rml ;;

   --help) help;;

        *) rm1;;

   esac

if [ "$#" -eq 2 ]

    case "$from1"

       -i) rmi ;;

       -f) rmf ;;

       -r) rmr ;;

      -rf) rmr ;;

   --help) help ;;

    esac

fi 

  if [ "$#" -gt 2 ]

         then

           for file in $*

             do

               mv $file "$home/"

           done 2&gt; /dev/null

        fi

原文http://blog.chinaunix.net/u1/40306/showart_474636.html

繼續閱讀