$linuxjunkies
>

lsof(1)

List open files and the processes that opened them.

UbuntuDebianFedoraArch

Synopsis

lsof [OPTION]... [FILE|PROCESS]...

Description

lsof displays information about files opened by processes. It can show which files, sockets, pipes, and devices are in use by which processes, making it invaluable for troubleshooting resource conflicts, finding who's using a file, and understanding system activity.

Without arguments, lsof lists all open files on the system. You can filter by process ID, user, filename, or network connections to narrow results.

Common options

FlagWhat it does
-p PIDList files opened by process ID (comma-separated for multiple PIDs)
-u USERList files opened by a specific user
-iList all network connections (IPv4 and IPv6)
-i :PORTList processes using a specific port (e.g., -i :8080)
-nSuppress hostname lookup (faster output)
-PSuppress port name lookup; show numeric port instead
-aAND together conditions (default is OR)
-c COMMANDList files opened by processes matching command name
-d FDList files matching file descriptor (e.g., -d 1-3 for stdin/stdout/stderr)
-tOutput only process IDs (useful for piping to kill)

Examples

Show first 20 open files system-wide (may require root for full output)

lsof | head -20

List all files opened by process with PID 1234

lsof -p 1234

Show which process is listening on port 8080

lsof -i :8080

List all TCP connections with numeric IP addresses and ports

lsof -i TCP -n -P

Find which process(es) have the log file open

lsof /var/log/app.log

Show network connections opened by the apache user

lsof -u apache -a -i

List all files opened by processes named nginx

lsof -c nginx

Get unique file descriptors used by all MySQL processes

lsof -p $(pgrep mysql) -t | sort -u

Related commands