Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 204System · Linux

How to see which files a process has open

"The disk is full but du finds nothing", "umount: target is busy", "Too many open files", "who writes to this log?" — all four have the same answer: look at the open files of processes. On Linux everything is a file — log files, sockets, pipes, deleted files that are still in use. lsof and /proc show them. This how-to answers the four questions.

  • 5 Lessons
  • 13 Commands

$ run as a normal user# run as root (or with sudo)

204.1

The quick answer

lsof -p 4711
lsof /var/log/app/app.log
lsof +L1
lsof -i :8080

Commands in this lesson

204.2

Step by step

  1. All files of one process (quick answer, line 1) — columns: FD (file descriptor: cwd, txt, 3w = number 3 open for writing), TYPE (REG file, IPv4 socket, FIFO pipe), NAME.
  2. Who uses this file? (line 2) — the process that writes a log, or reads a config.
  3. **Disk full, but du finds nothing** — a deleted file is still open, so its space is not free:
lsof +L1
lsof -nP | grep '(deleted)'

Restart (or reload) that process, or empty the file through /proc without restarting: : > /proc/4711/fd/7.

  1. "umount: target is busy" — who uses the mount point:
lsof +f -- /mnt/data
fuser -vm /mnt/data

Stop those processes, or cd away in your own shell.

  1. "Too many open files" — count and compare with the limit:
ls /proc/4711/fd | wc -l
grep 'open files' /proc/4711/limits

If the count grows forever, it is a leak (files or sockets not closed). If the app really needs more, raise the limit for the service: LimitNOFILE=65536 in a systemctl edit override, then restart.

  1. Network sockets (line 4) — lsof -i :8080 shows which process listens or is connected; -nP skips name lookups (faster).

204.3

Check that it worked

After the restart, lsof +L1 shows no big deleted files and df -h shows the space free again; umount works; the open-file count of the service stays below its limit (grep 'open files' /proc/PID/limits shows the new limit).

204.4

Common problems

ProblemCauseFix
lsof shows only your own processesNot root.Run with sudo.
lsof hangsA dead NFS mount, or slow name lookups.lsof -b -nP; check the NFS mount.
Space still used after deleting a logThe writer keeps the file open.Restart/reload it; use logrotate with copytruncate or a postrotate reload.
New LimitNOFILE has no effectService not restarted, or limit set in the wrong place.systemctl daemon-reload and restart; check /proc/PID/limits.
lsof: command not foundNot installed.Install lsof, or use ls -l /proc/PID/fd.
Container files not visibleProcess runs in another mount namespace.Run lsof inside the container or use nsenter.

204.5

Go further

1Why can a disk stay full after you delete a big file?

A process still has the file open, so the space is only freed when that process closes it or ends.

2How do you find who blocks an umount?

lsof +f -- /mount/point or fuser -vm /mount/point lists the processes using the mount.

All commands in this chapter