1. Overview
A Linux command that we type into a shell can be built-in, a function, an alias, or an external executable. We can find what it is and its path with several Linux utilities such as which, command, type, locate, whatis, and whereis.
In this article, we will explore the which, command, type, and whereis utilities as these are normally found in most Linux-based operating systems.
2. PATH Environment Variable
Before we jump to the explanation of the utilities, we need to know that the application, such as our shell, finds (and executes) the command from a list of directories that are stored in an environment variable called PATH. Each directory is separated with a colon character “:”.
We can show what is inside this variable by calling the echo command:
$ echo $PATH
/home/baeldung/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
This means that when we install a program or application on our system, to be able to call the executable from any directory from our shell, we need to ensure that the PATH variable has the path of the executable.
We can update the PATH variable temporarily by running this command:
$ export PATH=$PATH:/sampledir/path
$ echo $PATH
/home/baeldung/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:/sampledir/path
The PATH will be reset on reboot.
However, we can update the PATH variable permanently by updating the .bashrc file:
export PATH=$PATH:/sampledir/path
3. which Command
Most Linux-based operating systems have the which command installed. We can use this command to get the path of a Linux command:
$ which docker
/usr/bin/docker
This shows that when we call the docker command, it will run the Docker executable file in the /usr/bin/ directory.
Moreover, the which command has a parameter -a which will print all matching path names:
$ which -a docker
/usr/bin/docker
/bin/docker
So we have two executable files in two different directories. The shell uses the one in the /usr/bin/ directory because the directory appears first in the PATH variable, and the file has the right permissions.
Otherwise, it will go to the next executable file in the /bin/ directory:
$ echo $PATH
/home/tenzin/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:/sampledir/path
4. command Command
The command command is another utility that we can use to find the path of a Linux command.
This utility tells us the difference between an executable (docker) or an alias (ls):
$ command -v docker
/usr/bin/docker
$ command -V docker
docker is hashed (/usr/bin/docker)
We need to pass the -v or -V parameter:
$ command -v ls
alias ls='ls --color=auto'
$ command -V ls
ls is aliased to `ls --color=auto'
Otherwise, it will run the Linux command that we supply:
$ command ls
Android Downloads Pictures Templates
AndroidStudioProjects file123.txt 'Screenshot from 2021-06-14 16-11-31.png' Videos
...
5. type Command
The type command can not only show the path of a Linux command, but it can also tell if the target is built-in, a function, an alias, or an external executable.
Let’s show the path of a Linux command:
$ type -P ls
/usr/bin/ls
Without the parameter, it will show the command definition:
$ type ls
ls is aliased to `ls --color=auto'
If we use the -a parameter, it shows the command definition, executable type, and its path:
$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
We can also use type -t to display the executable type:
$ type -t which
file
$ type -t command
builtin
$ type -t type
builtin
$ type -t whereis
file
$ type -t ls
alias
$ type -t docker
file
6. whereis Command
Finally, let’s take a look at the whereis command. This command locates the path of the binary, source, and manual page of a given command.
If we call the utility directly, it shows all the locations of the binary, source, and manual page:
$ whereis docker
docker: /usr/bin/docker /etc/docker /usr/libexec/docker /usr/share/man/man1/docker.1.gz
In addition, we can use -b parameter to show just the binary:
$ whereis -b docker
docker: /usr/bin/docker /etc/docker /usr/libexec/docker
Moreover, if we want to show just the source (which does not exist on this system):
$ whereis -s docker
docker:
If we want to show just the manual:
$ whereis -m docker
docker: /usr/share/man/man1/docker.1.gz
7. Conclusion
We can use the utilities – which, command, type, and whereis to find the path of a Linux command. Some utilities show more information than others.
As we have seen in this tutorial, there are a few caveats with some utilities, but simply put, we can use these four utilities to get more information about the Linux command we want to use.