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, types, and whereis commands as they are usually found in most Linux-Based OSs.

So let’s explore how to get the path of a Linux command in this article.

PATH Environment Variable

Before we get into the details of the utilities, let us first understand that the applications, such as our shell (the program that runs when we type commands), searches for the 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 function.

Command

$ echo $PATH

Output

/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 variables include the location of the executable.

We can temporarily change the PATH variable by typing this command

Command

$ export PATH=$PATH:/newdir/path $ echo $PATH

Output

/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 variables permanently by editing the.bashrc file.

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.

Command

$ which echo

Output

/usr/bin/echo

When we type the echo command into our terminal window, it will execute the echo executable file located at /bin/ directory.

Furthermore, the which command has an option -a which will print out all matching paths −

Command

$ which -a php

Output

/usr/bin/php
/usr/bin/php
/bin/php

We have two executables in two different directories. One is located in the /usr/bin/, and the other is located in the /opt/php8/. The former is used by default because its location appears in the PATH environment variable, and the latter has the correct permissions.

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 utilities directly, they show us all the locations for the binary, source code, and man pages −

Command

$ whereis php

Output

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.

Command

$ whereis -b php

Output

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 use -m parameter −

Command

$ whereis -m php

Output

php: /usr/share/man/man1/php.1.gz

type Command

Type commands can not only show the paths of Linux commands, but they can also determine whether the targets are internal, functions, aliases, or external executables.

Let's use type command with -p parameter to see the path of supplied Linux Command −

Command

$ type -p php

Output

/usr/bin/php

If you don't include the parameter, it will display the command definitions.

Command

$ type echo

Output

echo is a shell builtin

If we use the -a option, it shows the command description, executable type, and the full file name −

Command

$ type -a echo

Output

echo is a shell builtin
echo is /usr/bin/echo
echo is /usr/bin/echo
echo is /bin/echo

We can also use the command type -t to show the executable type.

Command

$ type -t echo

Output

builtin

The above command has output builitin, as echo is type of builtin file.

Command

$ type -t ls

Output

alias

Similarly above, the ls command is an alias file, and below command shows php command is just a file (executable file).

Command

$ type -t php

Output

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 having two option “-v” and “-V”, where “-v” gives output in just as a result and “-V” option provides output in sentence format.

Command

$ command -v php

Output

/usr/bin/php

Command

$ command -V php

Output

php is /usr/bin/php

Command

$ command -v echo

Output

echo

Command

$ command -V echo

Output

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

$ command ls

Output

command filebeat-6.4.1-x86_64.rpm test.pcap config pmgo pmta4.0.zip

Conclusion

We can look up the location of a Linux command using the commands - which, command, type and whereis. Some utilities display more information than others.

We've seen in this tutorial that there are a few caveats when using certain commands, but basically, we can use these tools to get more information about a particular Linux utility.

Updated on: 01-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements