The Linux lsof
command is a command line utility that is used to list the open files on a Linux system. An open file can be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream, a network file (Internet socket, NFS file, or UNIX domain socket), or a file descriptor.
The lsof
command can be useful for a number of different purposes, including:
- Troubleshooting problems with open files or file descriptors
- Identifying processes that are using a particular file or network port
- Locating deleted files that are still open by a process
- Determining the files that a process has open
To use the lsof
command, simply type lsof
at the command prompt and press Enter. This will display a list of all open files on the system, along with the PID (process ID) of the process that has the file open, the user that owns the process, and the type and status of the file.
$ lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 202,1 4096 2272 /
init 1 root rtd DIR 202,1 4096 2 /
init 1 root txt REG 202,1 1449600 7965 /sbin/init
...
The lsof
command has a number of options that can be used to control the information that is displayed and the format of the output. For example, the -p
option can be used to limit the output to files that are open by a specific process.
For example, to display a list of open files for process ID 1234
, you could use the following command:
$ lsof -p 1234
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1234 root cwd DIR 202,1 4096 2272 /
init 1234 root rtd DIR 202,1 4096 2 /
init 1234 root txt REG 202,1 1449600 7965 /sbin/init
...
The lsof
command can also be used to display only files of a particular type, such as regular files, directories, or network files. To do this, you use the -t
option followed by the type of file that you want to display.
The system, you could also use the following command:
$ lsof -t -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 10671 root 3u IPv4 20451 0t0 TCP *:ssh (LISTEN)
sshd 10671 root 4u IPv6 20452 0t0 TCP *:ssh (LISTEN)
sshd 10673 root 3u IPv4 20451 0t0 TCP *:ssh (LISTEN)
sshd 10673 root 4u IPv6 20452 0t0 TCP *:ssh (LISTEN)
...
The lsof
command can also be used to display files that are open by a specific user. To do this, you use the -u
option followed by the username of the user.
For example, to display a list of open files for user john
, you could use the following command:
$ lsof -u john
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 12557 john cwd DIR 202,1 4096 143355 /home/john
bash 12557 john rtd DIR 202,1 4096 2 /
bash 12557 john txt REG 202,1 1472512 143354 /bin/bash
...
In conclusion, the lsof
command is a useful tool for displaying information about open files on a Linux system. Whether you are troubleshooting a problem with open files, locating deleted files, or simply curious about the open files on your system, the lsof
command can provide the information you need. With its many options and capabilities, the lsof
command is a valuable tool for any Linux user or system administrator.