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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

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 also use whereis. It will show the path to the binary but also some related files like documentation:

whereis program

Solution 3

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 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.

Solution 5

Shellscript what-about

I have a bash shellscript, that tries to

  • find where a program is located,
  • find the corresponding package and
  • tell what kind of program it is

Maybe use the name what-about, make it executable and put it in PATH,

#!/bin/bash

LANG=C
inversvid="\0033[7m"
resetvid="\0033[0m"

if [ $# -ne 1 ]
then
 echo "Usage: ${0##*/} <program-name>"
 echo "Will try to find corresponding package"
 echo "and tell what kind of program it is"
 exit 1
fi
command="$1"

str=;for ((i=1;i<=$(tput cols);i++)) do str="-$str";done
tmp="$command"
first=true
curdir="$(pwd)"
tmq=$(which "$command")
tdr="${tmq%/*}"
tex="${tmq##*/}"
if test -d "$tdr"; then cd "$tdr"; fi
#echo "cwd='$(pwd)' ################# d"

while $first || [ "${tmp:0:1}" == "l" ]
do
 first=false
 tmp=${tmp##*\ }
 tmq="$tmp"
 tmp=$(ls -l "$(which "$tmp")" 2>/dev/null)
 tdr="${tmq%/*}"
 tex="${tmq##*/}"
 if test -d "$tdr"; then cd "$tdr"; fi
# echo "cwd='$(pwd)' ################# d"
 if [ "$tmp" == "" ]
 then
  tmp=$(ls -l "$tex" 2>/dev/null)
  tmp=${tmp##*\ }
  if [ "$tmp" == "" ]
  then
   echo "$command is not in PATH"
#   package=$(bash -ic "$command -v 2>&1")
#   echo "package=$package XXXXX 0"
   bash -ic "alias '$command' > /dev/null 2>&1" > /dev/null 2>&1
   if [ $? -ne 0 ]
   then
    echo 'looking for package ...'
    package=$(bash -ic "$command -v 2>&1"| sed -e '0,/with:/d'| grep -v '^$')
   else
    echo 'alias, hence not looking for package'
   fi
#   echo "package=$package XXXXX 1"
   if [ "$package" != "" ]
   then
    echo "$str"
    echo "package: [to get command '$1']"
    echo -e "${inversvid}${package}${resetvid}"
   fi
   else
    echo "$tmp"
   fi
 else
  echo "$tmp"
 fi
done
tmp=${tmp##*\ }
if [ "$tmp" != "" ]
then
 echo "$str"
 program="$tex"
 program="$(pwd)/$tex"
 file "$program"
 if [ "$program" == "/usr/bin/snap" ]
 then
  echo "$str"
  echo "/usr/bin/snap run $command     # run $command "
  sprog=$(find /snap/"$command" -type f -iname "$command" \
   -exec file {} \; 2>/dev/null | sort | tail -n1)
  echo -e "${inversvid}file: $sprog$resetvid"
  echo "/usr/bin/snap list $command    # list $command"
  slist="$(/usr/bin/snap list "$command")"
  echo -e "${inversvid}$slist$resetvid"
 else
  package=$(dpkg -S "$program")
  if [ "$package" == "" ]
  then
   package=$(dpkg -S "$tex" | grep -e " /bin/$tex$" -e " /sbin/$tex$")
   if [ "$package" != "" ]
   then
    ls -l /bin /sbin
   fi
  fi
  if [ "$package" != "" ]
  then
   echo "$str"
   echo " package: /path/program  [for command '$1']"
   echo -e "${inversvid} $package ${resetvid}"
  fi
 fi
fi
echo "$str"
#alias=$(grep "alias $command=" "$HOME/.bashrc")
alias=$(bash -ic "alias '$command' 2>/dev/null"| grep "$command")
if [ "$alias" != "" ]
then
 echo "$alias"
fi
type=$(type "$command" 2>/dev/null)
if [ "$type" != "" ]
then
 echo "type: $type"
elif [ "$alias" == "" ]
then
 echo "type: $command: not found"
fi
cd "$curdir"

Demo examples

Sometimes there are two alternatives, e.g. for echo, both a separate compiled program and shell built-in command. The shell built-in will get priority and be used unless you use the full path of the separate program,

echo in 18.04.6 LTS

$ what-about echo
-rwxr-xr-x 1 root root 35000 jan 18  2018 /bin/echo
----------------------------------------------------------------------------------
/bin/echo: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0,
BuildID[sha1]=057373f1356c861e0ec5b52c72804c86c6842cd5, stripped
----------------------------------------------------------------------------------
 package: /path/program  [for command 'echo']
 coreutils: /bin/echo 
----------------------------------------------------------------------------------
type: echo is a shell builtin

echo in 22.04 LTS

$ what-about echo
-rwxr-xr-x 1 root root 35120 Feb  7 17:03 /usr/bin/echo
----------------------------------------------------------------------------------
/usr/bin/echo: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/ld-linux-x86-64.so.2,
BuildID[sha1]=b56baf290970e98b8056b1a8114a4404b8186689, for GNU/Linux 3.2.0, stripped
dpkg-query: no path found matching pattern /usr/bin/echo
lrwxrwxrwx 1 root root 7 Apr 27 10:02 /bin -> usr/bin
lrwxrwxrwx 1 root root 8 Apr 27 10:02 /sbin -> usr/sbin
----------------------------------------------------------------------------------
 package: /path/program  [for command 'echo']
 coreutils: /bin/echo 
----------------------------------------------------------------------------------
type: echo is a shell builtin

rename in 18.04.6 LTS

Sometimes a command is linked to program, that might be hidden, e.g. the version of rename that I use,

$ what-about rename
lrwxrwxrwx 1 root root 24 maj 12  2018 /usr/bin/rename -> /etc/alternatives/rename
lrwxrwxrwx 1 root root 20 maj 12  2018 /etc/alternatives/rename -> /usr/bin/file-rename
-rwxr-xr-x 1 root root 3085 feb 20  2018 /usr/bin/file-rename
----------------------------------------------------------------------------------
/usr/bin/file-rename: Perl script text executable
----------------------------------------------------------------------------------
 package: /path/program  [for command 'rename']
 rename: /usr/bin/file-rename 
----------------------------------------------------------------------------------
type: rename is /usr/bin/rename

rm in 18.04.6 LTS

I have an alias for rm in order to avoid mistakes, and the alias has priority over the program in PATH. You can prefix with backslash, \rm to skip the alias and run the program directly. (Please remember that the alias applies only for the specific user, and not for sudo and other users, unless they have defined a similar alias.)

$ what-about rm
-rwxr-xr-x 1 root root 63704 jan 18  2018 /bin/rm
---------------------------------------------------------------------------
/bin/rm: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV),
dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for
GNU/Linux 3.2.0, uildID[sha1]=864c9bbef111ce358b3452cf7ea457d292ba93f0,
stripped
---------------------------------------------------------------------------
 package: /path/program  [for command 'rm']
 coreutils: /bin/rm 
---------------------------------------------------------------------------
alias rm='rm -i'
type: rm is /bin/rm

firefox in 18.04.6 LTS

$ what-about firefox
lrwxrwxrwx 1 root root 25 jun  8 23:23 /usr/bin/firefox -> ../lib/firefox/firefox.sh
-rwxr-xr-x 1 root root 2667 jun  8 23:23 ../lib/firefox/firefox.sh
----------------------------------------------------------------------------------
/usr/lib/firefox/firefox.sh: POSIX shell script, ASCII text executable
----------------------------------------------------------------------------------
 package: /path/program  [for command 'firefox']
 firefox: /usr/lib/firefox/firefox.sh 
----------------------------------------------------------------------------------
type: firefox is /usr/bin/firefox

firefox in 22.04 LTS

$ what-about firefox
lrwxrwxrwx 1 root root 13 Jun 14 10:17 /snap/bin/firefox -> /usr/bin/snap
-rwxr-xr-x 1 root root 15815640 Apr 21 10:50 /usr/bin/snap
----------------------------------------------------------------------------------
/usr/bin/snap: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/ld-linux-x86-64.so.2, Go
BuildID=_CKxPPsUzOWnuqfKlk4n/eiKeSmbgVb16CaXm3O1l/-ofceI8uzhaCgwaTtgJt/YygjnGD2ASByLbaEMRR4,
stripped
----------------------------------------------------------------------------------
/usr/bin/snap run firefox     # run firefox 
/usr/bin/snap list firefox    # list firefox
Name     Version    Rev   Tracking         Publisher  Notes
firefox  101.0.1-1  1443  latest/stable/…  mozilla**  -
----------------------------------------------------------------------------------
type: firefox is /snap/bin/firefox

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply