LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2377|回复: 5

如何在linux命令行下恢复删除的文件

[复制链接]
发表于 2007-4-28 08:33:23 | 显示全部楼层 |阅读模式
如何在linux命令行下恢复删除的文件
发表于 2007-4-28 14:19:47 | 显示全部楼层
基本上无法恢复。

拜托不要再问这种问题,或者问之前至少搜索一下。
回复 支持 反对

使用道具 举报

发表于 2007-4-29 12:54:38 | 显示全部楼层
这个我也想知道,不过好象不同的文件系统是不一样的,还是建议以后建立一个空目录,要删除的时候把文件移动到这里,然后每隔一段时间清理一下的比较好点。
回复 支持 反对

使用道具 举报

发表于 2007-4-29 13:37:02 | 显示全部楼层
Post by gao_linfeng
这个我也想知道,不过好象不同的文件系统是不一样的,还是建议以后建立一个空目录,要删除的时候把文件移动到这里,然后每隔一段时间清理一下的比较好点。

这种和把 rm 定义为 rm -i 的别名一样,都算不上好的解决方案。
回复 支持 反对

使用道具 举报

发表于 2007-4-30 09:51:55 | 显示全部楼层
是有的,以前看到过,到sf.net上搜一下吧。
不过是类似Final Recovery一类的东西,如果相应的数据已被覆盖,就不行了。
回复 支持 反对

使用道具 举报

发表于 2010-1-8 14:58:51 | 显示全部楼层
字符界面的回收站第二版

以前用shell写过一个rm但是并没有恢复功能,前一阵cu论坛上有个网友说能不能增加一个功能就是把删除的文件的原路径记录到一个文件中,所以我又写了这个rm增加了恢复功能,同时也能看到删除文件的原来路径。
rm功能:
rm -r
rm -f
rm -rf
rm -i
rm --help
以上功能和 平时用的rm命令功能基本一样,
rm -l新增加的功能
会把所有删除的文件显示一个列表,前边有行号你想恢复哪个文件只要按相应的行号就行,并按y确认


#!/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`/from1random:`date`" >> $mvlog
       mv "$from1"  "$garbage/$from1random"
    fi
    }
    function more
    {
    for file in *
    do
    echo "`pwd`/filerandom:`date`" >> $mvlog
    mv $file "$garbage/$filerandom"
    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`/from2random:`date`" >> $mvlog
             mv "$from2" "$garbage/$from2random"
        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 "lease 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
  • 回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表