linux 中误删文件的恢复(被进程占用)

 

可以分为两种情况:

a、误删的文件正在被进程所使用

b、误删的文件没有被进程使用

 

001、 

[root@PC1 test01]# ls
[root@PC1 test01]# seq 5 > a.txt      ## 创建测试文件
[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# tail -f a.txt      ## 利用tail -f 文件表示该文件被进程使用
1
2
3
4
5

 

002、打开另一个终端,然后删除该文件

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# rm a.txt              ## 删除该文件
rm: remove regular file ‘a.txt’? y
[root@PC1 test01]# ls                    ## 确认已经删除

 

003、查找该文件

[root@PC1 test01]# lsof | grep a.txt       ## 查找该文件
tail      93777                 root    3r      REG              253,2        10   33554499 /home/test01/a.txt (deleted)

 

004、进入进程号目录

[root@PC1 test01]# lsof | grep a.txt
tail      93777                 root    3r      REG              253,2        10   33554499 /home/test01/a.txt (deleted)
[root@PC1 test01]# cd /proc/93777/fd
[root@PC1 fd]# ls
0  1  2  3  4

 

005、 找到被删除的文件

[root@PC1 fd]# ls -ltr       ## 列出详细信息,按照时间反向
total 0
lr-x------. 1 root root 64 Jun 22 23:21 4 -> anon_inode:inotify
lr-x------. 1 root root 64 Jun 22 23:21 3 -> /home/test01/a.txt (deleted)
lrwx------. 1 root root 64 Jun 22 23:21 2 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 22 23:21 1 -> /dev/pts/1
lrwx------. 1 root root 64 Jun 22 23:21 0 -> /dev/pts/1

 

006、复制该文件进行恢复

[root@PC1 fd]# cp ./3 /home/test01/a.txt     ## 利用cp进行恢复

 

007、检查

[root@PC1 fd]# cd /home/test01/
[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt          ## 说明文件已经恢复
1
2
3
4
5

 

posted @ 2023-06-22 23:28  小鲨鱼2018  阅读(658)  评论(0编辑  收藏  举报