注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

我的博客

 
 
 

日志

 
 
关于我

砍过人吸过粉站在路边接过吻 当过兵站过岗耍过流氓入过党 上过班下过岗打过领导得过奖

网易考拉推荐

linux rm -rf 问题  

2010-12-09 11:42:47|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
最近连续2次发生了 rm -rf问题,这样我做的工作白费了。痛定思痛,网上找了一个脚本,rm时,把文件放到备份目录里,这样就有机会反悔了。

网易blog不能上传文件,也不能贴代码,垃圾。








#!/bin/bash
#-----------------------------------------------------------------
#     filename:                                                                   
#       author: wds                                                               
#        begin:2008.1.27                                                                   
#          end:2008.2.1                                                                   
#      version: v.2      
# script address: http://blog.chinaunix.net/u1/40306/index.html
#-----------------------------------------------------------------
from1=$1
from2=$2
garbage=$HOME/.garbage
mvlog=$garbage/mv.log
if [ ! -e $garbage ]
 then
    mkdir -p $garbage
    chmod 777 $garbage
fi

function rand
{
a=(0 1 2 3 4 5 6 7 8 9 a b c d e A B C D E F )
for ((i=0;i<7;i++));do
        echo -n ${a[$RANDOM % ${#a[*]}]}
done
}
random=$(rand)

function rm1
{
if [ -d "$from1" ]
 then
   echo "rm: cannot remove '$from1/' : Is a directory"
else
   echo "`pwd`/:$from1:$random:`date`" >> $mvlog
   mv "$from1"  "$garbage/$from1:$random"
fi
}

function more
{
for file in *
do
echo "`pwd`/:$file:$random:`date`" >> $mvlog
mv $file "$garbage/$file:$random"
done 2> /dev/null
}

function rmi
{
if [ ! -d "$from2" ]
 then
   echo -n "rm:remove regular empty file '$from2'?" ; read answer;
    if [ "$answer" = 'y' -o "$answer" = 'Y' ]
     then
        echo "`pwd`/:$from2:$random:`date`" >> $mvlog
         mv "$from2" "$garbage/$from2:$random"
    fi
else
   echo "rm: cannot remove directory '$from2': Is a directory" 
fi
 
}

function rmf
{
if [ ! -d "$from2" ]
 then
        echo "`pwd`/:$from2:$random:`date`" >> $mvlog
         mv "$from2" "$garbage/$from2:$random"
else
   echo "rm: cannot remove directory '$from2': Is a directory"
fi

}

function rmr
{
if [ -e "$from2" ]
 then
        result=$(echo $from2 | sed 's/\///g')
        echo "`pwd`/:$result:$random:`date`" >> $mvlog
         mv "$result" "$garbage/$result:$random"
fi


}

function rml
{
while :
do
clear
line=$(cat -n $mvlog | awk -F : '{print $1,"FileName:"$2,    "Time:"$4}')
linecount=$(cat $mvlog | wc -l)
echo -e "$line\c"
echo
echo
echo "Please input number you want revent(line count:$linecount)--exit(e)"
read answer
 if [ "$answer" = e -o "$answer" = E ]
  then
    break
 else
    (
     echo "please input y(sure:)"
       read answer1
       if [ "$answer1" = y -o "$answer" = Y ]
        then
          address=$(sed -n "$answer""p" $mvlog | awk -F : '{print $1}')
          filename=$(sed -n "$answer""p" $mvlog | awk -F : '{print $2}')
          filerand=$(sed -n "$answer""p" $mvlog | awk -F : '{print $3}')
          fullname=$address$filename
           if [ -e "$fullname" ]
            then
               echo "The file exist!"
               sleep 1
           else
               old="$garbage/$filename:$filerand"
               new="$address$filename"
               mv "$old" "$new"
               delline=$( cat $mvlog | sed "$answer""d" | sort -o $mvlog)
               echo "update ok!!!"
               sleep 1
           fi
       fi
    )
  fi
done


}

function help
{
echo "
-i)  If you wants delete some file , this function is confirm you want,the same as old rm.
-f)  If you wants delete some directory ,you can use this function ,the same as old rm.
-r)  If you wants delete some directory of file ,this function can use , the same as old rm.
-l)  This is new function,is you wants resume some file or directory you can use this function,
     first this function can list some file in you garbage , these have some number ,if you
     wants resume 1,you can input 1 and then input y to confirm.

If you want add some function or some new idear please contact me...
     author:wds
      email:7717060@sina.com
     
"
}


case "$1"
  in
[a-z]) : ;;
[0-9]) : ;;
[A-Z]) : ;;
    ?) more ;;
    *) :;;
esac

if [ "$#" -eq 0 ]
 then
   echo -n "rm: missing operand
Try 'rm --help' for more informaction.
"
fi

if [ "$#" -eq 1 ]
 then
   case "$from1"
      in
       -i) echo "Try 'rm --help' for more informaction."; break ;;
       -f) echo "Try 'rm --help' for more informaction."; break ;;
       -r) echo "Try 'rm --help' for more informaction."; break ;;
      
       -l) rml ;;
   --help) help;;
        *) rm1;;
   esac
fi



if [ "$#" -eq 2 ]
 then
    case "$from1"
      in
       -i) rmi ;;
       -f) rmf ;;
       -r) rmr ;;
       -l) rml ;;
      -rf) rmr ;;
   --help) help ;;
    esac
fi 


  if [ "$#" -gt 2 ]
         then
           for file in $*
             do
               mv $file "$home/"
           done 2> /dev/null
        fi
  评论这张
 
阅读(3659)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016