When to use lsof
Use lsof to connect open files and sockets to processes. It helps diagnose listening ports, busy filesystems and space retained by deleted files; visibility depends on account privileges and system restrictions.
Syntax
lsof [OPTIONS] [FILE...]Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.
Common options and arguments
Usage examples
Find listening TCP ports
sudo lsof -nP -iTCP -sTCP:LISTENListening TCP sockets are shown with numeric ports and addresses.
Find out who opened the file
lsof ./app.logProcesses holding the named log file open are listed.
Find deleted but open files
sudo lsof +L1Open files whose links have been removed are listed, helping investigate retained disk space.
Notes and limitations
- Inspecting processes owned by other users may require elevated privileges.
- Scanning every open descriptor on a busy server can be expensive. Narrow the query by PID, file or network filter.
Common questions and troubleshooting
Why does deleting a log not immediately free its space?
A process may still have the file open. Coordinate a supported reopen or restart rather than killing an unidentified process.
Why are some processes absent from the result?
The account may lack permission to inspect them, or they changed during collection. An incomplete listing is not proof that no process holds a resource.
Further help
Options vary by version and distribution. Check the installed documentation with this command: man lsof.