Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save cuiwm/ea6b13930d1349895613b4d399308ecd to your computer and use it in GitHub Desktop.

Select an option

Save cuiwm/ea6b13930d1349895613b4d399308ecd to your computer and use it in GitHub Desktop.
pwdx for mac. Usage: pwx pid also portpwdx that finds the pwd for the process that owns a port. Usage: portpwdx port
function pwdx {
# note: NF is the number of columns, so $NF gives us the last column
lsof -a -p $1 -d cwd -n | tail -1 | awk '{print $1, "\t", $NF}'
}
function portpwdx {
# pid=$(lsof -i :5858 | tail -1 | perl -pe 's/[^\s]+\s+([^\s]+)\s.*/$1/')
pid=$(lsof -i :$1 | tail -1 | perl -pe 's/[^\s]+\s+([^\s]+)\s.*/$1/')
if [[ ! -z $pid ]]; then
echo $pid
pwdx $pid
return 0
else
echo "No process listening to port $1"
return -1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment