How to get the location of a program in Ubuntu terminal?

How to get the location of a program in Ubuntu? For example I have Oracle, how to get the folder racine (location) of Oracle?


Solution 1:

Bash and Dash have the command built-in command that can show you the location of a command with the -v switch, if the command refers to an executable. For built-in commands and aliases the result is different. Examples:

$ command -v java
/usr/bin/java
$ echo $?
0
$ command -v echo
echo
$ command -v ls
alias ls='ls -h --color=auto'
$ command -v non-existing_command; echo $?
1

Additionally, all shells derived from Sh, know the type command that tells you about the nature of any command.

$ type java
java is /usr/bin/java
$ type ls
ls is aliased to `ls -h --color=auto'
$ type echo
echo is a shell builtin
$ type non-existing_command
bash: type: non-existing_command: not found

If your shell (e. g. Bash) supports it, type -a lists everything that a command may refer to:

$ type -a ls
ls is aliased to `ls -h --color=auto'
ls is /bin/ls
$ type -a echo
echo is a shell builtin
echo is /bin/echo
$ type -a touch
touch is /usr/bin/touch
touch is /bin/touch

Solution 2:

You can use which to determine which binary is being run.

  • which ssh
  • which Oracle

These are examples and would return the full path for the binaries.

You could also use whereis to locate additional information, but it may confuse you in this situation.

Solution 3:

You can also use whereis. It will show the path to the binary but also some related files like documentation:

whereis program

Solution 4:

As David Foerster already mentioned, you can use type -a which will show all the locations a given executable can be found in the active $PATH:

$ type -a now
now is /home/rick/bin/now
now is /mnt/e/bin/now

type -a will also identify if the command is a shell built-in. For example:

$ type -a test
test is a shell builtin
test is /usr/bin/test

type -a will also identify if the command is a shell keyword. For example:

$ type -a if
if is a shell keyword

type a lists programs, commands, shell built-ins and shell keywords in the hierarchal order they would be called depending on the $PATH environment variable. By changing PATH= to a different order it changes which version of the program is called. This is handy when you have production, development and test program versions on the same machine.

Program isn't in $PATH

What if the program isn't in your path? The fastest way of finding it is with the locate command:

$ locate .bashrc
/etc/bash.bashrc
/etc/skel/.bashrc
/home/rick/.bashrc
/home/rick/.bashrc~
/mnt/e/.bashrc
/mnt/e/Temporary Work/.bashrc
/usr/share/base-files/dot.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc

I kind of cheated here because .bashrc isn't a real executable, it's a "source" file to include in a bash script which is an executable. However it serves to illustrate appropriately.