A Python process deleted its own 15,000-byte log file and kept running. ls showed nothing, the disk space didn’t come back, and the file seemed to be gone. One lsof command found it in under a second, still open, still holding its data, and a copy through /proc recovered all 1,000 lines. lsof, short for “list open files,” answers a family of questions that otherwise lead to guesswork: what’s using this port, why won’t this drive unmount, which process is eating space nobody can see. Every example below was run on a Linux test machine and shows the real output.
What lsof shows
Everything is a file
The lsof manual page describes the tool as listing “file information about files opened by processes.” On Linux, that covers far more than documents: network sockets, pipes, devices, libraries and each process’s working directory all count as open files.
Reading the columns
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 580 root 3u IPv4 3211 0t0 TCP 127.0.0.1:8099 (LISTEN)
FD is the file descriptor: a number with a mode letter (r read, w write, u both), or a label such as cwd for the current directory. TYPE says what kind of thing it is, and NAME says which one.
Two flags worth always adding
The man page explains that -n “inhibits the conversion of network numbers to host names” and -P does the same for port numbers. Without them, lsof stops to look up names, which can make it slow on a machine with network problems, exactly when it’s needed.

Who’s using this port
Finding the process behind a port
A test web server ran on port 8099. Asking lsof about that port:
$ lsof -nP -i :8099
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 580 root 3u IPv4 3211 0t0 TCP 127.0.0.1:8099 (LISTEN)
The manual describes -i as selecting files “any of whose Internet address matches the address specified.” That one line answers the classic “address already in use” error: which program, which process ID, and which address it’s bound to.
Both ends of a connection
With a client connected to the test server, adding -sTCP:ESTABLISHED showed the connection from both sides, since both processes ran on the same machine:
$ lsof -nP -i :8099 -sTCP:ESTABLISHED
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 669 root 4u IPv4 4413 0t0 TCP 127.0.0.1:8099->127.0.0.1:52214 (ESTABLISHED)
python3 670 root 3u IPv4 4082 0t0 TCP 127.0.0.1:52214->127.0.0.1:8099 (ESTABLISHED)
The arrow reads local address to remote address. Process 669 is the server, which accepted the connection on a new descriptor, 4, while its listening socket stayed on 3.
ss gives the same answer
The ss command from the iproute2 tools answers the port question too. ss -tlnp 'sport = :8099' printed users:(("python3",pid=669,fd=3)) for the listening socket. On minimal systems where lsof isn’t installed, ss -tlnp is usually already there.
Only listeners
lsof -nP -iTCP -sTCP:LISTEN lists every program waiting for TCP connections, the quickest inventory of what a machine is serving.
Just the process ID
For scripts, -t produces, in the manual’s words, “terse output comprising only process identifiers.” lsof -t -i :8099 printed just 580, ready to pass to another command. kill $(lsof -t -i :8099) stops whatever holds the port, which is convenient and also worth double-checking before running.
Deleted files that still take up space
Finding them
The test process opened app.log, wrote 1,000 lines, deleted the file and kept running. The man page says +L1 selects “open files that have been unlinked”:
$ lsof -nP +L1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
python3 585 root 3w REG 254,0 15000 0 1155157 /tmp/lsofx/app.log (deleted)
NLINK 0 means no directory entry points to the file anymore. It still exists because a process holds it open, and its 15,000 bytes still count against the disk.
Recovering the contents
The proc_pid_fd manual page describes /proc/PID/fd as a directory “containing one entry for each file which the process has open,” each one “a symbolic link to the actual file.” Even for a deleted file, that link still opens the data:
$ ls -l /proc/585/fd/3
l-wx------ 1 root root 64 ... /proc/585/fd/3 -> /tmp/lsofx/app.log (deleted)
$ cp /proc/585/fd/3 recovered.log
$ wc -l < recovered.log
1000
All 1,000 lines came back. This only works while the process is still running; once it exits, the space is freed and the data with it.
Freeing the space without a restart
A log file deleted by mistake under a running service keeps growing out of sight. In a second test, a process held a deleted 50 MB file open. Truncating it through /proc, with : > /proc/PID/fd/3, dropped its size in lsof to 0 bytes, and df showed 47 megabytes freed on the disk, without stopping the process. Restarting the service also frees it, but truncation works when a restart isn’t an option.

