Use Lsof in Linux to restore accidentally deleted files
Source: Internet
Author: User
Article Title: Use Lsof in Linux to restore files accidentally deleted. Linux is a technology channel of the IT lab in China. Including basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source. First, we will introduce the basic concepts of some files. Files are actually links to inode, inode links contain all attributes of a file, such as permissions and owners, and data block addresses (the file is stored in these data blocks on the disk ). When you delete an object (rm), the link to inode is actually deleted and the inode content is not deleted. The process may still be in use. Only when all links of inode are completely removed, these data blocks can be written into new data.
The proc file system can help us recover data. Each process on the system has a directory and its own name in/proc: it contains a fd (file descriptor) subdirectory (the process needs to open all the links to the file ). If you delete a file from the file system, there is also an inode reference:
/Proc/process ID/fd/file descriptor
Next, you need to know the process ID (pid) and file descriptor (fd) of the opened file ). These can be obtained through the lsof tool. lsof means "list open files, list files opened by (processes ". Then you can copy the data to be restored from/proc.
The following describes how to use lsof to restore accidentally deleted files on the Fedora Core 5 system:
Environment
HOST: a vmware-based virtual independent host is used.
System: Fedora Core 5
Kernel: BRL. 16-1.2122 _ FC5
Lsof version:
[Zhaoke @ fedora5 ~] $/Usr/sbin/lsof-v
Lsof version information:
Revision: 4.77
Preparations:
If your system does not have lsof installed, you can obtain it from the author's website or pbone.
Recovery Process:
First, we need to create a text file, delete it, and restore it:
[Zhaoke @ fedora5 ~] $ Man lsof | col-B> myfile
Then let's take a look at the file content:
[Zhaoke @ fedora5 ~] $ Less myfile
You can see all the text help information of lsof.
Press Ctrl-Z to exit the less command, and then view the file attributes at the shell prompt:
Stat: cannot stat 'myfile': No such file or directory
The myfile file is deleted.
At this time, you should not terminate the process that is still using files. It is difficult to restore the file once it is terminated.
Now let's start retrieving data. First, use lsof to check it:
[Zhaoke @ fedora5 ~] $ Lsof | grep myfile
Less 9104 zhake 4r REG 253,0 116549 492686/home/zhake/myfile (deleted)
The first line is the name of the process (command name), the second line is the process number (PID), and the fourth line is the file descriptor (r indicates a common file ), now you know that the 9104 process still opens a file. The file descriptor is 4. then we start to copy data from/proc. You may consider using cp-a, but it does not actually work. You will copy a symbolic link pointing to the deleted file:
[Zhaoke @ fedora5 ~] $ Ls-l/proc/9104/fd/4
Lr-x -- 1 zhake 64 Nov 20/proc/9104/fd/4->/home/zhake/myfile (deleted)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service