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 inode, 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.

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.txt
less 4607 juliet 4r REG 254,4 21
8880214 /home/juliet/testing.txt (deleted)

The important columns are the second one, which gives you the PID 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/4
lr-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!

linux中通过lsof恢复删除的文件,前题是fd被占用。的更多相关文章

  1. Linux中.a,.la,.o,.so文件的意义和编程实现

    Linux中.a,.la,.o,.so文件的意义和编程实现    Linux下文件的类型是不依赖于其后缀名的,但一般来讲:        .o,是目标文件,相当于windows中的.obj文件     ...

  2. 诠释Linux中『一切都是文件』概念和相应的文件类型

    导读 在 Unix 和它衍生的比如 Linux 系统中,一切都可以看做文件.虽然它仅仅只是一个泛泛的概念,但这是事实.如果有不是文件的,那它一定是正运行的进程. 要理解这点,可以举个例子,您的根目录( ...

  3. linux中tar 打包指定路径文件

    linux中tar打包指定路径文件www.111cn.net 编辑:yahoo 来源:转载在linux系统中打包与解压文件我都可以使用tar命令来解决,只要使用不同的参数就可以实现不同的需要了,下面来 ...

  4. 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)

    原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...

  5. linux中c表示字符设备文件符号

    linux中c表示字符设备文件,b表示块设备文件,l表示符号链接文件,r表示可读权限,w表示可写权限.linux文件属性解读:文件类型:-:普通文件 (f)d:目录文件b:块设备文件 (block)c ...

  6. linux 中的页缓存和文件 IO

    本文所述是针对 linux 引入了虚拟内存管理机制以后所涉及的知识点.linux 中页缓存的本质就是对于磁盘中的部分数据在内存中保留一定的副本,使得应用程序能够快速的读取到磁盘中相应的数据,并实现不同 ...

  7. linux中touch命令参数修改文件的时间戳(转)

    linux中touch命令参数不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件,以下是linux中touch命令参数的使用方法: touch [-acm][-r ...

  8. linux中touch命令参数修改文件的时间戳(转载)

    转自:http://os.51cto.com/art/200908/144237.htm linux中touch命令参数不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存 ...

  9. Linux基础教程 linux中使用find命令搜索文件常用方法记录

    find是linux非常强大的搜索命令,通过man find查看find手册,可以发现find的说明一屏接一屏,估计要看完也得花不少时间.兄弟连Linux培训 小编总结了下,整理出find常用的使用方 ...

随机推荐

  1. JAVA中获取键盘输入的方法总结

    Java程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!下面介绍三种解决方法: 方法一: ...

  2. Python-爬虫-猫眼T100

    目标站点需求分析 涉及的库 from multiprocessing import Poolfrom requests.exceptions import RequestExceptionimport ...

  3. Oracle 中 nvl、nvl2、nullif、coalesce、decode 函数的用法详解

    NVL(EXPR1,EXPR2) NVL2(EXPR1,EXPR2,EXPR3) NULLIF(EXPR1,EXPR2) COALESCE(EXPR1,,..,EXPRn) decode ------ ...

  4. Java的家庭记账本程序(D)

    日期:2019.2.8 博客期:031 星期一 今天是把程序的查询功能以列表的形式完成了! 截图如下:

  5. PHP中json_encode()使用须知,JSON数组和JSON对象

    ⊰ 偷偷的告诉你,这是一个很不谨慎就会踩得坑 ⊱  如下代码 (看了一下很简单,没毛病啊,老铁) $arr = array( '0'=>'a','1'=>'b','2'=>'c',' ...

  6. Django Rest Framework(2)

    目录 一.认证 二.权限 三.限制访问频率 四.总结 一.认证(补充的一个点) 认证请求头 #!/usr/bin/env python # -*- coding:utf-8 -*- from rest ...

  7. 高并发编程基础(java.util.concurrent包常见类基础)

    JDK5中添加了新的java.util.concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为同步容器将所有对容器状态的访问都串行化了,这样保证了线程的安全性,所以这种方法 ...

  8. AppServ安装到一半卡住的问题

    今天在笔记本安装AppServ的时候,运行到Installing mysql service时就卡住不动了,因为之前在自己的台式电脑安装过AppServ,当时是一步成功的,所以觉得这个问题莫名其妙,因 ...

  9. MySQL数据库权限分类

    一.权限表 mysql数据库中的3个权限表:user .db. host 权限表的存取过程是: 1)先从user表中的host. user. password这3个字段中判断连接的IP.用户名.密码是 ...

  10. 微信小程序--代码构成---WXSS 样式

    WXSS 样式 WXSS 具有 CSS 大部分的特性,小程序在 WXSS 也做了一些扩充和修改. 新增了尺寸单位.在写 CSS 样式时,开发者需要考虑到手机设备的屏幕会有不同的宽度和设备像素比,采用一 ...