删除文件跟恢复文件系列
发表于:2007-07-04来源:作者:点击数:
标签:
这一共有三个脚本,他们是一套的 1)del:删除文件,其实是把它移动到/trash/user/下,不同的用户有不同的存放目录,同时用该目录下的.record文件记录文件原来的路径,删除时间,以备恢复只用。 2)recover:恢复文件,通过.record文件找到足够的信息,从而把它
这一共有三个脚本,他们是一套的
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: shixinzeng@sjtu.edu.cn
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 o
clearcase/" target="_blank" >ccurred 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
if [ "`dirname "$i"`" = "." ] #relative path
then
echo -e "$PWD/`basename "$i"`\t\t$DATE ">>$RECORD
else
echo -e ""$i" \t\t$DATE">>$RECORD
fi
fi
else
echo "You have not enough permission to delete $i!"
fi
done
exit 0
--------------------------------------------------------------------------------
recover:
源码:--------------------------------------------------------------------------------
#!/bin/bash
#recover
#Author: pupilzeng
#E-mail: shixinzeng@sjtu.edu.cn
#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: shixinzeng@sjtu.edu.cn
#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
原文转自:http://www.ltesting.net