When we type a command in the termial, the shell checks your PATH to find an executable with that name. There are often several directories in your PATH. This makes it hard for you to know where the file is located. The which command in Linux can help you locate that executable file. As a system administrator, it can be one of many helpful tools in your toolbox. The which command comes pre-installed on all of the Linux operating systems. In this article, we will explain how the which command works and how we can use it to locate the executable files of any command we run in the terminal.

The Which Command and Your PATH

In the following section we will explain a little about what happens when we run a command in a Linux shell. It will help you to understand how the which command works.

A command is basically a path to an executable file. When we execute any command in the Linux Bash shell, the shell searches for it in each of the directories listed in the $PATH environment variable. If you view the $PATH environment variable, you will see the different paths separated by colons.

[email protected]:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin

Let us look at an example. If you run the sync command in the Bash shell, the shell starts searching in each of the paths specified in the $PATH environment variable. It begins its search process in the left to right order. If it finds any executable file associated with the sync command it stops searching, adds it to the hash table, and executes that file. If it does not find an executable file in any of the directories it returns “command not found” error.

Basic Syntax of Which Command

Here is the basic syntax of which command:

$ which [option] [command1] [command2]

Using the Which Command

Show Full Path of a Specific Command

To check which files are executed when we run a specific command in the shell, use the which command as follows:

$ which [command]

We can use this check the path of the file that is executed by the shell when we run the sync command:

[email protected]:~$ which sync
/usr/bin/sync 

You can see that the shell has found the executable file of the sync command in the second path (i.e. /usr/bin) from those listed in the $PATH environment variable. Therefore, it stopped its search process and displayed the path.

Show All Possible Paths of a Specific Command

There are some cases, where the executable file is placed in multiple locations or you have different instances of the same program installed. If you execute a command, then the shell will execute the first instance it finds in your $PATH. In turn, if you run the which command, it will only display the first location it finds, even if there are more matches.

To view all the matched locations, you can use the -a switch like this:

$ which -a

Which Command Example 1:

[email protected]:~$ which -a sync
/usr/bin/sync
/bin/sync

This command has returned two paths for executable files. In this case, one of the paths is just a symbolic link (symlink) to the other.

Which Command Example 2:

Let’s take a look at another example that I experienced in my system. When I run which -a command with pwsh, it returns multiple locations for executable files. It happens because I have installed two instances of PowerShell in my system; one using the “Snap package” and others using the “tar.gz package”.

[email protected]:~$ which -a pwsh
/usr/bin/pwsh
/bin/pwsh
/snap/bin/pws

At this point, you might be thinking which of the above path will be executed when we run the pwsh command in shell. It all depends on the directory which appears first in the $PATH environment variable, in this case /usr/bin/.

Locate Executable Files of Multiple Commands

You also view the executable files of multiple commands at once using the following syntax:

$ which [command1] [command2]

Example:

[email protected]:~$ which ip vim zip
/usr/sbin/ip
/usr/bin/vim
/usr/bin/zip

The which command only displays path info for the executable commands not for the built-in commands. If you run the which command with the built-in commands as arguments, like cd, history, and type, you will not receive any output.

[email protected]:~$ which cd history type
[email protected]:~$

NOTE: If a file exists in your PATH but is not set to executable, the which command will not display it. It will only show executable files.

Conclusion

So there you have it, the basics of the which command and how it relates to your $PATH. It is a fairly simple command and we covered the common options. For more in-depth information read the man page linked below. As usual, if you have any comment or questions please use the comment form at the bottom of the page!

Resources and Links