linux command - lsof View which files a process has opened or which process a file is used by

What is the lsof command ?

Information about files opened by a process can be listed . The file being opened can be

1. Ordinary files, 2. Directories 3. Network file system files, 4. Character device files 5. (function) shared libraries 6. Pipes, named pipes 7. Symbolic links

8. The underlying socket word stream, network socket, unix domain name socket

9. In linux , most things are treated as files.....and many others

How to use lsof

The use of the lsof command is mainly introduced here in the form of a case.

1. List all open files:

lsof

Note: If you do not add any parameters, all opened files will be opened. It is recommended to add the following parameters for specific positioning

2.  See who is using a certain file

lsof   /filepath/file

3. Recursively view the file information of a directory

lsof +D /filepath/filepath2/

Note: When +D is used, all subdirectories and files in the corresponding directory will be listed

4. Than use the +D option to traverse and view all file information in a directory

lsof | grep ‘/filepath/filepath2/’

5. List file information opened by a user

lsof  -u username

Note: -u option, u is actually the abbreviation of user

6. List information about files opened by a program

lsof -c mysql

Remarks: The -c option will list the files of all programs starting with mysql. In fact, you can also write lsof | grep mysql, but the first method is obviously a few fewer characters than the second method.

7. List file information opened by multiple programs

lsof -c mysql -c apache

8. List information about files opened by a user and a program

lsof -u test -c mysql

9. List open file information except for a certain user

lsof   -u ^root

Note: The ^ symbol before the user name will prevent the process opened by the root user from being displayed.

10. Display the open file by a process number

lsof -p 1

11. List file information corresponding to multiple process numbers

lsof -p 123,456,789

12. List file information opened by other process IDs except for a certain process ID

lsof -p ^1

13. List all network connections

lsof -i

14. List all tcp network connection information

lsof  -i tcp

15. List all udp network connection information

lsof  -i udp

16. List who is using a certain port

lsof -i :3306

17. List who is using a specific udp port

lsof -i udp:55

specific tcp port

lsof -i tcp:80

18. List all active network ports of a user

lsof  -a -u test -i

19. List all network filesystems

lsof -N

20. Domain name socket file

lsof -u

21. File information opened by a user group

lsof -g 5555

22. List the corresponding file information according to the file description

lsof -d description(like 2)

23. List file information according to file description range

lsof -d 2-3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325775807&siteId=291194637