lsof Command Cheatsheet
lsof, which stands for "list open files," is a powerful command-line utility for listing all open files and the processes that are using them. In Linux, "everything is a file," so `lsof` can identify processes using regular files, directories, network sockets, and more.
Common Usage
| Command | Description |
|---|
lsof | Lists all open files on the system. This can be a very long list. |
lsof [FILE_PATH] | Lists processes that have a specific file open. This is useful for troubleshooting "file in use" errors. |
lsof -i | Lists all network connections (sockets). |
lsof -i :[PORT] | Lists processes using a specific network port. For example, `lsof -i :80`. |
lsof -c [PROCESS_NAME] | Lists files opened by a specific process name. For example, `lsof -c nginx`. |
lsof -u [USER_NAME] | Lists files opened by a specific user. For example, `lsof -u root`. |
Key `lsof` Flags
| Flag | Description |
|---|
-i | Lists network files. You can combine it with protocols (-i tcp) or ports (-i :80). |
-c | Selects processes by command name. |
-u | Selects processes by user ID or name. |
-p | Selects processes by Process ID (PID). For example, `lsof -p 12345`. |
-n | Prevents the conversion of IP addresses to hostnames. Speeds up the command. |
-P | Prevents the conversion of port numbers to service names. Speeds up the command. |