Skip to content

Instantly share code, notes, and snippets.

@tianchaijz
Forked from sergepetit/tips_lsof.sh
Last active January 24, 2022 02:41
Show Gist options
  • Save tianchaijz/7b0cdc612b24116ca479e99d2b6fe680 to your computer and use it in GitHub Desktop.
Save tianchaijz/7b0cdc612b24116ca479e99d2b6fe680 to your computer and use it in GitHub Desktop.
Shell command lsof : see open files and ports; Selection is OR by default, use -a for AND; Tested on OSX
#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
#plenty more options and usage patterns
@akrisiun
Copy link

akrisiun commented Nov 10, 2019

# filter LISTEN - faster variant
lsof -anP -i4 -sTCP:LISTEN

# exclude UDP
lsof -anP -i4 -sTCP:LISTEN | grep TCP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment