Skip to content

lsof Linux Command Guide

The lsof command in Linux allows users to list all the open files and processes that are currently running. It can be used to identify which files are being accessed by which processes, making it a valuable tool for system administration and troubleshooting. By using lsof, you can manage your system resources more efficiently and gain insights into the activities of running processes.

lsof Syntax:

Terminal window
lsof [options] [parameters]

Options:

OptionDescription
-cList files opened by processes
-FOutput custom field delimiter
-iList internet and network files
-tDisplay only PIDs
-uList files opened by user
-pList files opened by process ID

Parameters:

ParameterDescription
PIDProcess ID to show files for
usernameUser name to show files for
filenameList files opened by filename
directoryList files opened in directory
portList files opened on port number
deviceList files opened on device number

lsof Command Samples:

List all open files

Terminal window
lsof

Display a list of all open files in the system.

List open files for a specific user

Terminal window
lsof -u username

Show all open files for a specific user by specifying their username.

Find processes using a specific file

Terminal window
lsof /path/to/file

Identify the processes that are currently using a specific file or directory.

Display open network connections

Terminal window
lsof -i

List all open network connections with associated IP addresses and ports.

List open files by process ID

Terminal window
lsof -p PID

Display all files opened by a specific process ID (PID).

Show open files for a specific process name

Terminal window
lsof -c process-name

List all files opened by processes with a specific name.

List all open files in a directory

Terminal window
lsof +D /path/to/directory

Display all open files within a specific directory.

lsof FAQ:

How do I use lsof in Linux?

To use the lsof command in Linux, execute the following command:

Terminal window
lsof --version

What is the purpose of lsof in Linux?

The lsof command in Linux is used to list open files and the processes that opened them.

Terminal window
lsof /path/to/file

How can I list all network connections with lsof?

You can list all network connections using lsof by running the following command:

Terminal window
lsof -i

How do I display processes listening on specific ports with lsof?

To display processes listening on specific ports, use the following command syntax with lsof:

Terminal window
lsof -i :port_number

How can I show all files opened by a specific user with lsof?

To show all files opened by a specific user, run the following lsof command:

Terminal window
lsof -u username

How can I find which process is using a specific file with lsof?

To find which process is using a specific file, execute the following command using lsof:

Terminal window
lsof /path/to/file

Applications of the lsof command

  • Viewing open files and sockets
  • Monitoring network connections
  • Troubleshooting processes that are holding onto files
  • Identifying which process is using a specific file or directory
  • Checking for deleted files that are still open by processes
  • Investigating memory usage by processes
  • Finding potential security vulnerabilities such as open ports or file leaks