lsof Command Guide for Listing Open Files
lsof lists every file that processes have open — including network sockets, which no other command shows you so directly.
What is lsof, and Why Use It?
In Linux, everything is a file: regular files, directories, sockets, pipes, and devices. lsof (list open files) prints a table of which process has which of these open right now. It is the single fastest way to answer questions like “what is holding this port?”, “which file did this program delete but never let go of?”, and “which process is using this disk?”
Why Learn lsof?
- Find what is using a port:
lsof -i :80tells you which process is listening — and whether it is the one you think it is. - Debug “device busy” errors: an unmount or a delete fails because some process still has the file open;
lsofnames the culprit. - Recover deleted space: a deleted file still occupies disk until the last process closes it —
lsoffinds the holder. - See network activity: sockets are files, so
lsof -iis a live picture of who is connected.
lsofis the closest thing Linux has to an X-ray of the machine: point it at a file, a port, or a user, and it tells you exactly who is touching what.
1. Basic Usage: See What Is Open
# Every file every process has open (a LOT of output)
lsof
# Just your own files
lsof -u $USER
With no arguments lsof lists every open file on the system — often thousands of lines. Filter it down to something useful.
2. Find What Is Using a Port
# What is listening on port 80?
lsof -i :80
# What is connected to port 443?
lsof -i :443
# All network connections
lsof -i
-ifilters to internet sockets.:80matches port 80 specifically.- This is the fastest replacement for the old
netstat -tunlphabit.
3. Find What Is Holding a File or Directory
# Which process has this file open?
lsof /var/log/syslog
# Which files are open under a directory?
lsof +D /mnt/data
# Why can't I unmount this?
lsof +D /mnt/old-drive
lsof <file>matches by exact path.lsof +D <dir>recursively checks everything under a directory — useful beforeumount.
4. Filter by User, Command, or PID
# All files opened by a specific user
lsof -u www-data
# Files opened by a specific command
lsof -c nginx
# Files opened by one process
lsof -p 1234
# Combine filters
lsof -u www-data -c nginx
-u user— by username or UID.-c command— by command name (prefix match).-p pid— by process ID.
5. Common Patterns
# List the processes holding a deleted-but-still-open file
lsof | grep deleted
# See which files a process has open, then kill it
lsof -c myapp
kill <pid>
# Which sockets are in LISTEN state?
lsof -i -P -n | grep LISTEN
grep deletedis the classic trick for reclaiming disk held by a deleted file.-Pshows port numbers instead of service names;-nskips DNS lookups (faster).
Common Pitfalls
- No output ≠ nothing open:
lsofneeds privileges to see other users’ processes; run withsudofor a complete picture. +Dis slow: it walks an entire directory tree; avoid it on huge filesystems.- Port vs. service name:
lsof -i :80matches the port;lsof -i 80may match nothing — the colon matters. - Deleted files need the holder found first:
lsof | grep deletedshows the file, but you must kill the process before the space is freed.
Related Pages
| Page | Why It Matters |
|---|---|
ps | lsof -p uses PIDs from ps. |
kill | Stop the process that is holding the file or port. |
top | The process must be running to appear in lsof. |
strace | For deeper inspection of what a process is doing. |
lsof Cheatsheet
| Command | What It Does |
|---|---|
lsof | List every open file. |
lsof -i :80 | Show what is using port 80. |
lsof -u user | Files opened by a user. |
lsof -c cmd | Files opened by a command. |
lsof -p pid | Files opened by one process. |
lsof +D /path | Recursively check a directory. |
lsof | grep deleted | Find deleted files still holding disk. |