The lsof
command is used in Unix/Linux systems to list open files and the processes that have them open. It provides detailed information about files, directories, network sockets, and other resources that are being accessed by processes. Here’s how you can use the lsof
command:
Open a terminal or command prompt.
Run the lsof
command followed by the desired options and filters.
Here are some common use cases for the lsof
command:
List all open files:
lsof
This command will list all open files and the processes that have them open.
List open files for a specific process ID (PID):
lsof -p {{PID}}
Replace {{PID}}
with the actual process ID for which you want to list open files. This command will display the open files associated with the specified process.
List open files for a specific user:
lsof -u {{username}}
Replace {{username}}
with the actual username for which you want to list open files. This command will display the open files associated with the specified user.
List open files for a specific file or directory:
lsof {{path/to/file}}
Replace {{path/to/file}}
with the actual path to the file or directory for which you want to list open files. This command will display the processes that have the specified file or directory open.
List open network connections:
lsof -i
This command will list open network connections, including TCP and UDP sockets.
List open files by a specific port number:
lsof -i :{{port_number}}
Replace {{port_number}}
with the actual port number for which you want to list open files. This command will display the processes that have files open on the specified port.
These are just a few examples of how you can use the lsof
command. The lsof
command offers many more options and filters to refine the output and gather specific information about open files and processes. You can refer to the lsof
command’s manual page (man lsof
) for more information and additional options.