How to Use lsof Command in Linux to List Open Files

Open Featured

The good thing about Linux is that you can easily view and manage everything, from the boot process to the installation of software packages. Here we discuss how you can use the lsof command in Linux to view open files and the processes using them. Knowing how to view this can help you understand how the system works and even take the necessary actions for specific processes.

Lsof Command

To view open files and the users or processes responsible for them, we use the lsof utility. By default, lsof is pre-installed in most distributions.

However, if you do not have it installed, you can use the package manager to install it on your system.

Debian/Ubuntu

On Debian, run the command:

Arch/Manjaro

On Manjaro and other Arch-based distributions, use pacman by running the command:

CentOS/REHL/Fedora

For CentOS and the REHL family, you can use dnf:

Use lsof Command to List Open Files for a Linux Process

Like most Linux commands, the lsof utility is incredibly simple to use. Start by typing the command lsof:

Once you run the command above, lsof should return information about the open files in the system.

Note: if you have sudo privileges, run the command with sudo to avoid “permission denied” errors on specific files.

As shown in the output above, the lsof output has the following columns:

ColumnRepresentation
CommandShows the name of the process using the target file.
PIDThe unique identifier for the process using the file.
TIDThe column shows the thread identifier.
TASKCMDThe name of the task command.
USERUsername or UID of the user running the process.
FDFile descriptor of the file and modes.
TYPENode associated with the target file.
DEVICEDevice number separated by commas.
SIZE/OFFFile size in bytes of file offset size
NODEInode value of the local file. You can use the stat command to show inode information for the file.
NAMEMount point of the file.

Now that you understand what the contents of the lsof command printout represent, let us use the command to filter for specific information.

How to Filter for Specific Process

To filter for only specific files opened by the specific process, we can use either the process name or the PID value.

For example, to show files used by the firefox process, we can use the command:

Lsof C Firefox

The command will show all the files opened by the firefox process.

To filter by process ID, we can use the -p option and pass the process ID. You can use the top command to get the process ID of the target process.

For example, to get the PID of the firefox process, we can use the command:

Once you have the PID of the target process, use lsof to show the open files:

Lsof Pid

The above command will print the files opened by the process with the PID specified.

How to Filter for a Specific User

To view only the files opened by a specific user, we can use the -u flag. For example, to filter for the Debian user, use the command:

Lsof U Debian

How to Filter for a Specific File

Suppose you only want to know the process and the user who opened a specific file. To do this, pass the name of the file to lsof:

Lsof Sleep

The above will only filter for the specific file and return the related information, including the user, process ID, and more.

Wrapping Up

In this simple tutorial, we discussed how to query the system for information about open files using the lsof command in Linux. Here are some additional commands for you to list the content of a directory in the terminal.

John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.