ZetCode

Linux lsof Command

last modified October 24, 2026

The lsof command lists open files. On Linux, almost everything a process uses is a file: regular files and directories, shared libraries, pipes, terminals, and network sockets. lsof answers questions such as which process holds a file open, which program listens on a port, why a disk stays full after a large log was deleted, or why a file system cannot be unmounted.

The examples were run with lsof 4.95 on Debian 12, in a container booted with systemd. Besides the system services (nginx with two worker processes, sshd, journald), the user jano runs a Python web server on port 8000, a small program that writes to a log file, and an nc server on port 9000 with one connected client.

The log writer is the following script; it appends a line to app.log every second and keeps the file open.

applog.py
#!/usr/bin/env python3
# appends a line to app.log every second
import time

with open('app.log', 'a') as f:
    while True:
        f.write(time.strftime('%H:%M:%S') + ' request served\n')
        f.flush()
        time.sleep(1)

Listing open files

Without arguments, lsof lists the open files of all processes.

lsof | head -n 10
lsof | wc -l
sudo lsof | wc -l

A regular user can inspect only their own processes. For processes of other users, here root's systemd, journald, and cron, lsof prints a line with Permission denied instead of the files. With sudo, the listing is complete: 502 open files instead of 180 (both counts include the header line). Most examples in this article that look at system services therefore use sudo.

$ lsof | head -n 10
COMMAND   PID       USER   FD      TYPE DEVICE SIZE/OFF     NODE NAME
systemd     1       root  cwd   unknown                          /proc/1/cwd (readlink: Permission denied)
systemd     1       root  rtd   unknown                          /proc/1/root (readlink: Permission denied)
systemd     1       root  txt   unknown                          /proc/1/exe (readlink: Permission denied)
systemd     1       root NOFD                                    /proc/1/fd (opendir: Permission denied)
systemd-j  36       root  cwd   unknown                          /proc/36/cwd (readlink: Permission denied)
systemd-j  36       root  rtd   unknown                          /proc/36/root (readlink: Permission denied)
systemd-j  36       root  txt   unknown                          /proc/36/exe (readlink: Permission denied)
systemd-j  36       root NOFD                                    /proc/36/fd (opendir: Permission denied)
cron       54       root  cwd   unknown                          /proc/54/cwd (readlink: Permission denied)
$ lsof | wc -l
181
$ sudo lsof | wc -l
503

Files of a process

The -p option selects a process by its PID. Here we look at the log writer.

pgrep -a python3
pid=$(pgrep -f applog.py)
lsof -p $pid

The columns are: COMMAND (the first nine characters of the process name), PID, USER, FD (file descriptor), TYPE (REG regular file, DIR directory, CHR character device, FIFO pipe, IPv4/IPv6 network socket, unix Unix socket), DEVICE, SIZE/OFF (file size or offset), NODE (inode number), and NAME.

The FD column is the most interesting one. cwd is the current working directory, rtd the root directory, txt the program's executable, and mem a memory-mapped file such as a shared library. Numbers are real file descriptors, followed by the access mode: r read, w write, u both. Descriptors 0, 1, and 2 are standard input, output, and error, here all redirected to /dev/null. Descriptor 3 is the log file opened for writing.

$ pgrep -a python3
106 python3 -m http.server 8000
107 python3 applog.py
$ pid=$(pgrep -f applog.py)
$ lsof -p $pid
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
python3 107 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
python3 107 jano  rtd    DIR   0,48     4096 2538886 /
python3 107 jano  txt    REG   0,48    18248 2263368 /usr/local/bin/python3.14
python3 107 jano  mem    REG   0,48  3048928 2641271 /usr/lib/locale/locale-archive
python3 107 jano  mem    REG   0,48   911904 2641606 /usr/lib/x86_64-linux-gnu/libm.so.6
python3 107 jano  mem    REG   0,48  1926232 2641585 /usr/lib/x86_64-linux-gnu/libc.so.6
python3 107 jano  mem    REG   0,48    27028 2641563 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
python3 107 jano  mem    REG   0,48 31059608 2263661 /usr/local/lib/libpython3.14.so.1.0
python3 107 jano  mem    REG   0,48   215000 2641574 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
python3 107 jano    0r   CHR    1,3      0t0     138 /dev/null
python3 107 jano    1w   CHR    1,3      0t0     138 /dev/null
python3 107 jano    2w   CHR    1,3      0t0     138 /dev/null
python3 107 jano    3w   REG   0,48 37688967 2539022 /home/jano/work/app.log

