Linux which Command | Explained

The “which” command in Linux allows users to search for the location of a specific command on their system. This is useful for finding out which version of a command will be executed when you run it, as different versions of the same command may exist in different directories on the system.

The content of this article will comprise the following topics:

Let’s start discussing them in detail in the next section.

Basic Understanding of the which Command

One of the most basic ways of getting information about any command is by typing the command mentioned below:

$ whatis which

In the above image, the output “locate a command” means that it will give you a path of any command that you are looking for. There is another way of getting detailed information about the which command by typing:

$ man which

In the above command, the keyword ‘man’ stands for ‘manual,’ which provides all the relevant details about the command you are looking for. 

The which command depends on the PATH environment variable that contains a list of directories from where the which command searches through. After that, it will show you the path from where the command is being executed. So, to show you the list of all those paths that the command will use, you need to type:

$ echo $PATH

In the next section, we will discuss how you can utilize the which command.

How to Use the which Command in Linux

The syntax of using a which command is very straightforward, which is mentioned below:

$ which <command_name>

The syntax also shows that the primary functionality is to deal with the commands.

Finding the Path of the Command

For example, if you are looking to find the path of the ‘cat’ command, then you can do that by typing:

$ which cat

Finding the Path of Multiple Commands

You can utilize the which command to find the path of multiple commands by typing:

$ which cat touch ls

Finding All Paths using a which Command

There is one flag which is ‘-a’ that you can utilize to get all the paths of any specific command as below:

$ which -a cat touch ls

Alternate of the which Command

There is an alternate command with the name of ‘type’ which works exactly the same like the which command and its syntax is mentioned below:

$ type cat

Pro Tip: The which command doesn’t execute all those commands that are solely built for a bash shell script that can be seen in the example below:

$ which read cat

As you can see in the above image, the which command did not provide you the path of the read command as it is solely built for the bash shell script.

That’s all from the which command.

Conclusion

You can find the path of any command by using the “which” command in Linux, which thoroughly searches through the PATH environment variable. It returns the full path to the command if found, allowing users to determine which command version will be executed. In this article, we have discussed how you can utilize the which command, and we have also mentioned a similar alternative which is the ‘type’ command that you can also use.