Why won’t this unmount
The busy target
A small test filesystem was mounted, and a shell sat inside it. Trying to unmount:
$ umount /tmp/lsofx/mnt2
umount: /tmp/lsofx/mnt2: target is busy.
Finding the culprit with fuser
The fuser manual describes it as displaying “the PIDs of processes using the specified files or file systems.” With -v for detail and -m for a mounted filesystem:
$ fuser -vm /tmp/lsofx/mnt2
USER PID ACCESS COMMAND
/tmp/lsofx/mnt2: root kernel mount /tmp/lsofx/mnt2
root 614 ..c.. sleep
The c in the access column marks the process’s “current directory.” Nothing was open inside the folder; a process was just standing in it. Once that process ended, umount succeeded.
The lsof version
lsof +D /tmp/lsofx/mnt searches, as the man page puts it, all the files and directories under a path “to its complete depth.” It found the same kind of culprit in an ordinary directory:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sleep 603 root cwd DIR 254,0 4096 1155159 /tmp/lsofx/mnt
The cwd in the FD column is the man page’s label for “current working directory.” On a large tree, +D can be slow because it checks every file below the path.
Who has this file open, and is it locked
A test script opened a SQLite database and held an exclusive transaction. Asking lsof about the file:
$ lsof /tmp/lsofx/shop.db
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 675 root 3uw REG 254,0 8192 1155157 /tmp/lsofx/shop.db
The extra w after 3u is a lock flag. The lsof manual lists w “for a write lock on part of the file” and W “for a write lock on the entire file.” When an application reports that a database is locked, this shows which process holds the lock.
Looking inside one process
Everything a process has open
lsof -p PID lists one process’s open files. Combined with -a, which the manual says “causes list selection options to be ANDed,” and -d 0-10 for the first file descriptors, it shows the essentials:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 585 root 0r CHR 1,3 0t0 3 /dev/null
python3 585 root 1w REG 254,0 915 1196050 [output file]
python3 585 root 2w REG 254,0 915 1196050 [output file]
python3 585 root 3w REG 254,0 15000 1155157 /tmp/lsofx/app.log (deleted)
Standard input read from /dev/null, standard output and error both went to the same file, and descriptor 3 was the deleted log. That’s a full picture of where a background process reads and writes, useful when a daemon’s output seems to vanish.
Without the AND
Without -a, lsof treats its options as alternatives and lists files matching any of them. lsof -p 585 -i :8099 would list both process 585’s files and anything on port 8099, which surprises people who expected a filter.

Quick reference
The commands above, in one place
| Question | Command |
|---|---|
| What’s using port 8099? | lsof -nP -i :8099 |
| What’s listening on TCP? | lsof -nP -iTCP -sTCP:LISTEN |
| Just the PID | lsof -t -i :8099 |
| Deleted files still open | lsof -nP +L1 |
| Recover a deleted open file | cp /proc/PID/fd/N recovered |
| Free its space now | : > /proc/PID/fd/N |
| Why won’t this unmount? | fuser -vm /mount/point |
| Who’s inside this folder? | lsof +D /path |
| Everything one process has open | lsof -p PID |
Narrowing the output
A full lsof with no options lists every open file on the system, often tens of thousands of lines on a busy server. Starting from the most specific question, a port, a path or a PID, keeps the answer to a few lines. Adding -n and -P keeps it fast: on the test machine, a full listing finished in well under a tenth of a second, but lookups of host names on a machine with a broken DNS server can turn the same command into a long wait.
Root sees more
As an ordinary user, lsof shows only that user’s processes in full. Investigating a system service, a busy mount or someone else’s port usually needs sudo.
Sources
- lsof(8), Linux manual page: https://man7.org/linux/man-pages/man8/lsof.8.html
- proc_pid_fd(5), Linux manual page: https://man7.org/linux/man-pages/man5/proc_pid_fd.5.html
- fuser(1), Linux manual page: https://man7.org/linux/man-pages/man1/fuser.1.html