Which process has a file open

Given a file name, lsof lists the processes that have the file open.

lsof app.log
lsof /var/log/nginx/access.log
sudo lsof /var/log/nginx/access.log

The second command prints nothing, because only root's and www-data's processes write the nginx log, and jano cannot see their files. Empty output from lsof without sudo does not mean that nobody uses a file. With sudo, we see the nginx master process and its two workers, which all share the descriptor 4.

$ lsof app.log
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
python3 107 jano    3w   REG   0,48 37688967 2539022 app.log
$ lsof /var/log/nginx/access.log
$ sudo lsof /var/log/nginx/access.log
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
nginx   102     root    4w   REG   0,48        0 2637040 /var/log/nginx/access.log
nginx   103 www-data    4w   REG   0,48        0 2637040 /var/log/nginx/access.log
nginx   104 www-data    4w   REG   0,48        0 2637040 /var/log/nginx/access.log

Files under a directory

The +D option lists open files anywhere below a directory; +d looks only at the directory itself and its immediate entries.

lsof +D /home/jano/work
sudo lsof +D /var/log

Every process started in the work directory has it as its cwd, including the shell and the two lsof processes of the command itself (lsof forks a helper). Under /var/log, journald has its journal files both mapped in memory and open, and nginx writes its access and error logs. +D walks the whole tree, so it can be slow on large directories.

$ lsof +D /home/jano/work
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash     82 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
python3 106 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
python3 107 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
python3 107 jano    3w   REG   0,48 37688967 2539022 /home/jano/work/app.log
nc      108 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
sleep   109 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
nc      110 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
lsof    145 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
lsof    146 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
$ sudo lsof +D /var/log
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
systemd-j  36     root  mem    REG   0,48  8388608 2538925 /var/log/journal/a799110e38ff4b39a0c43af084d6ed01/system.journal
systemd-j  36     root  mem    REG   0,48  8388608 2538967 /var/log/journal/a799110e38ff4b39a0c43af084d6ed01/user-1000.journal
systemd-j  36     root   20u   REG   0,48  8388608 2538925 /var/log/journal/a799110e38ff4b39a0c43af084d6ed01/system.journal
systemd-j  36     root   21u   REG   0,48  8388608 2538967 /var/log/journal/a799110e38ff4b39a0c43af084d6ed01/user-1000.journal
nginx     102     root    2w   REG   0,48        0 2637042 /var/log/nginx/error.log
nginx     102     root    3w   REG   0,48        0 2637042 /var/log/nginx/error.log
nginx     102     root    4w   REG   0,48        0 2637040 /var/log/nginx/access.log
nginx     103 www-data    2w   REG   0,48        0 2637042 /var/log/nginx/error.log
nginx     103 www-data    3w   REG   0,48        0 2637042 /var/log/nginx/error.log
nginx     103 www-data    4w   REG   0,48        0 2637040 /var/log/nginx/access.log
nginx     104 www-data    2w   REG   0,48        0 2637042 /var/log/nginx/error.log
nginx     104 www-data    3w   REG   0,48        0 2637042 /var/log/nginx/error.log
nginx     104 www-data    4w   REG   0,48        0 2637040 /var/log/nginx/access.log

Selecting by command and user

The -c option selects processes whose name starts with the given string, and -u selects processes of a user. A leading ^ negates the selection.

