working directory of running process on mac os

Aarkan picture Aarkan · Nov 30, 2011 · Viewed 15.2k times · Source

I want to know the working directory of a process on Mac OS (10.6). I tried finding the PWD environment variable in ps command's output, but the PWD variable is not available there. Is there a better way to find this for a running process on mac?

Answer

Gordon Davisson picture Gordon Davisson · Nov 30, 2011

lsof -d cwd will print the current working directories for all of your processes. If you want to show info on processes you don't own, you need to run it as root (i.e. use sudo as a prefix). If you want to show the info for only certain programs or processes, use e.g. lsof -a -d cwd -c programname or lsof -a -d cwd -p processid (note: in both cases, the -a flag means that the other flags' restrictions get "and"ed together). lsof is pretty complex and has lots more options, so read its man page for more info.