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 @   小鲨鱼2018  阅读(773)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-06-22 R语言中实现在命令行中传参
2022-06-22 R语言中read.table中colClasses = "character"选项作用
2021-06-22 c语言中以10进制、8进制、16进制显示同一个数字
点击右上角即可分享
微信分享提示