lsof – List Of Open Files

Files opened by processes running on your system.

# lsof

List all opened internet sockets.

# lsof -i

List all opened internet sockets on port 80.

# lsof -i :80

List all udp or tcp connections.

# lsof -i udp
# lsof -i tcp
# lsof -i tcp:80

Files open by PID.

# lsof -p 4455

List all open files EXCEPT process with PID.

# lsof -p ^1

Files open by the application httpd.

# sudo lsof /usr/sbin/httpd
# sudo lsof `which httpd`

FIles open by name.
The -c option searches for a pattern. For example, i used the letter “h” below which will show you all processes that start with the letter “h”.

# lsof -c h
# lsof -c httpd
# lsof -c Dropbox
# lsof | grep httpd
# lsof -c bash

Shows all processes that have a TCP connection opened to or from the host ractivate.adobe.com at port 49154.

# lsof -iTCP@practivate.adobe.com:49154

Shows a list of open files ran by user.

# lsof -u tommy
# lsof -u _www
# lsof -u root

Shows a list of open files EXCEPT ran by the user root.

# lsof -u ^root

Files open in a directory.

# lsof +D /Users/Tommy

List all UNIX domain sockets.

# lsof -U

List by group id.

# lsof -g 1597

List NFS files.

# lsof -N

The -t option outputs only PIDs of processes. Used together with -i it outputs PIDs of all processes with network connections. This allows you to kill all processes that use network.

# kill -9 `lsof -t -i`

Repeat listing files every 1 second.

# lsof -r 1

The -r option makes lsof repeatedly list files until interrupted. Argument 1 means repeat the listing every 1 second. This option is best combined with a narrower query such as monitoring user network file activity:

# lsof -r 1 -u john -i -a

Note:
Mac OS X, lsof only shows your own processes unless running as root with sudo.