Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3322510
  • 博文数量: 631
  • 博客积分: 10716
  • 博客等级: 上将
  • 技术积分: 8397
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-01 22:35
文章分类

全部博文(631)

文章存档

2020年(2)

2019年(22)

2018年(4)

2017年(37)

2016年(22)

2015年(1)

2013年(12)

2012年(20)

2011年(19)

2010年(20)

2009年(282)

2008年(190)

分类:

2009-02-23 20:31:22


这一共有三个,他们是一套的
1):删除,其实是把它移动到/trash/$user/下,不同的用户有不同的存放目录,同时用该目录下的.record文

件记录文件原来的路径,时间,以备只用。
2)recover:恢复文件,通过.record文件找到足够的,从而把它恢复
3)erase:这个是彻底的删除文件,从/trash/$user/目录下,相当于windows下的清空回收站。
为了,这个脚本是要在/trash/$user/目录下运行。
以下是这三个脚本
del:
#!/bin/bash
#move the file(s) to the /trash/~ instead of deleting.
#Author: pupilzeng
#E-mail:
USER=`whoami`
TRASH=/trash/$USER
RECORD=/trash/$USER/.record #record file
ORIG=`pwd`
DATE=`date +%T---%Y/%m/%d`

Usage ()
{
        echo "Usage: `basename $0` file(s)"
}

if [ "$1" = "-h" -o "$1" = "--help" ];then
        Usage
        exit 0
fi
if [ $# -le 0 ];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 `basename "$i"`
                echo -e "$PWD/`basename "$i"`\t\t$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

recover代码:
#!/bin/bash
#recover
#Author: pupilzeng
#E-mail:
#To recover the removed file(s) by script myrm
USER=`whoami`
TRASH=/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

erase代码:
#!/bin/bash
#erase
#Author: pupilzeng
#E-mail:
#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 <Usage:`basename $0` [Option] file(s)
Options:
  -f  :don't prompt before erase files
END
}

USER=`whoami`
TRASH=/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
阅读(878) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~