Skip to content

Instantly share code, notes, and snippets.

@danielnegri
Created November 15, 2016 20:44
Show Gist options
  • Select an option

  • Save danielnegri/2c70bf96ea9cffd46a19caff08a85172 to your computer and use it in GitHub Desktop.

Select an option

Save danielnegri/2c70bf96ea9cffd46a19caff08a85172 to your computer and use it in GitHub Desktop.
Report current working directory of a process by port
# Report current working directory of a process by port
function pwdx {
# lsof -a -p $1 -d cwd -n | tail -1 | awk '{print $NF}'
lsof -a -p $1 -d cwd -n
}
portpwdx () {
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