lsof: the files a process holds open

Video 129 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:44, with English and Hindi audio.

A disk is full, but the files on it look small. The missing space is often held by a file that was deleted by name while a running program still has it open. The lsof command lists the files, network connections and pipes that each process has open, so you can find that holder. This article shows how to use it with a process, a port, a path and a deleted file.

What you will learn

A full disk with no visible cause

On a warehouse server, the data disk is almost full, and new writes start to fail. The engineer looks at the folders, and they add up to much less than the disk says is used. The missing space is held by a file that was deleted by name, but a running program still has it open.

An open file is anything a running process has opened for use. That includes ordinary files, network connections, and pipes between programs. The kernel tracks each one, and the space of a file is freed only when no process holds it any longer.

Finding the files, ports and holders

To see the files one process holds, give lsof its process ID with the p option. Each row shows a file, a socket, or a pipe.

List the first open files of process 4321
sudo lsof -p 4321 | head -n 6

To find who holds a port, use the i option with a colon and the port number. The port 5432 used here is the usual one for PostgreSQL.

Show the network connections on port 5432, and the program that holds each one
sudo lsof -i :5432

To find every process that holds one file open, give lsof the file's path. Use the real path of your own file.

Show every process holding this file open
sudo lsof /srv/exports/orders.csv

Deleted files that still take space

A log file on the data disk can be deleted while an export job still writes into it. The name disappears from the folder, but the job keeps writing into the invisible file, and the disk fills up. The plus L one option finds those files, so run it when a disk is full and the folders look small.

List open files that were deleted but are still held
sudo lsof +L1

Restarting the program that holds the file closes it. The kernel then frees the space.

Reading the columns

What goes wrong with lsof

Try it yourself

You need a Linux server where you have sudo rights. The steps only read the open files, and change nothing. Use a process ID and a file path of your own in place of the examples.

  1. List the first open files of process 4321. Use a process ID of your own in its place.

    sudo lsof -p 4321 | head -n 6

    What you should see: Each row shows a file, a socket or a pipe that the process holds open.

  2. Show the network connections on port 5432, the usual PostgreSQL port, and the program that holds each one.

    sudo lsof -i :5432

    What you should see: Each connection on that port is listed, with the program that holds it.

  3. Show every process holding one file open. Use the real path of a file of your own.

    sudo lsof /srv/exports/orders.csv

    What you should see: Every process that holds the file open is listed.

  4. List open files that were deleted but are still held. Their space is not freed while a program holds them.

    sudo lsof +L1

    What you should see: Deleted files that a program still holds are listed, with the word deleted after the name.

Terms

Open file
A file, network connection or pipe that a running process has opened and is using, tracked by the kernel.
File descriptor
The number, or the special letters such as cwd, that names an open file inside a process, shown in the FD column.

Check yourself

Why does a full disk not show its space in the folders?

A deleted file keeps its space while a program still has it open. The folders no longer list the file, but the disk still holds its space.

Which option finds who holds a network port?

The i option, with a colon and the port number. For example, lsof -i :5432 shows the holders of port 5432.

Why run lsof with sudo?

Without sudo, lsof shows only your own files, so other users' open files stay hidden and the picture is incomplete.

Previous: The sys folder: the kernel's view of devices. Next: What does systemd do?.