14 January 2020

lsof tips & tricks

lsof is a command meaning "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them. This open source utility was developed and supported by Victor A. Abell, the retired Associate Director of the Purdue University Computing Center.

pid from port
lsof -i @192.14.166.72:20000

open jar files:
lsof -p | grep jar

Get the list of files opened
lsof –p

Get the count
lsof –p | wc –l

pid listening tcp at specific port:
lsof -i tcp:8888
lsof -i:22


From gist

#list all ports for tcp
sudo lsof -itcp

#find all things listening on ports
lsof -Pnl +M -i4 | grep LISTEN

#all ports for tcp, dont resolve port name from numbers
sudo lsof -itcp -P

#open files and ports of process #$PID
sudo lsof -p $PID

#only ports of tcp for process #$PID, dont resolve port name, dont resolve ip name
sudo lsof -a -p $PID -P -n -itcp

#only ports of tcp for process #$PID, dont resolve port name, dont resolve ip name, refresh every 5 seconds
sudo lsof -a -p $PID -P -n -itcp -r 5

#search by file (can be slow)
sudo lsof /complete/path/to/file

My Blog List

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.