Master the Linux ‘lsof’ Command: A Comprehensive Guide for Beginners
This article provided a comprehensive guide on the Linux ‘lsof’ command, covering its history, use cases, usage, parameters, and effective techniques. Through various examples, newcomers to Linux can gain an understanding and effectively use the ‘lsof’ command.
Instructions
This guide aims to provide a comprehensive understanding of the ‘lsof’ command in Linux. It covers the history, usage, and applications of ‘lsof’. Additionally, it discusses commonly used and additional supported parameters and provides a number of common use cases and effective techniques for using the ‘lsof’ command.
History
The ‘lsof’ command has been a part of Unix/Linux systems for many years. It’s an invaluable tool for developers and system administrators who need to investigate system resources.
When and why to use it
When you need to list open files handled by the system processes, the ‘lsof’ command is useful. It provides extensive information on all files that are opened by processes.
How to use it
The ‘lsof’ command can be run without any options, which will list all the open files handled by all active processes.
$ lsof
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 8,1 4096 2 /
systemd 1 root rtd DIR 8,1 4096 2 /
The commonly used parameters
u
List files owned by user(s).
$ lsof -u root
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 8,1 4096 2 /
systemd 1 root rtd DIR 8,1 4096 2 /
c
List files for specific command.
$ lsof -c systemd
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 8,1 4096 2 /
systemd 1 root rtd DIR 8,1 4096 2 /
Other supported parameters
-a
: AND selections-b
: Avoid kernel blocks.-d
: File descriptor selection.-D
: Directory selection.-f
: File structure selection.-g
: Process group selection.-i
: Select IPv[46], TCP or UDP files.-l
: List UID numbers.-n
: Avoid network names.-P
: Avoid port names.-r
: Repeat listing.-s
: File size selection.-S
: Interrupt display.
Most common use cases
One of the most common uses of ‘lsof’ is to determine which process is using a specific file or directory.
$ lsof /path/to/directory
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
process 123 user cwd DIR 8,1 4096 2 /path/to/directory
The tricky skills
You can use ‘lsof’ with other commands to achieve more specific information. For instance, you can use ‘lsof’ with ‘grep’ to find specific processes that are using a certain file or directory.
$ lsof | grep "/path/to/directory"
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
process 123 user cwd DIR 8,1 4096 2 /path/to/directory
What needs to be noted
- ‘lsof’ provides extensive information, so it can be overwhelming at first. Start with basic usage and slowly incorporate more options as needed.
- The output of ‘lsof’ can be quite extensive. Use piping with ‘less’ or ‘more’ for easier reading.
Conclusion
‘lsof’ is a powerful and versatile command that is crucial for system administrators and developers. By mastering ‘lsof’, you can gain detailed insight into how your system’s resources are being used.