删除文件与恢复文件
时间:2008-05-02 来源:剑心通明
作者 pupilzeng
这一共有三个脚本,他们是一套的
1)del:删除文件,其实是把它移动到/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:[email protected]
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:[email protected]
#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:[email protected]
#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=/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
这一共有三个脚本,他们是一套的
1)del:删除文件,其实是把它移动到/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:[email protected]
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:[email protected]
#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:[email protected]
#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=/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
相关阅读 更多 +