欢迎来到电脑知识学习网,专业的电脑知识大全学习平台!

手机版

Linux下利用Lsof恢复误删文件的方法

操作系统 发布时间:2019-02-15 09:28:40
本文介绍如何在Linux下利用Lsof恢复误删文件
 
 
原理:在Linux系统的/proc 分区下保存着进程的目录和名字,包含fd(文件描述符)和其下的子目录(进程打开文件的链接),那么如果删除了一个文件,还存在一个 inode的引用:/proc/进程号/fd/文件描述符。我们只要知道当前打开文件的进程pid和文件描述符fd就能利用lsof工具列出进程打开的文件。
一、将 ls 的手册过滤掉主要控制符后重定向到文件ls.txt 中,并用more查看,CTRL + Z 暂停查看操作
  1: [root@localhost script]# man ls |col -b > ls.txt
  2: [root@localhost script]# more ls.txt
  3: LS(1)                    User Commands                           LS(1)
  4: 
 1: [1]+  Stopped                 more ls.txt
  2: [root@localhost script]#
  3: [root@localhost script]# jobs
  4: [1]+  Stopped                 more ls.txt
  5:     
二、假设误删文件 ls.txt
  1: [root@localhost script]# rm ls.txt
  2: rm:是否删除 一般文件 “ls.txt”? y
三、利用lsof找到进程6511、并拷贝恢复,只能在这个文件被使用或调用的情况下有效
3: [root@localhost script]# lsof |grep ls.txt
  4: more      6511      root    3r      REG      253,0     7300    1083699 /opt/script/ls.txt (deleted)
  5: 
  1: [root@localhost script]# ls -l /proc/6511/fd/
  2: 0  1  2  3
  3: [root@localhost script]# ls -l /proc/6511/fd/3
  4: lr-x------ 1 root root 64 10-30 21:21 /proc/6511/fd/3 -> /opt/script/ls.txt (deleted)
  5: 
 1: cp /proc/6511/fd/3 ls.txt.saved

责任编辑:电脑知识学习网

操作系统