lsof List open files

Version 4.99.4 · source

Start here

lsof -i :<port>
Who uses this port?
lsof -iTCP -sTCP:LISTEN -nP
List listeners
lsof -p <pid>
Files of a process
lsof -u <user>
Files of a user
lsof <file>
Who has it open?
lsof +D <dir>
Open files under dir
lsof -t -i :<port>
PIDs only
lsof +L1
Deleted, still open

Concepts

  1. Selectors -i -p -u -c names
  2. Matching files every open file
  3. Output table, PIDs, fields

Find by process

lsof -p <pid>
Open files of a process
lsof -p <pid1>,<pid2>
Several processes
lsof -p ^<pid>
Everything except a process
Negation applies first, even with -a.
lsof -c <prefix>
Processes by command-name prefix
lsof -c /<regex>/i
Command name by regex
lsof -c ^<prefix>
Exclude a command
lsof -g <pgid>
By process group ID
lsof -R
Add parent PID (PPID) column
lsof -d cwd -a -p <pid>
Working directory of a process
lsof -d txt -a -p <pid>
Executable and libraries

Find by user and file

lsof -u <user>
Files opened by a user
lsof -u ^<user>
Everyone except a user
lsof <file>
Processes holding a file open
lsof <mountpoint>
Processes using a filesystem
Use this before umount reports 'target is busy'.
lsof +d <dir>
Open files directly in a dir
lsof +D <dir>
Open files in a dir tree
Recursive and slow on big trees.
lsof -d <fds>
Select by descriptor (0-2, cwd, mem)
lsof -d ^mem,^txt -p <pid>
Skip libraries and binary
lsof -N
Only NFS files
lsof -a -u <user> -d ^mem
AND selectors together

Network

lsof -i
All network files
lsof -i :<port>
Who uses a port
lsof -i TCP:<port>
Restrict to TCP port
lsof -i :<p1>-<p2>
Port range
lsof -i :<p1>,<p2>
Port list
lsof -i @<host>
Connections to a host
lsof -i <4|6>
IPv4 or IPv6 only
lsof -iTCP -sTCP:LISTEN
Listening TCP sockets
lsof -iTCP -sTCP:ESTABLISHED
Established connections
lsof -iUDP
UDP sockets
lsof -i -s TCP:^LISTEN
Exclude a TCP state
lsof -nP -i
No DNS or port-name lookups
lsof -U
Unix domain sockets
lsof -a -U -c <cmd>
Unix sockets of one command
lsof -i -a -p <pid>
Network files of one process

Output control

lsof -t <selectors>
PIDs only, no header
Implies -w, so warnings are hidden.
lsof -l
Show UIDs instead of names
lsof -H
Human-readable sizes
lsof -o
Show file offset
lsof -s
Show file size
lsof -F pcn
Machine-readable fields
-F? lists the field letters; add -F0 for NUL-separated output.
lsof -w
Suppress warnings
lsof +c 0
Full command names
lsof -r <secs>
Repeat forever
lsof +r <secs>
Repeat until no files match
lsof +L1
Unlinked (deleted) but open files
lsof +L
Show link counts
lsof -V
Report selectors that matched nothing
lsof -v
Version info

Reading the table

Common FD values
FD Meaning
cwd Current directory
txt Program binary
mem Memory-mapped file or library
rtd Root directory
3u Descriptor 3, open read and write
4r / 5w Read-only / write-only
DEL Mapped file deleted
TYPE column
TYPE Meaning
REG Regular file
DIR Directory
CHR Character device
FIFO / PIPE Pipe
unix Unix domain socket
IPv4 / IPv6 Network socket

Recipes

Free a busy port
# find the PID, then stop itlsof -t -i :8080kill $(lsof -t -i :8080)
Show what is listening
sudo lsof -iTCP -sTCP:LISTEN -nP# only one command (nginx here)sudo lsof -nP -i -a -c nginx
Reclaim space of deleted files
sudo lsof +L1 | grep -i deleted# Linux only: empties the file under a live process, so be suresudo truncate -s 0 /proc/<pid>/fd/<fd>
Watch connections live
lsof -nP -i -a -p <pid> -r 2# unmount blocked?lsof /mnt/usb