删除文件跟恢复文件系列
点击次数:17 次 发布日期:2008-11-29 19:30:49 作者:源代码网
|
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 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 if [ "`dirname "$i"`" = "." ] #relative path then echo -e "$PWD/`basename "$i"` $DATE ">>$RECORD else echo -e ""$i" $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 < 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 源代码网供稿. |
