Linux 下利用 Lsof 恢復誤刪文件

 

  原理:在Linux系統的/proc 分區下保存着進程的目錄和名字,包含fd(文件描述符)和其下的子目錄(進程打開文件的連接),那麼若是刪除了一個文件,還存在一個 inode的引用:/proc/進程號/fd/文件描述符。咱們只要知道當前打開文件的進程pid和文件描述符fd就能利用lsof工具列出進程打開的文件。 node

 

1、將 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: 

2、假設誤刪文件 ls.txt code

  1: [root@localhost script]# rm ls.txt
  2: rm:是否刪除 通常文件 「ls.txt」? y


3、利用lsof找到進程65十一、並拷貝恢復,只能在這個文件被使用或調用的狀況下有效 進程

  ip

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
  2: 
相關文章
相關標籤/搜索