shell: lsof (list open files, lists all open files)

table of Contents

1. The meaning of each column

2. Check the usage of a file

3. Query the usage of port 8080

4. Recover deleted files

5. View the opened file under a certain path

 6. View the files opened by the root user


list open files, lsof lists all open files of the active process, because everything is a file in linux.

1. The meaning of each column

COMMAND: The name of the process

PID: Process identifier

USER: process owner

FD: File descriptor, the application program recognizes the file through the file descriptor. Such as cwd, txt, etc. TYPE: file type, such as DIR, REG, etc.

DEVICE: Specify the name of the disk

SIZE: the size of the file

NODE: index node (identification of the file on the disk)

NAME: the exact name of the opened file

The file descriptor cwd value in the FD column represents the current working directory of the application, which is the directory where the application is started, unless it changes this directory itself.

Files of type txt are program codes, such as application binary files themselves or shared libraries, such as /lib/systemd/systemd shown in the above list.

The second value represents the file descriptor of the application, which is an integer returned when the file is opened. As in the last line of the file /run/systemd/journal/syslog type=DGRAM, its file descriptor is 21. u means that the file is opened and in read/write mode, not read-only (r) or write-only (w) mode. At the same time, a capital W indicates that the application has a write lock on the entire file. This file descriptor is used to ensure that only one application instance can be opened at a time. When initially opening each application, there are three file descriptors, from 0 to 2, which represent standard input, output, and error streams. Therefore, the FD of the file opened by most applications starts from 3.

Compared with the FD column, the Type column is more intuitive. The files and directories are called REG and DIR, respectively. And CHR and BLK, respectively, represent character and block devices; or UNIX, FIFO, and IPv4, respectively, represent UNIX domain sockets, first-in-first-out (FIFO) queues, and Internet protocol (IP) sockets. 

2. Check the usage of a file

3. Query the usage of port 8080

lsof -i:8080

4. Recover deleted files

A text file in the /home/baichao/c++/sock/socket/service_client directory is being opened by vim.

We delete the woshihaoren file

Then check if there is a process to open the service1 file

lsof | grep woshihaoren

7552 means that the PID is 7552, and 4u means that the file descriptor is 4.

View the content in /proc/7552/fd/4

head -n 10 4

Output to woshihaoren1 file 

5. View the opened file under a certain path

 /home/baichao/c++/sock/socket/service_client/service1 is running

lsof +D url

 6. View the files opened by the root user

lsof -u root

 

Guess you like

Origin blog.csdn.net/weixin_40179091/article/details/113279422