Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Processes and Signals

Linux lsof command

Reference for the Linux lsof command: list files opened by processes, find who uses a port or file, show deleted-but-open files and read the output columns (read-only).

lsof stands for "list open files". On Linux nearly everything is a file: regular files, directories, pipes, sockets, devices. So lsof can tell you which process has a file open, which one listens on a port, which files a program has open right now, and why a disk will not unmount.

On our live runner lsof is not installed, so the examples that call it are read-only. The same facts can be read from /proc/PID/fd, which the runnable example shows.

Terminal
lsof [OPTION]... [FILE...]

Try it

Read-only (needs the lsof package):

Terminal
lsof /var/log/syslog          # who has this file open?
lsof -i :8080                 # who uses port 8080?
lsof -p 1234                  # everything process 1234 has open
lsof -u alice                 # everything user alice has open
lsof +D /srv/data             # open files under a directory
lsof +L1                      # files deleted but still open (link count < 1)
Bash The idea behind lsof: look in /proc/PID/fd

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash A deleted file that is still open

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
lsof [-aUnPR] [-i [ADDRESS]] [-p PIDS] [-u USERS] [-c NAME] [-d FDS] [+D DIR | +d DIR] [FILE...]
Option Description
FILE Show processes that have this file or directory open.
-i Internet sockets. -i :80, -i TCP, -i @host, -i TCP:22, -i 4 and -i 6 filter by port, protocol, host, IP version.
-p PID Only these process IDs (comma-separated); ^PID excludes.
-u USER Only this user (name or UID); ^user excludes.
-c NAME Processes whose command starts with NAME.
-d FD Only these descriptors, such as cwd, txt, 3, 0-2.
+D DIR Recursively list open files below DIR (slow on big trees).
+d DIR Only files directly in DIR.
+L1 Files with a link count below 1, i.e. deleted but open.
-n, -P Do not resolve host names or port names (much faster, numeric output).
-t Terse: print only process IDs, ideal for kill $(lsof -t ...).
-a AND the selectors together; by default they are OR-ed.
-r N Repeat every N seconds.

Output columns: COMMAND, PID, USER, FD (descriptor: cwd, txt, mem, 3u = descriptor 3 opened for read and write), TYPE (REG, DIR, IPv4, FIFO, ...), DEVICE, SIZE/OFF, NODE and NAME.

Returns: 0 if something was listed; 1 if nothing matched or on error.

Usage notes

  • Options are OR-ed by default. lsof -u alice -i lists alice's files plus all network files. Add -a to get alice's network files only.
  • Permissions. You see only your own processes unless you are root; others' entries are skipped silently, which can make lsof -i :80 look empty when a root-owned server holds the port.
  • Port in use? lsof -nP -iTCP:8080 -sTCP:LISTEN is the usual form. ss -ltnp is a lighter alternative.
  • Why can't I unmount? lsof +f -- /mnt/usb shows who is using the mount point; fuser -vm /mnt/usb is similar.
  • Disk full after deleting a log? A running program still holds the deleted file; lsof +L1 finds it, and restarting the program frees the space.
  • Speed. Without -n -P, lsof does DNS and service lookups for sockets and can take seconds.
  • No lsof? Read /proc/PID/fd, /proc/PID/cwd and /proc/net/tcp, or use fuser.