Drill: Find Open Files with lsof¶
Goal¶
Use lsof to find open files, detect deleted files still holding disk space, and identify network connections by process.
Setup¶
- Linux system with lsof installed (
apt install lsoforyum install lsof) - Root access for viewing all processes
Commands¶
List all files open by a specific process:
Find which process has a specific file open:
Find deleted files still holding disk space:
List all network connections for a process:
Find what is listening on a specific port:
List all files opened by a specific user:
Find processes using files in a specific directory:
Show established TCP connections:
What to Look For¶
(deleted)marker on files means the file is removed from the filesystem but the process still holds a file descriptor- FD column shows file descriptor number and access mode (r, w, u for read/write/both)
- TYPE column distinguishes regular files (REG), directories (DIR), sockets (IPv4/IPv6)
- Deleted files holding space require restarting the process or truncating via /proc/PID/fd
Common Mistakes¶
- Using
+Don large directories (it recurses and can be slow); use+dfor non-recursive - Forgetting to use
-a(AND) when combining flags; without it lsof uses OR logic - Not running as root and missing other users' processes
- Confusing file descriptor numbers with PIDs
Cleanup¶
No cleanup needed. lsof is read-only.