sudo lsof -c nginx | wc -l
sudo lsof -c ngi -c ssh | awk '{ print $1, $2, $3 }' | uniq
sudo lsof -u www-data | wc -l
sudo lsof -u ^root | awk 'NR > 1 { print $3 }' | sort | uniq -c

The three nginx processes have 69 open files. Repeated -c options add more names, and a prefix is enough: ngi matches nginx. The last command counts open files of all users except root.

$ sudo lsof -c nginx | wc -l
70
$ sudo lsof -c ngi -c ssh | awk '{ print $1, $2, $3 }' | uniq
COMMAND PID USER
sshd 65 root
nginx 102 root
nginx 103 www-data
nginx 104 www-data
$ sudo lsof -u www-data | wc -l
47
$ sudo lsof -u ^root | awk 'NR > 1 { print $3 }' | sort | uniq -c
    124 jano
     30 messagebus
     46 www-data

Combining options with -a

By default, lsof joins different selection options with OR. To require all conditions, add -a.

lsof -c nc -i :8000 | head -n 4
lsof -a -c nc -i :8000
lsof -a -c nc -i

The first command lists files that belong to nc processes or use port 8000, so the Python server shows up along with all files of both nc processes. With -a, the selection is "nc processes using port 8000", which is empty. The last command shows only the network files of nc: the listening socket, and both ends of the connection between the server and the client. Forgetting -a is the most common lsof mistake.

$ lsof -c nc -i :8000 | head -n 4
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
python3 106 jano    3u  IPv4  79920      0t0     TCP *:8000 (LISTEN)
nc      108 jano  cwd    DIR   0,48     4096 2538948 /home/jano/work
nc      108 jano  rtd    DIR   0,48     4096 2538886 /
$ lsof -a -c nc -i :8000
$ lsof -a -c nc -i
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      108 jano    3u  IPv4  80942      0t0  TCP *:9000 (LISTEN)
nc      108 jano    4u  IPv4  80943      0t0  TCP localhost:9000->localhost:56838 (ESTABLISHED)
nc      110 jano    3u  IPv4  77489      0t0  TCP localhost:56838->localhost:9000 (ESTABLISHED)

Network connections

The -i option selects internet sockets.

lsof -i
lsof -i -n -P
sudo lsof -i -n -P

By default, lsof translates addresses to host names and port numbers to service names, which can be slow when DNS does not respond. -n keeps numeric addresses and -P numeric ports; use both in scripts. As root, we also see sshd on port 22 and nginx on port 80, each listening on IPv4 and IPv6.

$ lsof -i
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 106 jano    3u  IPv4  79920      0t0  TCP *:8000 (LISTEN)
nc      108 jano    3u  IPv4  80942      0t0  TCP *:9000 (LISTEN)
nc      108 jano    4u  IPv4  80943      0t0  TCP localhost:9000->localhost:56838 (ESTABLISHED)
nc      110 jano    3u  IPv4  77489      0t0  TCP localhost:56838->localhost:9000 (ESTABLISHED)
$ lsof -i -n -P
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 106 jano    3u  IPv4  79920      0t0  TCP *:8000 (LISTEN)
nc      108 jano    3u  IPv4  80942      0t0  TCP *:9000 (LISTEN)
nc      108 jano    4u  IPv4  80943      0t0  TCP 127.0.0.1:9000->127.0.0.1:56838 (ESTABLISHED)
nc      110 jano    3u  IPv4  77489      0t0  TCP 127.0.0.1:56838->127.0.0.1:9000 (ESTABLISHED)
$ sudo lsof -i -n -P
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     65     root    3u  IPv4  79055      0t0  TCP *:22 (LISTEN)
sshd     65     root    4u  IPv6  79057      0t0  TCP *:22 (LISTEN)
nginx   102     root    5u  IPv4  78181      0t0  TCP *:80 (LISTEN)
nginx   102     root    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
nginx   103 www-data    5u  IPv4  78181      0t0  TCP *:80 (LISTEN)
nginx   103 www-data    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
nginx   104 www-data    5u  IPv4  78181      0t0  TCP *:80 (LISTEN)
nginx   104 www-data    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
python3 106     jano    3u  IPv4  79920      0t0  TCP *:8000 (LISTEN)
nc      108     jano    3u  IPv4  80942      0t0  TCP *:9000 (LISTEN)
nc      108     jano    4u  IPv4  80943      0t0  TCP 127.0.0.1:9000->127.0.0.1:56838 (ESTABLISHED)
nc      110     jano    3u  IPv4  77489      0t0  TCP 127.0.0.1:56838->127.0.0.1:9000 (ESTABLISHED)

