
Linux Command Line
The “lsof” command in Linux is a powerful tool for displaying information about open files and processes. It can be used to troubleshoot issues related to file and network connections, and can provide a wealth of information about the status of a system.
One common use for the “lsof” command is to check for open network connections. By using the “-i” option, you can specify a protocol (such as TCP or UDP) and see a list of all open connections using that protocol. For example, to see a list of all open TCP connections, you can use the command “lsof -i TCP”.
Another useful feature of “lsof” is the ability to display information about specific processes. By using the “-p” option followed by a process ID, you can see a list of all open files associated with that process. This can be helpful when trying to understand the resources a particular process is using or if the process is hanging.
You can also use the “-u” option to display a list of open files associated with a specific user. This can be useful for monitoring resource usage by individual users on a shared system.
In addition to these examples, “lsof” also offers a wide range of other options for displaying information about open files and processes. For example, the “-c” option can be used to display a list of open files for a specific command, and the “-d” option can be used to display a list of open files associated with a specific file descriptor.
Examples of using lsof
- List all open files:
lsof
- List all open network connections:
lsof -i
- List all open network connections using TCP:
lsof -i TCP
- List all open files associated with a specific process ID:
lsof -p [PID]
- List all open files associated with a specific user:
lsof -u [username]
- List all open files associated with a specific command:
lsof -c [command name]
- List all open files associated with a specific file descriptor:
lsof -d [file descriptor number]
- List all open files of a specific file type, such as regular files:
lsof +r
- List all open files that are being written to:
lsof +w
- List all open files on a specific mount point:
lsof +D /mount/point
These are just a few examples of the many options available with the “lsof” command. You can use these options in combination to refine your search and gather specific information about open files and processes on your Linux system. It is also possible to combine linux commands to get more detailed or specific information.
Combining commands
You can use “lsof” in combination with other commands such as “grep”, “awk”, “find” etc. to filter and analyze the information it provides.
For example:
- Use “grep” to filter the output of “lsof” for specific keywords:
lsof | grep [keyword]
- Use “awk” to extract specific fields from the output of “lsof”:
lsof | awk '{print $1, $2}'
- Use “find” to search for open files in a specific directory:
find /directory -type f -exec lsof {} +
- Use “sed” to remove unwanted parts of the output:
lsof | sed -n '/pattern/!p'
- Pipe the output of lsof to sort command to sort it by a specific column:
lsof | sort -k 2
These are a few examples of how you can use “lsof” in combination with other commands, you can use other command line tools and scripting languages such as python, perl etc to process the output of lsof and automate processes.
See our list of 75 Linux commands you should know about.