List Open Files for a Process on Linux
In this article
When a process runs on Linux, it often has files open: regular files, directories, sockets, pipes, and device nodes. Knowing exactly which files a process has open helps you debug issues like "file is busy", find leaked file descriptors, or understand what a daemon is reading or writing. The standard tool for this is lsof, and the /proc filesystem provides a raw alternative. This guide shows you both methods, step by step.
What You'll Need
- A Linux system with a terminal and shell access.
- The
lsofcommand installed (most distributions include it by default; if not, install it with your package manager, e.g.,sudo apt install lsofon Debian/Ubuntu orsudo dnf install lsofon Fedora). - Root or sudo privileges if the target process belongs to another user.
- The process ID (PID) of the process you want to inspect. Find it with
pgreporps.
Step-by-Step
- Find the process ID (PID). Use
pgrepwith the process name, orps aux | grep. For example, to find the PID of an SSH daemon:pgrep sshd. Note the number(s) returned. - List open files with lsof. Run
lsof -p. Replacewith the actual number. The output shows columns: COMMAND, PID, USER, FD (file descriptor), TYPE (e.g., REG for regular file, DIR for directory, CHR for character device, IPv4/IPv6 for sockets), DEVICE, SIZE/OFF, NODE, and NAME (the file path or socket address). - See only regular files. Add the
-a(AND) flag with-dto filter by file descriptor type. For example, to see only regular files:lsof -a -p– but a simpler approach is to use-d ^cwd,^rtd,^txt,^mem lsof -p. The| grep REG -dflag with^excludes those special descriptors. - List files in the /proc filesystem. The kernel exposes each process's open file descriptors in
/proc/. List them with/fd/ ls -l /proc/. Each symlink points to the actual file or socket. For example,/fd/ 0 -> /dev/pts/0(standard input),1 -> /dev/pts/0(standard output),2 -> /dev/pts/0(standard error), and others for opened files. - Follow the symlinks to see the actual paths. The
ls -loutput already shows the target. If a file has been deleted but is still open, the symlink will show(deleted)at the end of the path – that's a common sign of a file being held open after removal. - Check the process's current working directory and root. The
/proc/symlink points to the current working directory, and/cwd /proc/points to the root directory as seen by the process. Use/root readlink /proc/and/cwd readlink /proc/./root - Use lsof to see which process has a specific file open. If you have a file and want to know which process is using it, run
lsof /path/to/file. This is often more useful than listing all files of a process. - Combine with grep for a quick filter. For a long list, pipe to
grep:lsof -pto find log files. Or use| grep ".log" lsof -pto see network connections.| grep TCP
Common Mistakes
- Forgetting to use sudo. If the process runs as another user (like root or a service account),
lsofmay show nothing or only a few files. Runsudo lsof -pto see all files. - Confusing PID with process name.
lsof -p sshdis wrong – you must provide a numeric PID. Usepgrep sshdfirst. - Assuming all open files are regular files. Sockets, pipes, and character devices are also "files" in Unix. The TYPE column tells you what kind of resource it is.
- Misreading the FD column. The FD column shows the file descriptor number (e.g., 3u) and mode (u for read/write, r for read, w for write). A missing number (like cwd, txt, mem) indicates a special descriptor, not a regular file.
- Using
ls -l /proc/without sudo. If the process is not yours, you'll get permission denied. Use/fd sudo ls -l /proc/./fd
Tips
- Use
lsof -pto show only network connections (sockets) for that process.-i - Use
lsof -pto show file flags (like append or synchronous).+fg - To see the command line of the process, check
/proc/(with null bytes, so use/cmdline tr '\0' ' 'to make it readable). - If you need to see all processes that have a particular file open,
lsof /path/to/fileis the fastest way – no need to loop over PIDs. - For a live view, you can watch
watch -n 1 'lsof -pto refresh every second.' - Remember that file descriptors are inherited by child processes. If you see a file open and you didn't expect it, check the parent process with
ps -o ppid= -p.
Troubleshooting
- lsof returns "command not found". Install it: on Debian/Ubuntu,
sudo apt update && sudo apt install lsof; on RHEL/CentOS/Fedora,sudo dnf install lsof. Alternatively, use the/procmethod. - lsof shows no output for a running process. This usually means the process is owned by another user and you don't have permission. Run with
sudo. If it still shows nothing, the process may have closed all its file descriptors (rare but possible). - Permission denied when accessing /proc/
/fd. The/procfilesystem restricts access to the process owner and root. Usesudo. - You see a path with "(deleted)". The file was unlinked, but the process still holds a descriptor. This is common for temporary files or logs that were rotated. The process will keep using the space until it closes the descriptor.
- lsof hangs or is slow. On busy systems,
lsofmay take time. Try narrowing the scope with-pand-dflags. If it hangs completely, usetimeout 5 lsof -p. - You need to list open files for a thread. Use
lsof -pto show thread-specific information, or look at-T /proc/for each thread ID./task/ /fd/
Frequently Asked Questions
- What is the difference between lsof and /proc?
lsofis a user-friendly tool that reads from/procand formats the output. The/procmethod gives you raw symlinks and is always available, even iflsofis not installed. - Can I list open files for a process I don't own? Yes, but you need root privileges. Use
sudo lsof -porsudo ls -l /proc/./fd - How do I find the PID of a process by name? Use
pgreporpidof. For example,pgrep nginxreturns the PID(s) of nginx. - What does the FD column mean in lsof output? It shows the file descriptor number and mode. For example,
3umeans descriptor 3, open for read and write.cwdis the current working directory,txtis the executable,memis a memory-mapped file, andrtdis the root directory. - Why do I see sockets as files? In Unix, everything is a file, including network sockets. The TYPE column will show IPv4, IPv6, or UNIX for sockets. To see only network connections, use
lsof -i. - Can I close an open file descriptor from the command line? No, you cannot directly close a file descriptor of another process. You must either wait for the process to close it, kill the process, or use debugging tools like
gdbto force a close (which is risky and not recommended).