How to List all Open File Descriptors

One of the easiest ways to list all open file descriptors is to use the lsof command

If you are here to list out all the open file descriptors, then you may be aware of what a file descriptor is and what the use of it is, but if you are unaware of file descriptors, then let me tell you that file descriptors are non-negative integer numbers that identify open files within the kernel.

And the kernel keeps track of these file descriptors to ensure that when a process requests to access a file, the system should know where it can be found.

I believe you understand the basic nature of a file descriptor after reading this, and if you want to learn more about it, you can read this article, which explains the concept of a file descriptor in detail.

Now let’s continue with this guide to show you the steps to listing out all the open file descriptors.

List File Descriptors in Linux

There are two common ways to list all open file descriptors: One way is to use the lsof command line utility, and another way is to read through the /proc/PID/fd directory.

Using the lsof command to list all open file descriptors

First, I will show you how you can use the lsof command to list all open file descriptors, which is basically used to list all open files for a process running in your system.

To show you how it works, let me take a simple example where I’ll use the -p option to list all the files of the specific process, and to specify the process, I’m using the $$ to substitute the process ID of the current shell.

$ lsof -p $$

And here is the output, which shows that the current bash shell has four different file descriptor counts: 0, 1, 2, and 255, which are mixed with cwd (the current working directory), rtd (the root directory), txt (programme text or binary code), and mem (memory mapped file), which you can suppress for more concise output.

List of open file for specific process
List of open files for specific processes

To remove other than file descriptors like (cwd, txt, mem, rtd information), you can use the -a and -d options to specify the range of file descriptors, and as usual, use -p with the process number, but here I’m substituting with $$ to get process id of current shell.

$ lsof -a -d 0-255 -p $$
List only file descriptors
List only file descriptors

If you’re wondering why “u” is attached next to the file descriptors, it means the file is open with read and write permission. It could also be r (file open with read permission), w (file open with write permission), or W (file open with write permission and write lock) in your case.

Using the /proc/PID/fd command to list all open file descriptors

The same thing can be done with the ls -l /proc/PID/fd command, which will also list all the file descriptors for the specific process.

For example, to get a list of all the file descriptors for the current shell, you could run the below command, which will list all the file descriptors with their paths and permissions.

$ ls -l /proc/$$/fd
list of file descriptors
list of file descriptors

If you want to look for a specific process, use the ps -aux | grep <program-name> command to get the process number, and then use the following command to list all the file descriptors:

$ ls -l /proc/1143912/fd

The output of the above command:

Process id fetch from ps command and use ls command to list file descriptors
The ps command returns the process id, and the ls command with lists file descriptors.

It is now up to you to decide which method you want to use to list all of the file descriptors.

If you have any questions or comments, please feel free to leave them in the comments section below. Until then, put your feet up and relax. As always, the next article will be up shortly.

Leave a Reply

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.