Find out current working directory of a process

eval ls -l /proc/{$(pgrep -d, COMMAND)}/cwd
This is an alternative to another command using two xargs. If it's a command you know there's only one of, you can just use: ls -l /proc/$(pgrep COMMAND)/cwd
Sample Output
$ eval ls -l /proc/{$(pgrep -d, vim)}/cwd
lrwxrwxrwx 1 splante 20000 0 Apr 14 09:25 /proc/11611/cwd -> /home/splante/p
lrwxrwxrwx 1 splante 20000 0 Apr 14 09:26 /proc/11680/cwd -> /tmp
$ sudo ls -l /proc/$(pgrep X)/cwd
lrwxrwxrwx 1 root root 0 Apr 13 18:31 /proc/3945/cwd -> /

3
By: splante
2011-04-14 13:41:58

6 Alternatives + Submit Alt

What Others Think

The above command didn't work for me. I need to add the double quotation to make it work eval "ls -l /proc/{$(pgrep -d, bash)}/cwd"
sungam · 391 weeks and 5 days ago
Interesting. It works for me without the quotes on CentOS 5.5, bash v3.2.25(1) and OpenSUSE 11.4 bash v 4.1.10(1). It also worked on zsh 4.3.10 without quotes. What shell and version didn't it work on?
splante · 391 weeks and 5 days ago
eval is not needed. Just type 'ls -l /proc/$(pgrep -d, X)/cwd'
chmurli · 391 weeks and 4 days ago
chmurli, your version works if there is one process running for the command, in which case you don't need the "-d," either. If you notice, I had mentioned that in the comments. If there is more than one process, you need the curly braces and the comma, and you need the eval.
splante · 391 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: