Skip to content

Instantly share code, notes, and snippets.

@petergi
Created May 7, 2025 14:25
Show Gist options
  • Save petergi/bd5d18a7204c58fde9f59cb83e57dbcf to your computer and use it in GitHub Desktop.
Save petergi/bd5d18a7204c58fde9f59cb83e57dbcf to your computer and use it in GitHub Desktop.
You know.. a couple of handy examples of how to use lsof...
# Find what's using a volume on macOS
lsof | grep /Volumes/
# Check what processes are listening on port
lsof -iTCP -sTCP:LISTEN -P -n
# Kills a process using PID
#
# awk grabs the PIDs.
# tail gets rid of the pesky first entry: "PID".
# xargs executes kill -9 on the PIDs.
# The -r / --no-run-if-empty, prevents kill command failure, in case lsof did not return any PID.
lsof +D ./ | awk '{print $2}' | tail -n +2 | xargs -r kill -9
# Kills the process running on a port
lsof -t -t:port | xargs kill
# Determines which app is opening too many files
lsof -n | awk '{print $1}' | uniq -c | sort -rn | head -n $LINES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment