博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux中通过lsof恢复删除的文件,前题是fd被占用。
阅读量:5056 次
发布时间:2019-06-12

本文共 1706 字,大约阅读时间需要 5 分钟。

http://www.serverwatch.com/tutorials/article.php/3822816/Recovering-Deleted-Files-With-lsof.htm

 

One of the more neat things you can do with the versatile utility lsof is use it to recover a file you've just accidentally deleted.

Tip of the Trade: Accidentally deleted files are easily recovered with lsof.

A file in Linux is a pointer to an , which contains the file data (permissions, owner and where its actual content lives on the disk). Deleting the file removes the link, but not the inode itself – if another process has it open, the inode isn't released for writing until that process is done with it.

Recent Tips
»
»
»
Read All

To try this out, create a test text file, save it and then type less test.txt. Open another terminal window, and type rm testing.txt. If you try ls testing.txt you'll get an error message. But! less still has a reference to the file. So:

> lsof | grep testing.txtless	4607	juliet  4r  REG 254,4   21             8880214 /home/juliet/testing.txt (deleted)

The important columns are the second one, which gives you the of the process that has the file open (4607), and the fourth one, which gives you the file descriptor (4). Now, we go look in /proc, where there will still be a reference to the inode, from which you can copy the file back out:

> ls -l /proc/4607/fd/4lr-x------ 1 juliet juliet 64 Apr  7 03:19              /proc/4607/fd/4 -> /home/juliet/testing.txt (deleted)> cp /proc/4607/fd/4 testing.txt.bk

Note: don't use the -a flag with cp, as this will copy the (broken) symbolic link, rather than the actual file contents.

 

Now check the file to make sure you've got what you think you have, and you're done!

转载于:https://www.cnblogs.com/hark0623/p/6709751.html

你可能感兴趣的文章
Windbg调试Sql Server 进程
查看>>
linux调度器系列
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
SVN服务器搭建和使用(三)(转载)
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
【安卓5】高级控件——拖动条SeekBar
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
Swift 中的指针使用
查看>>
Swift - 使用闭包筛选过滤数据元素
查看>>
alue of type java.lang.String cannot be converted to JSONObject
查看>>
搜索引擎选择: Elasticsearch与Solr
查看>>
JAVA设计模式之简单工厂模式与工厂方法模式
查看>>
③面向对象程序设计——封装
查看>>