Filtering network files

The argument of -i has the form [46][protocol][@host][:port]; every part is optional. TCP states are selected with -s.

sudo lsof -i :80
sudo lsof -i TCP:22
lsof -i @127.0.0.1 -n -P
sudo lsof -i TCP -sTCP:LISTEN -P -n
sudo lsof -i 6 -P -n

The first two commands answer the common question "what is using this port?"; without -P, the ports are shown as http and ssh. @127.0.0.1 selects connections to or from the loopback address. -sTCP:LISTEN lists only listening sockets, which gives a list of the services on the machine. -i 6 selects IPv6 sockets. The ss command shows the same information faster; see the ss article.

$ sudo lsof -i :80
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   102     root    5u  IPv4  78181      0t0  TCP *:http (LISTEN)
nginx   102     root    6u  IPv6  78182      0t0  TCP *:http (LISTEN)
nginx   103 www-data    5u  IPv4  78181      0t0  TCP *:http (LISTEN)
nginx   103 www-data    6u  IPv6  78182      0t0  TCP *:http (LISTEN)
nginx   104 www-data    5u  IPv4  78181      0t0  TCP *:http (LISTEN)
nginx   104 www-data    6u  IPv6  78182      0t0  TCP *:http (LISTEN)
$ sudo lsof -i TCP:22
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     65 root    3u  IPv4  79055      0t0  TCP *:ssh (LISTEN)
sshd     65 root    4u  IPv6  79057      0t0  TCP *:ssh (LISTEN)
$ lsof -i @127.0.0.1 -n -P
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      108 jano    4u  IPv4  80943      0t0  TCP 127.0.0.1:9000->127.0.0.1:56838 (ESTABLISHED)
nc      110 jano    3u  IPv4  77489      0t0  TCP 127.0.0.1:56838->127.0.0.1:9000 (ESTABLISHED)
$ sudo lsof -i TCP -sTCP:LISTEN -P -n
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     65     root    3u  IPv4  79055      0t0  TCP *:22 (LISTEN)
sshd     65     root    4u  IPv6  79057      0t0  TCP *:22 (LISTEN)
nginx   102     root    5u  IPv4  78181      0t0  TCP *:80 (LISTEN)
nginx   102     root    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
nginx   103 www-data    5u  IPv4  78181      0t0  TCP *:80 (LISTEN)
nginx   103 www-data    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
nginx   104 www-data    5u  IPv4  78181      0t0  TCP *:80 (LISTEN)
nginx   104 www-data    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
python3 106     jano    3u  IPv4  79920      0t0  TCP *:8000 (LISTEN)
nc      108     jano    3u  IPv4  80942      0t0  TCP *:9000 (LISTEN)
$ sudo lsof -i 6 -P -n
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     65     root    4u  IPv6  79057      0t0  TCP *:22 (LISTEN)
nginx   102     root    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
nginx   103 www-data    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)
nginx   104 www-data    6u  IPv6  78182      0t0  TCP *:80 (LISTEN)

PIDs only

The -t option prints only process IDs, one per line, which is handy as input for other commands.

lsof -t -i :8000
sudo lsof -t -c nginx
kill $(lsof -t -i :8000)
lsof -i :8000; echo $?

