Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I type a name of some executable in command line and it starts, how can I detect path where that executable is located?

share|improve this question
2  
I guess your question is answered here already: serverfault.com/questions/232762/… –  obeliksz Jul 3 '13 at 7:45
add comment

2 Answers 2

up vote 3 down vote accepted

You can find out which executable will be started by using the command

which <command>

If you want to determine the path programmatically you're on the wrong stack exchange.

share|improve this answer
3  
If you pass option -a, all possible matches are displayed. And there is also whereis which searches for manuals and sources, too. –  scai Jul 3 '13 at 8:43
add comment

The problem of the simple solution

whichcommand finds the executable file of the given command in your $PATH. Unfortunately this will not work as expected in all cases.

When command is an alias or a function, you will not get a correct result because the actual executable being run could have a completely different name. For example many Linux distributions have the ll alias set by default. which ll does not find anything. Another problematic case is when your shell behaves in a non-standard way and does not run the first command found in the $PATH.

The more reliable solution

On Linux if you want to be completely sure about the path of the executable file of a running process then you can get it from its PID (Process ID; in the example below put the number instead of $PID):

readlink /proc/$PID/exe

How to get the number? PID of the last process run in shell in background is $!. Small demonstration:

sleep 20 &
readlink /proc/$!/exe

You can get PIDs of other processes for example from a process list using ps -ef.

Explanation: /proc/$PID/exe is a symbolic link to the executable file of the running process. The command readlink shows the target the symbolic link is pointing to.

Scripts are a special case

Unfortunately the readlink /proc/$PID/exe method does not work as expected with scripting languages like bash, Perl, Python etc. In such cases you will get the path to the interpreter of the script (like /bin/bash) instead of the path to the script. To get closer to the information you can use:

cat /proc/$PID/cmdline     # the commandline with the interpreter and script
readlink /proc/$PID/cwd    # the current working directory

Unfortunately the current working directory can change since the program start. Some interpreters like bash can keep the script file open on descriptor 255:

readlink /proc/$PID/fd/255

Everything can change

Any process can call exec() which replaces its /proc/$PID/exe, /proc/$PID/cmdline and other information.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.