Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Get the Path of a Linux Command?
A Linux command that you run from a terminal window can be built-in (a part of the system), a function (an application that runs when you enter certain commands), an alias (another name for a command), or an external executable (a program that you download). You can use the which, command, whereis, whatis, and type tools to determine what each one is and where they are located.
We will look at the which, command, type, and whereis commands as they are usually found in most Linux-based operating systems.
PATH Environment Variable
Before we get into the details of the utilities, let us first understand that applications, such as our shell (the program that runs when we type commands), search for commands in a list of folders that are stored in an environmental variable called PATH. Each folder is separated by a colon ":" symbol.
We can see what is inside this variable using the echo command
$ echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
When we install an executable file on our computer, so that we can run it from anywhere, we must ensure that the PATH environment variable includes the location of the executable.
We can temporarily change the PATH variable by typing this command
$ export PATH=$PATH:/newdir/path $ echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/newdir/path
The above created PATH will be reset when you restart your computer. We can change the PATH environment variable permanently by editing the .bashrc file and adding
export PATH=$PATH:/newdir/path
which Command
Most Linux-powered operating system distributions include the which utility. We can use this tool to determine the location of a Linux program.
$ which echo
/usr/bin/echo
When we type the echo command into our terminal window, it will execute the echo executable file located at /usr/bin/ directory.
Furthermore, the which command has an option -a which will print out all matching paths
$ which -a php
/usr/bin/php /usr/bin/php /bin/php
This shows multiple executables in different directories. The first one found in the PATH variable is used by default.
whereis Command
Let's take a quick look at the whereis (where is) utility. This tool finds out where a program is located.
If we call the utility directly, it shows us all the locations for the binary, source code, and man pages
$ whereis php
php: /usr/bin/php /usr/lib64/php /etc/php.ini /etc/php.d /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
We can use the -b parameter to display just the binaries
$ whereis -b php
php: /usr/bin/php /usr/lib64/php /etc/php.ini /etc/php.d /usr/include/php /usr/share/php
If we want to display just the manual pages, use the -m parameter
$ whereis -m php
php: /usr/share/man/man1/php.1.gz
type Command
The type command can not only show the paths of Linux commands, but it can also determine whether the targets are internal, functions, aliases, or external executables.
Let's use the type command with -p parameter to see the path of supplied Linux command
$ type -p php
/usr/bin/php
If you don't include the parameter, it will display the command definitions
$ type echo
echo is a shell builtin
If we use the -a option, it shows the command description, executable type, and the full file name
$ type -a echo
echo is a shell builtin echo is /usr/bin/echo echo is /usr/bin/echo echo is /bin/echo
We can also use type -t to show the executable type
$ type -t echo
builtin
The above command outputs builtin, as echo is a type of builtin file.
$ type -t ls
alias
Similarly, the ls command is an alias, and the following command shows that php is just a file (executable file)
$ type -t php
file
command Command
Another useful tool for finding the location of a Linux command is the command command. This tool lets us know whether we're dealing with an executable file or an alias command.
The command has two options: -v and -V, where -v gives output as a result and -V option provides output in sentence format.
$ command -v php
/usr/bin/php
$ command -V php
php is /usr/bin/php
$ command -v echo
echo
$ command -V echo
echo is a shell builtin
We must add the -v or -V parameter. If not, it will execute the Linux command that we provide
$ command ls
command filebeat-6.4.1-x86_64.rpm test.pcap config pmgo pmta4.0.zip
Comparison
| Command | Purpose | Shows Path Only | Shows Command Type | Shows Multiple Locations |
|---|---|---|---|---|
which |
Find executable location | Yes | No | Yes (with -a) |
whereis |
Find binaries, sources, manuals | Yes | No | Yes |
type |
Show command type and location | Yes (with -p) | Yes | Yes (with -a) |
command |
Test command existence | Yes (with -v/-V) | Yes (with -V) | No |
Conclusion
We can look up the location of a Linux command using the commands which, command, type, and whereis. The type command is the most comprehensive as it shows both the command type and location, while which is the simplest for finding executable paths.
