Skip to main content

lsof Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for finding open files, processes, ports, and deleted files with lsof in Linux

The `lsof` command lists open files and the processes using them in Linux. This cheatsheet covers common filters for ports, users, PIDs, directories, deleted files, and practical troubleshooting patterns.

Basic Syntax

Core lsof command forms.

CommandDescription
lsofList open files visible to the current user
sudo lsofList open files system-wide
lsof /path/to/fileShow which process has a file open
lsof -p 1234Show files opened by one PID
lsof -u usernameShow files opened by one user

Port and Network Checks

Find listeners and active network connections.

CommandDescription
sudo lsof -nP -iTCP:80 -sTCP:LISTENFind what is listening on TCP port 80
sudo lsof -iList all network connections
sudo lsof -i TCPList TCP connections
sudo lsof -i UDPList UDP connections
sudo lsof -nP -iTCP:443 -sTCP:LISTENFilter to listeners on TCP port 443

Process and User Filters

Filter open files by process, command name, or user.

CommandDescription
sudo lsof -p 1234Files opened by PID 1234
sudo lsof -p ^1234Exclude PID 1234
sudo lsof -c nginxFiles opened by commands starting with nginx
lsof -u johnFiles opened by user john
sudo lsof -u ^johnExclude files opened by user john

Files and Directories

Check who is using a file or directory tree.

CommandDescription
lsof /var/log/nginx/access.logShow processes using one file
sudo lsof +d /var/logShow open files in one directory level
sudo lsof +D /var/logShow open files in a directory recursively
sudo lsof +D /mountpointFind what is blocking an unmount
sudo lsof /dev/sdaCheck processes using a device file

Deleted Files and Disk Space

Find deleted files that still consume disk space.

CommandDescription
sudo lsof +L1List deleted files still held open
sudo lsof +L1 /varLimit deleted-file search to one path
sudo lsof -a +L1 -u nginxDeleted files still open for one user
sudo lsof +L1 | sort -k7 -nSort deleted-file output by size/off column
sudo lsof +L1 | grep deletedQuick filter for deleted entries

Scripting and Combined Filters

Use lsof in scripts and tighter searches.

CommandDescription
sudo lsof -t -iTCP:8080 -sTCP:LISTENOutput only the PID listening on port 8080
sudo lsof -a -u john -i TCPShow only TCP connections owned by john
sudo lsof -a -p 1234 -d cwdShow only the current working directory for one PID
sudo lsof -Fn -p 1234Machine-readable output for scripts
kill $(sudo lsof -t -iTCP:8080 -sTCP:LISTEN)Stop the process listening on port 8080

Troubleshooting

Quick checks for common lsof usage issues.

IssueCheck
Output looks incompleteRun with sudo to see files opened by other users’ processes
Port lookup shows service names instead of numbersAdd -nP for numeric addresses and ports
Need listeners only, not all connectionsAdd -sTCP:LISTEN with -iTCP:PORT
Recursive directory search is slowPrefer +d for top-level only, or narrow the path
Disk space is still not freed after deleteUse lsof +L1 and restart the process holding the file

Use these guides for the full walkthroughs.

GuideDescription
lsof Command in LinuxFull lsof guide with examples
ss Command in LinuxInspect sockets and listening services
How to Check Listening Ports in LinuxCompare ss, netstat, and lsof
Find Large Files in LinuxTrack disk usage and deleted files
ps Command in LinuxInspect processes and related details