This is our on-going series of Linux commands and in this article we are going to review lsof command with practical examples. lsof meaning ‘LiSt Open Files’ is used to find out which files are open by which process. As we all know Linux/Unix considers everything as a files (pipes, sockets, directories, devices etc).One of the reason to use lsof command is when a disk cannot be unmounted as it says the files are being used. With the help of this command we can easily identify the files which are in use.
1. List all Open Files with lsof Command
In the below example, it will show long listing of open files some of them are extracted for better understanding which displays the columns like Command, PID, USER, FD, TYPE etc.
Sections and it’s values are self-explanatory. However, we’ll review FD & TYPE columns more precisely. FD – stands for File descriptor and may seen some of the values as:
Also in FD column numbers like 1u is actual file descriptor and followed by u,r,w of it’s mode as:
TYPE – of files and it’s identification.
2. List User Specific Opened Files
The below command will display the list of all opened files of user tecmint.
To find out all the running process of specific port, just use the following command with option -i. The below example will list all running process of port 22.
4. List Only IPv4 & IPv6 Open Files
In below example shows only IPv4 and IPv6 network files open with separate commands.
5. List Open Files of TCP Port ranges 1-1024
To list all the running process of open files of TCP Port ranges from 1-1024.
6. Exclude User with ‘^’ Character
Here, we have excluded root user. You can exclude a particular user using ‘^’ with command as shown above.
7. Find Out who’s Looking What Files and Commands?
Below example shows user tecmint is using command like ping and /etc directory .
8. List all Network Connections
The following command with option ‘-i’ shows the list of all network connections ‘LISTENING & ESTABLISHED’.
9. Search by PID
The below example only shows whose PID is 1.
10. Kill all Activity of Particular User
Sometimes you may have to kill all the processes for a specific user. Below command will kills all the processes of tecmint user.
* 15 Linux lsof Command Examples (Identify Open Files)