Principle: In the Linux system/proc partition holds the process directory and the name, contains the FD (file descriptor) and its sub-directory (the process opens the file link), if deleted a file, there is also an inode reference:/proc/process number/fd/file descriptor. As long as we know the process PID and file descriptor of the currently open file, FD can use the Lsof tool to list the files opened by the process.
First, the LS manual filter out the main control after the redirect to the file Ls.txt, and more view, CTRL + Z pause the view operation
1: [[email protected] script]# man ls |col-b > Ls.txt
2: [[email protected] script]# more Ls.txt
3:ls (1) User Commands LS (1)
4:
1: [1]+ Stopped more Ls.txt
2: [[email protected] script]#
3: [[email protected] script]# jobs
4: [1]+ Stopped more Ls.txt
5:
Second, assume that the file is mistakenly deleted Ls.txt
1: [[email protected] script]# RM ls.txt
2:RM: Do you want to delete the generic file "Ls.txt"? Y
Third, using lsof to find process 6511, and copy recovery, only if the file is used or invoked in the case of valid
3: [[email protected] script]# lsof |grep ls.txt
4:more 6511 Root 3r REG 253,0 7300 1083699/opt/script/ls.txt (Deleted)
5:
1: [[email protected] script]# ls-l/proc/6511/fd/
2:0 1 2 3
3: [[email protected] script]# ls-l/PROC/6511/FD/3
4:lr-x------1 root root 10-30 21:21/proc/6511/fd/3-/opt/script/ls.txt (Deleted)
5:
1:CP/PROC/6511/FD/3 ls.txt.saved
Recover the files that were used in the process of mis-deletion