Linux中誤刪檔案的恢復方法

2021-09-12 15:05:48 字數 3753 閱讀 6598

做為linux的從業者大家對"rm -rf"這個命令肯定又愛又恨。

他的簡潔霸氣,勢不可擋,可謂"十步殺一人,千里不留行",

他乾淨徹底,不留痕跡,狠狠按下回車鍵後,「事了拂衣去,深藏功與名」。

但是,這個世界總是遺憾常有,而後悔藥不常有,刪錯後多半會呆呆地盯著螢幕,莫名奇妙地會"哀吾生之須臾,羨長江之無窮"。

環境準備:

[root@lh 1]

# yum -y install e2fsprogs-libs e2fsprogs e2fsprogs-devel

[root@lh 1]

# rpm -q e2fsprogs-libs e2fsprogs e2fsprogs-devel

安裝extundelete:

[root@lh 1]

# tar jxvf extundelete-0.2.4.tar.bz2

[root@lh 1]

# cd extundelete-0.2.4

[root@lh extundelete-0.2.4]

# ./configure && make && make install

注:如果您已經誤刪了檔案請跳過此章

格式化乙個分割槽

[root@lh 1]

# mkfs.ext4 /dev/sdc1

掛載並製造一些檔案

[root@lh 1]

# mkdir /diskc

[root@lh 1]

# mount /dev/sdc1 /diskc

[root@lh 1]

# cd /diskc

[root@lh diskc]

# cp /etc/passwd ./

[root@lh diskc]

# cp /bin/ls ./

[root@lh diskc]

# mkdir mlryj

[root@lh diskc]

# echo "welcome to 麻辣軟硬體" > mlryj/1.txt

檢視製造檔案的md5值

[root@lh diskc]

# md5sum passwd ls mlryj/1.txt

4b2c3e7fb5baf6f98307068d9c337620 passwd

c75a745aeabe3449655f2e7d3b081791 ls

dd69abb0cab322377ac06f6648f9ec8a mlryj/1.txt

檢視檔案詳情

[root@lh diskc]

# ll

總用量 140

drwx------ 2 root root 16384 3月 14 17:40 lost+found

-rwxr-xr-x 1 root root 117024 3月 14 17:43 ls

drwxr-xr-x 2 root root 4096 3月 14 17:43 mlryj

-rw-r--r-- 1 root root 1935 3月 14 17:43 passwd

刪除所有檔案

[root@lh diskc]

# rm -rf *

##找回被刪除的檔案

###取消掛載

[root@lh 1]

# umount /diskc

###可用的命令

此時使用如下三個命令:

1.檢視可以恢復哪些檔案:

extundelete /dev/sdc1 --inode 2
2.恢復某檔案:

extundelete /dev/sdc1 --restore-file somefile
3.恢復某目錄:

extundelete /dev/sdc1 --restore-directory /somedir
4.恢復所有檔案:

extundelete /dev/sdc1 --restore-all
命令執行後恢復後的檔案儲存在當前資料夾下的recovered_files目錄中

###檔案恢復及驗證

恢復單個檔案和目錄就不再贅述了,我們直接恢復所有檔案

[root@lh 1]

# extundelete /dev/sdc1 --restore-all

notice: extended attributes are not restored.

loading filesystem metadata ... 64 groups loaded.

loading journal descriptors ... 55 descriptors loaded.

searching for recoverable inodes in directory / ...

5 recoverable inodes found.

looking through the directory structure for deleted files ...

0 recoverable inodes still lost.

[root@lh 1]

# ls

recovered_files

檢視檔案是否成功找回

[root@lh 1]

# cd recovered_files/

[root@lh recovered_files]

# ls -l

總用量 124

-rw-r--r-- 1 root root 117024 3月 14 17:49 ls

drwxr-xr-x 2 root root 4096 3月 14 17:49 mlryj

-rw-r--r-- 1 root root 1935 3月 14 17:49 passwd

[root@lh recovered_files]

# md5sum passwd ls mlryj/1.txt

4b2c3e7fb5baf6f98307068d9c337620 passwd

c75a745aeabe3449655f2e7d3b081791 ls

dd69abb0cab322377ac06f6648f9ec8a mlryj/1.txt

[root@lh recovered_files]

# cat mlryj/1.txt

welcome to 麻辣軟硬體

我們可以看到,檔案的md5值和內容都沒有變化,恢復很成功了。

但是,雖然有此利器,我們使用"rm -rf"時,腦袋還是要謹慎一些,不要冒無謂的風險。至少從現在看:

只是目錄的"總用量"和恢復的檔案許可權發生了變化。

git 誤刪分支恢復方法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!在使用git的過程中,因為人為因素造成分支 commit 被刪除,可以使用以下步驟進行恢復。1.建立分支 abc git branch abc2.檢視分支列表 git branch a abc develop remotes origin dev ...

git 誤刪分支恢復方法

1.建立分支 abc git branch abc 2.檢視分支列表 git branch a abc develop remotes origin dev develop 3.切換到abc分支,隨便修改一下東西後 commit 切換分支 git checkout abc 建立乙個檔案 echo a...

oracle誤刪資料恢復方法

誤刪資料恢復 刪除表資料有三種方式 delete drop 和truncate delete誤刪除的解決方法 原理 利用oracle 提供的閃回方法,如果在刪除資料後還沒做大量的操作 只要保證被刪除資料的塊沒被覆寫 就可以利用閃回方式直接找回刪除的資料 具體步驟為 例如 誤刪除了 100條資料 刪除...