Inspect a process file descriptors, sockets, and deleted files
Build a paste-ready command, then review its compatibility and effects before running it.
sudo lsof -nP -p <process-id> | head -<count>Complete required fields to copy the generated command.
cautionrequires privileges Compatibility
Linux + macOSVerified for Linux, macOS using bash, zsh syntax.
Operational knowledgereview due 2027-01-19
Requirementslsoflsof must be installed and available on PATH.Elevated accessReview the exact scope before using an elevated account.
Version supportlsof Current supported releasesVerified for linux, macos using bash, zsh syntax; consult compatibility notes for platform-specific differences.
Expected signalsCOMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMERepresentative successful output; values vary with the selected target and system state.
Known errorslsof: command not foundlsof is missing or is not available on PATH.Permission deniedThe current identity does not have the required access.
Verifysudo lsof -nP -p {{processId}} | head -{{count}}Re-run the corresponding read-only inspection and confirm the intended state.Rollback noteNo universal automatic rollback is available. Capture the current state and use the tool-specific recovery procedure before applying changes.
Command breakdown
01sudoPrivilege elevationRuns the following command with elevated privileges after authorization.
02lsofCommandRuns the lsof stage of this one-liner.
03-nPOptionConfigures lsof with the -nP option.
04-pOptionSelects the command-specific port, process, or print mode.
05<process-id>ParameterA value supplied in the Fill parameters section.
06|PipelinePasses the output on the left to the command on the right.
07headCommandRuns the head stage of this one-liner.
08-<count>OptionConfigures head with the -<count> option.
This advanced community-curated one-liner combines several stages. Review every option and placeholder before running it.
Example input
sudo lsof -nP -p 8421 | head -8
Example output
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 8421 app cwd DIR 253,0 4096 183 /srv/api
node 8421 app 20u IPv4 921881 0t0 TCP 127.0.0.1:8080 (LISTEN)
node 8421 app 27w REG 253,0 8421184 9912 /var/log/api.log
Illustrative output — exact values vary by system and data.
Official sources