模拟rm的删除脚本

功能:
1、模拟rm命令删除文件,但rm删除后无法恢复删除的文件。所以当rm误删除后常常造成无法挽回的损失。这3个脚本相当于模拟windows回收站的功能,使你在误删除后可恢复原来的操作。
2、记录所有用的删除操作,有利于之后审计与监控。
3、其中主要包括del.sh、erase.sh、recover.sh三个脚本。

1)del.sh
#!/bin/bash
#move the file(s) to the /trash/~ instead of deleting.
USER=`whoami`
TRASH=/usr/games/trash/$USER
RECORD=$TRASH/.record #record file
ORIG=`pwd`
DATE=`date +%T---%Y/%m/%d`
Usage ()
{
        echo "Usage: rm file(s)"
}
if [ "$1" = "-h" -o "$1" = "--help" ];then
        Usage
        exit 0
fi
if [ $# -lt 1 ];then
        Usage
        exit 1
fi
if [ ! -d $TRASH ];then
        mkdir -p $TRASH
fi
for i in "$@"
        do
        if [ -w "$i" ];then
                mv "$i" $TRASH
                if [ $? -ne 0 ];then
                echo "Something wrong occurred while delete file $i"
                #but now i won't exit,because there may be other files to be deleted!
                else
                #now write the record file
                #the lines below were modified!
                CURRENTDIR=$PWD #*
                cd $TRASH
                #cd `basename "$i"`
                echo -e "$PWD/`basename "$i"`tt$DATE ">>$RECORD
                cd $CURRENTDIR #* return to original working directory
                 #The lines marked * can be omitted! For safety,they remain there.
               #---------------------------------------------------------------
                fi
                else
                        echo "You have not enough permission to delete $i!"
        fi
        done
exit 0
 
2)、erase.sh
#!/bin/bash
#erase
#erase the files in trash that you are sure they needn't at all.
#for assurance,you should do it in /trash/user directory.
Usage ()
{
   cat <<END
 Usage:`basename $0` [Option] file(s)
 Options:
  -f  :don't prompt before erase files
END
}
USER=`whoami`
TRASH=/usr/games/trash/$USER
RECORD=$TRASH/.record
FORCE=no
TEMP=$TRASH/.temp
if [ $# -lt 1 ]
then
        echo "Wrong parameters"
        Usage
        exit 1
fi
if [ $PWD != $TRASH ]
then
        echo "you should do it in $TRASH directory!"
        exit 1
fi
if [ "$1" = "-h" -o "$1" = "--help" ]
then
        Usage
        exit 0
fi 
if [ "$1" = "-f" ];then
        FORCE=yes
        shift
fi
for i in "$@"
do
        ANS=no
        if [ $FORCE = "yes" ];then
                rm -fr "$i"
        else
                echo -n "Do you really wanna erase "$i"? Yes|[No]:"
                read ANS
                case $ANS in
                "Y"|"y"|"Yes"|"yes")
                        rm -fr "$i"
                        ;;
                *)
                        continue
                        ;;
                esac
        fi
        if [ $? -eq 0 ];then
        #now remove the records
        grep -v "$i" $RECORD >$TEMP
        mv -f $TEMP $RECORD
        fi
done
exit 0
 
3)、recover.sh
#!/bin/bash
#recover
#To recover the removed file(s) by script myrm
USER=`whoami`
TRASH=/usr/games/trash/$USER
RECORD=$TRASH/.record
TEMP=$TRASH/.temp
Usage ()
{
        echo "Usage:`basename $0` file(s)"
}
if [ "$1" = "-h" -o "$1" = "--help" ];then
        Usage
        exit 0
fi
for i in "$@"
do
        DEST=`grep "$i" $RECORD |awk '{ print $1}'`
        mv -f "$i" $DEST
        if [ $? -ne 0 ];then
                echo "Something occurred!"
                exit 1
        else
                echo "Recovered $DEST"
        #remove record from $RECORD
        grep -v "$i" $RECORD >$TEMP
        mv -f $TEMP $RECORD
        fi
done
exit 0
 

你可能感兴趣的:(shell,职场,休闲,rm,删除脚本)