List a Process's Open Files with /proc/self/fd and lsof
List the files a Linux process has open with ls -l /proc/PID/fd, read fdinfo for offsets and flags, and use lsof for whole-system questions, with a C version.

When a program leaks descriptors, holds a deleted file, or will not release a mount point, the fastest question to ask is what it has open right now. Linux answers that two ways. The kernel exposes every process's descriptor table as a directory of symbolic links under /proc, and the lsof tool reads those same sources and adds the questions the kernel interface cannot answer alone, such as which process has this file open. This post shows both, from the one-line shell version up to a small C program that lists its own descriptors. It uses Linux 6.12 and procfs, and the outputs are examples with fixed numbers because real values differ by machine.
The /proc/PID/fd directory
For every process, the kernel provides the directory /proc/<pid>/fd. Each entry is named after a descriptor number and is a symbolic link to whatever the descriptor refers to. The proc(5) manual page documents this layout and notes that reading another process's entries needs the right permissions: you can inspect your own processes, and root can inspect anyone's.
The shorthand /proc/self always refers to the process doing the lookup, so a shell command that lists /proc/self/fd shows the descriptors of the ls process itself:
ls -l /proc/self/fdtotal 0
lrwx------ 1 dev dev 64 Nov 23 10:14 0 -> /dev/pts/2
lrwx------ 1 dev dev 64 Nov 23 10:14 1 -> /dev/pts/2
lrwx------ 1 dev dev 64 Nov 23 10:14 2 -> /dev/pts/2
lr-x------ 1 dev dev 64 Nov 23 10:14 3 -> /proc/4411/fdDescriptors 0, 1 and 2 point at the terminal. Descriptor 3 is the directory handle ls opened to read the listing, which is why it appears pointing at its own fd directory. The dates, the owner and the process id will differ on your machine. The permission string on the link shows the access mode: lrwx means the descriptor was opened for reading and writing, and lr-x means read-only.
To inspect a running service, replace self with its pid. A common way to find the pid is pgrep:
sudo ls -l /proc/$(pgrep -o nginx)/fdtotal 0
lrwx------ 1 root root 64 Nov 23 10:20 0 -> /dev/null
lrwx------ 1 root root 64 Nov 23 10:20 1 -> /dev/null
l-wx------ 1 root root 64 Nov 23 10:20 4 -> /var/log/nginx/error.log
lrwx------ 1 root root 64 Nov 23 10:20 6 -> 'socket:[48211]'
lrwx------ 1 root root 64 Nov 23 10:20 7 -> 'anon_inode:[eventpoll]'Two kinds of target appear here that are not paths. socket:[48211] is a socket identified by its inode number. anon_inode:[eventpoll] is an epoll instance. When a link target has a trailing (deleted), the file was unlinked while open, which is the signature of the "disk is full but the file is gone" problem.
Counting descriptors
A leak shows up as a count that only grows. Counting entries is one pipeline:
ls /proc/self/fd | wc -l4The result is 4 rather than 3 because the ls itself holds the directory descriptor while it counts. For a service, sample the count on a schedule and watch the trend. A number steadily climbing toward the limit from ulimit -n predicts an EMFILE failure before it happens, and fixing too many open files covers what to do next.
Reading offsets and flags from fdinfo
The directory /proc/<pid>/fdinfo has a matching file per descriptor with details about the open file description:
cat /proc/self/fdinfo/0pos: 0
flags: 02
mnt_id: 27
ino: 5pos is the current offset, flags is the octal value of the status flags and access mode (here 02, which is O_RDWR), mnt_id identifies the mount, and ino is the inode number. Because it describes the open file description rather than the descriptor, two descriptors that share one description, such as a dup() pair, show the same pos. That is the quickest way to confirm sharing when debugging.
Listing your own descriptors from C
Inside a program, read /proc/self/fd with the same directory calls you would use for any folder, and resolve each link with readlink():
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int extra = open("/etc/hostname", O_RDONLY); /* one descriptor to find */
DIR *dir = opendir("/proc/self/fd");
if (!dir) {
perror("opendir");
return EXIT_FAILURE;
}
int dir_fd = dirfd(dir);
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.')
continue;
int fd = atoi(ent->d_name);
if (fd == dir_fd)
continue; /* skip our own scan handle */
char link[64], target[PATH_MAX];
snprintf(link, sizeof link, "/proc/self/fd/%d", fd);
ssize_t n = readlink(link, target, sizeof target - 1);
if (n < 0)
continue;
target[n] = '\0';
printf("%3d -> %s\n", fd, target);
}
closedir(dir);
close(extra);
return EXIT_SUCCESS;
}Compiled with GCC 14 (gcc -Wall -o listfds listfds.c) and run from a terminal, the output looks like this:
0 -> /dev/pts/2
1 -> /dev/pts/2
2 -> /dev/pts/2
3 -> /etc/hostnameThe scan skips dir_fd because opendir() itself consumed a descriptor, which would otherwise appear as a link into /proc. readlink() does not append a terminating byte, so the code adds one. Targets such as socket:[48211] come back as plain strings through the same call, and they are not real paths, so never pass them to open().
The order from readdir() is not guaranteed, and the example happens to print in numeric order. Collect and sort if you need stable output.
lsof for the questions /proc cannot answer
The /proc interface is per process: given a pid, it lists descriptors. lsof inverts the question as well. To find who has a file open, give it the path:
lsof /var/log/app.logCOMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
app 3120 dev 4w REG 253,1 104857 931204 /var/log/app.logThe FD column shows 4w: descriptor 4, open for writing. To list one process's descriptors with types, use lsof -p 3120. To find processes holding a mount point busy, lsof +D /mnt/data walks the directory tree, which is slow on big trees, so prefer lsof /mnt/data first. To find deleted-but-open files, lsof +L1 lists open files with a link count below one:
lsof +L1COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
app 3120 dev 5w REG 253,1 52428800 0 931377 /var/log/old.log (deleted)That output is the answer to "the file is deleted, so why is the disk still full": process 3120 holds a 50 MiB file whose name is gone. Restart the process or truncate it through /proc/3120/fd/5 to reclaim the space.
Watching for a leak over time
A single listing tells you what is open. A leak shows up as a number that rises while the service is idle. The limit the process is working against is in /proc/<pid>/limits, which makes the comparison direct:
grep 'Max open files' /proc/self/limitsMax open files 1024 524288 filesThe first number is the soft limit and the second the hard limit, and both are examples that depend on your system and your service manager. A small shell loop turns the count into a time series you can read:
pid=$(pgrep -o app)
while sleep 60; do
printf '%s %s\n' "$(date +%H:%M:%S)" "$(ls /proc/$pid/fd | wc -l)"
done10:41:02 212
10:42:02 214
10:43:02 219
10:44:02 227A count that only moves upward, with no matching drop when traffic falls, points at a path that opens without closing. Group the current targets to see what is accumulating:
ls -l /proc/$pid/fd | awk '{print $11}' | sed 's/\[[0-9]*\]//' | sort | uniq -c | sort -rn | head 181 socket:
14 /var/lib/app/cache.db
3 /dev/nullThe sed step strips the inode number from each socket name, so that 181 different sockets count as one line instead of 181. Sockets dominating the list means connections are not being closed; check the idle timeout on the pool or the server first, because a missing close() on an error path is the second most common cause. The same file repeated many times means a path that opens without closing it. This is the evidence to bring to the code, and it costs nothing to collect in production because reading /proc does not stop the process.
When these tools fall short
/proc/<pid>/fd needs permission to read, so a non-root user sees Permission denied for other users' processes, and hardened systems can mount /proc with hidepid to hide processes from each other. lsof is slower than reading /proc directly because it scans every process, and it can hang on a stale network mount while resolving paths; the -n, -P and -b options skip name lookups and blocking calls. Containers add one more twist: a process in a different PID namespace has a different pid number inside than outside, so use the pid that the host's /proc shows.


