Open files in Linux: the lsof command
Updated November 1, 2026
lsof (list open files) shows which files and sockets are open and by which process.
Syntax
lsof [options]Common cases
lsof -p 1234 # what process 1234 has open
lsof /var/log/syslog # who holds this file
sudo lsof -i :80 # who took port 80
sudo lsof -i # all network connections
lsof +D /home/user # all open files under a directorySample output
$ python3 -m http.server 8080 >/dev/null 2>&1 &
[1] 20
$ lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 20 student 3u IPv4 22944830 0t0 TCP *:http-alt (LISTEN)
$ exec 4</tmp/big.bin; rm /tmp/big.bin
$ lsof -a -d 4 -p $$
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 12 student 4r REG 0,158 209715200 2153317 /tmp/big.bin (deleted)How it works: in Linux almost everything is a file
In Linux a regular file, a socket, a pipe and a device are all 'open files' to a process. So
lsofanswers many questions at once: who took a network port (-i :port), why a disk will not unmount (which process holds a file on it), where space went (a deleted but still-open file frees no space - it shows up inlsof). The same data lives in/proc/<PID>/fd, andlsofjust gathers it into a readable form.
Common mistakes
Without privileges lsof stays silent instead of complaining. Port 80 is held by a root process: as root, lsof -i :80 prints python3 2623 root 3u IPv4 22914832 0t0 TCP *:http (LISTEN), while the same command run as an ordinary user prints nothing at all and exits with code 1. An empty answer means "you cannot see it", not "the port is free".
lsof /path answers "who holds this file open right now", not "who has read it". lsof /etc/passwd gives zero lines even though every login reads that file. And lsof /tmp is not the files inside /tmp: it lists processes whose current directory is /tmp, marked cwd in the FD column. For the contents of a directory use lsof +D /tmp.
On a bad argument lsof dumps its full usage text, which reads like a complaint about your flags. lsof /etc/nosuchfile starts with lsof: status error on /etc/nosuchfile: No such file or directory and then prints ten lines of usage - the real cause is in that first line, the rest is noise. lsof -p bash behaves the same way: lsof: illegal process ID: bash on top, because -p takes a number only.
In the book
This topic is covered in full in the chapter Monitoring and bottlenecks.