kill $(lsof -t -i :8000) stops the program that blocks a port, a common step before restarting a development server. lsof returns 1 when it finds nothing. If several processes share the port, such as nginx workers, all of them receive the signal; stop a service with its service manager instead.

$ lsof -t -i :8000
106
$ sudo lsof -t -c nginx
102
103
104
$ kill $(lsof -t -i :8000)
$ lsof -i :8000; echo $?
1

Deleted files that still use space

Deleting a file removes its name. The data stays on the disk until the last process closes the file. A typical case: a disk is full, someone deletes a large log file, and the free space does not change.

ls -lh app.log
rm app.log
lsof +L1
pid=$(pgrep -f applog.py)
ls -l /proc/$pid/fd

The +L1 option lists open files with a link count below 1, which means files that no longer have a name. The log writer still holds the 36 MB file and keeps writing to it. The descriptors of a process are also visible as symbolic links in /proc/PID/fd, and the deleted file is marked there, too. Restarting the program frees the space, as does the method in the next section.

$ ls -lh app.log
-rw-r--r-- 1 jano jano 36M Oct 24 21:11 app.log
$ rm app.log
$ lsof +L1
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NLINK    NODE NAME
python3 107 jano    3w   REG   0,48 37688967     0 2539022 /home/jano/work/app.log (deleted)
$ pid=$(pgrep -f applog.py)
$ ls -l /proc/$pid/fd
total 0
lr-x------ 1 jano jano 64 Oct 24 21:11 0 -> /dev/null
l-wx------ 1 jano jano 64 Oct 24 21:11 1 -> /dev/null
l-wx------ 1 jano jano 64 Oct 24 21:11 2 -> /dev/null
l-wx------ 1 jano jano 64 Oct 24 21:11 3 -> /home/jano/work/app.log (deleted)

Recovering and truncating a deleted file

While a process holds a deleted file open, its content can be read through /proc/PID/fd/N, and the file can be emptied without restarting the process.

cp /proc/$pid/fd/3 recovered.log
ls -lh recovered.log
tail -n 2 recovered.log
: > /proc/$pid/fd/3
lsof +L1

cp copies the data into a new file, so an accidentally deleted file can be rescued as long as some process has it open. : > file truncates the file to zero bytes; the size in the lsof +L1 output drops to 0 and the disk space is released, while the program keeps running. This works well for log files opened in append mode.

$ cp /proc/$pid/fd/3 recovered.log
$ ls -lh recovered.log
-rw-r--r-- 1 jano jano 36M Oct 24 21:11 recovered.log
$ tail -n 2 recovered.log
21:11:49 request served
21:11:50 request served
$ : > /proc/$pid/fd/3
$ lsof +L1
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NLINK    NODE NAME
python3 107 jano    3w   REG   0,48        0     0 2539022 /home/jano/work/app.log (deleted)

Counting open files

Each process has a limit on the number of open file descriptors. Programs that leak descriptors eventually fail with Too many open files.

sudo lsof -p 1 | wc -l
sudo ls /proc/1/fd | wc -l
prlimit -n -p $pid

lsof counts more than descriptors: it also lists the cwd, rtd, txt, and mem entries and a header. To compare with the limit, count the entries in /proc/PID/fd; systemd has 44 descriptors open. prlimit shows the soft and hard limits of a running process. The soft limit of 1024 is the one that applies; a process may raise it up to the hard limit.

$ sudo lsof -p 1 | wc -l
73
$ sudo ls /proc/1/fd | wc -l
44
$ prlimit -n -p $pid
RESOURCE DESCRIPTION              SOFT   HARD UNITS
NOFILE   max number of open files 1024 524288 files

The fuser command

The fuser command from the psmisc package answers the question "who uses this file or port" in a shorter form, and can kill the processes it finds.

fuser -v /home/jano/work
fuser -v -n tcp 9000
sudo fuser -v 80/tcp
fuser -k 9000/tcp
lsof -i :9000; echo $?

