Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 :80 tells 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; lsof names the culprit.
  • Recover deleted space: a deleted file still occupies disk until the last process closes it — lsof finds the holder.
  • See network activity: sockets are files, so lsof -i is a live picture of who is connected.

lsof is 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
  • -i filters to internet sockets.
  • :80 matches port 80 specifically.
  • This is the fastest replacement for the old netstat -tunlp habit.

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 before umount.

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 deleted is the classic trick for reclaiming disk held by a deleted file.
  • -P shows port numbers instead of service names; -n skips DNS lookups (faster).

Common Pitfalls

  • No output ≠ nothing open: lsof needs privileges to see other users’ processes; run with sudo for a complete picture.
  • +D is slow: it walks an entire directory tree; avoid it on huge filesystems.
  • Port vs. service name: lsof -i :80 matches the port; lsof -i 80 may match nothing — the colon matters.
  • Deleted files need the holder found first: lsof | grep deleted shows the file, but you must kill the process before the space is freed.

PageWhy It Matters
pslsof -p uses PIDs from ps.
killStop the process that is holding the file or port.
topThe process must be running to appear in lsof.
straceFor deeper inspection of what a process is doing.

lsof Cheatsheet

CommandWhat It Does
lsofList every open file.
lsof -i :80Show what is using port 80.
lsof -u userFiles opened by a user.
lsof -c cmdFiles opened by a command.
lsof -p pidFiles opened by one process.
lsof +D /pathRecursively check a directory.
lsof | grep deletedFind deleted files still holding disk.