Which Command in Linux

Knowing the exact location of different programs is vital for various activities in your Linux life. With the “which” command, you can quickly get the path of a given executable in Linux. The “which” utility comes installed on all Linux systems, and knowing how to use it is essential.

Hopefully, after reading this post, you will have an answer and understand why you need the “which” command in Linux and how to use it. Let’s begin!

What Is the Which Command in Linux

“Which” is one of the many commands that you will find and use in Linux. It is used when you want to know the path of a command. Ideally, you may get a case where you have different versions of a binary executable or don’t know where a given command is stored.

When you have such a case, using the “which” command helps you get the exact path for the target binary executable. Moreover, if you want to check if there are different instances of a given command or if you are dealing with its symbolic link, using the “which” command saves the day.

With Linux, how a given binary executable appears in the PATH environment determines how it gets executed. So, if you have different versions of a given executable, how it appears in the PATH environment variable determines which version gets executed when you call that binary executable.

For instance, if you have two Java versions installed, their sequence in the PATH environment variable determines which version you will use. To check this sequence and select which version to use, that’s when you need the “which” command.

How to Use the Which Command in Linux

The “which” command is straightforward and only has one option. The following is its syntax:

$ which [option] [command]

You can optionally add the “-a” option and then specify the target command in which you want to check its path. For instance, if you want to check the actual path of the “man” command, execute your command as follows:

Once you run the command, it gives a path as the output. That way, you can note that the man executable binary is located in the “/bin” where all binaries are stored.

Note that the previous output doesn’t give other instances of the same command. Sometimes, you could have different instances of the same binary. Think of it like having different versions of a given binary. To access all the paths for the target binary, add the “-a” option.

Here’s an example where we check the “ip” command:

The following output shows that the “ip” binary has four locations. So, if you want to write a script that requires you to specify the binary path, you would know which path to use based on what you get as the output.

When executing the “which” command, you can specify multiple commands to find their exact path. For that, add the commands separated by spaces. The following example shows how checking more than one executable binary works.

The output shows the path for each of the specified commands.

Similarly, checking if the specified commands have other paths is possible. Add the “-a” option, and your output will show all the available paths for each specified command. Take a look at the following example:

You will see that each command’s path is listed before moving to the next command.

That’s how you can quickly utilize the “which” command in Linux to get the path for any command that you want to check.

Conclusion

The “which” command is handy, especially when scripting, as it lets you get the exact path for a given program. We discussed what the “which” command is and why you need it. Moreover, we’ve given a few examples on how the command works. With that, we hope that you understood the Linux “which” command.

source

Leave a Comment