In the ACCESS column, c stands for current directory and F for an open file. -n tcp 9000 and 9000/tcp are equivalent. -k sends SIGKILL to the processes; it killed the nc server. The client was not using port 9000 locally, so it survived, and lsof shows its socket in the CLOSE_WAIT state: the server closed the connection, but the client has not closed its end yet.

$ fuser -v /home/jano/work
                     USER        PID ACCESS COMMAND
/home/jano/work:     jano         82 ..c.. bash
                     jano        107 ..c.. python3
                     jano        108 ..c.. nc
                     jano        109 ..c.. sleep
                     jano        110 ..c.. nc
$ fuser -v -n tcp 9000
                     USER        PID ACCESS COMMAND
9000/tcp:            jano        108 F.... nc
$ sudo fuser -v 80/tcp
                     USER        PID ACCESS COMMAND
80/tcp:              root        102 F.... nginx
                     www-data    103 F.... nginx
                     www-data    104 F.... nginx
$ fuser -k 9000/tcp
9000/tcp:              108
$ lsof -i :9000; echo $?
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nc      110 jano    3u  IPv4  77489      0t0  TCP localhost:56838->localhost:9000 (CLOSE_WAIT)
0

Unix sockets

The -U option selects Unix domain sockets, which local services use to talk to each other.

sudo lsof -U -a -c systemd-j

journald listens on the sockets in /run/systemd/journal: dev-log receives syslog messages, socket native journal messages, and stdout the output of services. Each connected service holds one of the stdout connections. Note the -a: without it, the command would list all Unix sockets plus all files of journald.

$ sudo lsof -U -a -c systemd-j
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF  NODE NAME
systemd-j  36 root    3u  unix 0xffff8991f6664000      0t0 71547 /run/systemd/journal/dev-log type=DGRAM (CONNECTED)
systemd-j  36 root    5u  unix 0xffff8991f6663000      0t0 71549 /run/systemd/journal/socket type=DGRAM (CONNECTED)
systemd-j  36 root    6u  unix 0xffff8991f6662000      0t0 71551 /run/systemd/journal/stdout type=STREAM (LISTEN)
systemd-j  36 root   10u  unix 0xffff8991923fc400      0t0 76474 /run/systemd/journal/io.systemd.journal type=STREAM (LISTEN)
systemd-j  36 root   15u  unix 0xffff8991923f8000      0t0 76476 type=DGRAM (CONNECTED)
systemd-j  36 root   17u  unix 0xffff898fa4f83000      0t0 77369 /run/systemd/journal/stdout type=STREAM (CONNECTED)
systemd-j  36 root   18u  unix 0xffff8991923fe000      0t0 77370 /run/systemd/journal/stdout type=STREAM (CONNECTED)
systemd-j  36 root   19u  unix 0xffff8991965d1800      0t0 73683 /run/systemd/journal/stdout type=STREAM (CONNECTED)
systemd-j  36 root   23u  unix 0xffff8990ce100800      0t0 73680 /run/systemd/journal/stdout type=STREAM (CONNECTED)

Common options

OptionMeaning
-p PIDFiles of a process
-c nameProcesses whose name starts with name
-u userProcesses of a user; ^user excludes
+D dirOpen files below a directory (recursive)
-i [spec]Internet sockets, e.g. :80, TCP:22, @host
-sTCP:STATETCP state filter, e.g. LISTEN, ESTABLISHED
-UUnix domain sockets
-aJoin selections with AND
-n, -PNumeric addresses and ports
-tPrint PIDs only
+L1Deleted files that are still open

Best practices for lsof

Source

lsof(8) manual page, lsof on GitHub

In this article, we have used lsof to list the open files of processes, find the processes that use a file, directory, or port, combine selections with -a, find and recover deleted files that still take space, and compared it with fuser. See also the ps, kill, and ss articles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Linux tutorials.