Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

List Open Files for a Process on Linux

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 lsof command installed (most distributions include it by default; if not, install it with your package manager, e.g., sudo apt install lsof on Debian/Ubuntu or sudo dnf install lsof on 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 pgrep or ps.

Step-by-Step

  1. Find the process ID (PID). Use pgrep with the process name, or ps aux | grep . For example, to find the PID of an SSH daemon: pgrep sshd. Note the number(s) returned.
  2. List open files with lsof. Run lsof -p . Replace with 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).
  3. See only regular files. Add the -a (AND) flag with -d to filter by file descriptor type. For example, to see only regular files: lsof -a -p -d ^cwd,^rtd,^txt,^mem – but a simpler approach is to use lsof -p | grep REG. The -d flag with ^ excludes those special descriptors.
  4. List files in the /proc filesystem. The kernel exposes each process's open file descriptors in /proc//fd/. List them with ls -l /proc//fd/. Each symlink points to the actual file or socket. For example, 0 -> /dev/pts/0 (standard input), 1 -> /dev/pts/0 (standard output), 2 -> /dev/pts/0 (standard error), and others for opened files.
  5. Follow the symlinks to see the actual paths. The ls -l output 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.
  6. Check the process's current working directory and root. The /proc//cwd symlink points to the current working directory, and /proc//root points to the root directory as seen by the process. Use readlink /proc//cwd and readlink /proc//root.
  7. 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.
  8. Combine with grep for a quick filter. For a long list, pipe to grep: lsof -p | grep ".log" to find log files. Or use lsof -p | grep TCP to see network connections.

Common Mistakes

  • Forgetting to use sudo. If the process runs as another user (like root or a service account), lsof may show nothing or only a few files. Run sudo lsof -p to see all files.
  • Confusing PID with process name. lsof -p sshd is wrong – you must provide a numeric PID. Use pgrep sshd first.
  • 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//fd without sudo. If the process is not yours, you'll get permission denied. Use sudo ls -l /proc//fd.

Tips

  • Use lsof -p -i to show only network connections (sockets) for that process.
  • Use lsof -p +fg to show file flags (like append or synchronous).
  • To see the command line of the process, check /proc//cmdline (with null bytes, so use tr '\0' ' ' to make it readable).
  • If you need to see all processes that have a particular file open, lsof /path/to/file is the fastest way – no need to loop over PIDs.
  • For a live view, you can watch watch -n 1 'lsof -p ' to 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 /proc method.
  • 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 /proc filesystem restricts access to the process owner and root. Use sudo.
  • 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, lsof may take time. Try narrowing the scope with -p and -d flags. If it hangs completely, use timeout 5 lsof -p .
  • You need to list open files for a thread. Use lsof -p -T to show thread-specific information, or look at /proc//task//fd/ for each thread ID.

Frequently Asked Questions

  1. What is the difference between lsof and /proc? lsof is a user-friendly tool that reads from /proc and formats the output. The /proc method gives you raw symlinks and is always available, even if lsof is not installed.
  2. Can I list open files for a process I don't own? Yes, but you need root privileges. Use sudo lsof -p or sudo ls -l /proc//fd.
  3. How do I find the PID of a process by name? Use pgrep or pidof . For example, pgrep nginx returns the PID(s) of nginx.
  4. What does the FD column mean in lsof output? It shows the file descriptor number and mode. For example, 3u means descriptor 3, open for read and write. cwd is the current working directory, txt is the executable, mem is a memory-mapped file, and rtd is the root directory.
  5. 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.
  6. 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 gdb to force a close (which is risky and not recommended).