Linux varies from other operating systems because of Command Line Interface (CLI). If there is any piece of work that is in need of an intensive GUI method. Then you can do so by using some commands in the Linux Command Terminal.
Another functional characteristic is the choice of making any file executable. In other words, Linux does not restrict itself through file extensions. There are times when you want to know the location of an executable file. You can do so by using the in-built Which command in Linux as it finds the path of any executable. In this blog, we will cover all the fundamentals of this command and some applicable examples. Let’s dig into the topic now.
Also Read: How to reinstall python: On Different OS
Here we will understand the workings of PATH. You can call PATH a cluster of directories. Bash utilizes PATH by exploring the locations of executable programs and commands. PATH is an environment variable meaning it is a variable that has a name and value with it. The directory is the value for PATH.
If you want to know what is in your system’s PATH, then follow the steps given below:
- Firstly, you have to open the Command Terminal. You can do so by pressing the Ctrl + Alt + T keys together.
- Secondly, after the Terminal is open, you have to type the following:
$ echo $PATH
The output is as follows:
The Which Command in Linux
Following is the syntax for the Which command:
$ which <options> <file name>
The Which command has the -a option as its assigned option. When you use this option, then the Which command will produce all paths similar to the filename.
How to Use which Command in Linux?
which command can be easily bent with respect to arguments. You can utilize it for searching the paths of commands and executables very easily. Follow the steps given below in order to use the
- Firstly, press the keys Ctrl + Alt + T on your keyboard together. This will take you to the Command Terminal.
- Secondly, type the command mentioned below:
$ which <file name>
In our case, we are going to type the following:
$ which firefox
The output will come as follows:
If you want to get a comprehensive idea of the working of this command then write the command given below in the Command Terminal:
$ info which
The Output will be as follows:
Using Which with Multiple File Names
You can also utilize the
which command in searching the paths for multiple files and commands together. The user doesn’t face the issue of writing the same command over and over again. Follow the steps given below for using the
- Firstly, you have to open the Command Terminal by pressing Ctrl + Alt + T together on your keyboard.
- Secondly, type the command mentioned below if you want to find the path of multiple documents simultaneously.
$ which <file name 1> <file name 2> <file name 3>... <file name N>
Now, We will write the following:
$ which cat firefox
The Output will be as follows:
Please note that the
which command will provide the output of the path that it will first come across. When there are multiple paths, others will get ignored except for the first one. This will become troublesome in respect of multiple versions of a command.
Using Which with the -a Option
The abovementioned problem will get solved by using the
-a option. It is an acronym for “all”. It manages to produce all the paths that will guide you to a file. You can use the
which command together with the
-a option by using the steps mentioned below:
- Firstly, open the Command Terminal by pressing Ctrl + Alt + T together on your keyboard.
- Secondly, type the following command after the Terminal opens:
$ which -a <file name>
We will write the following now:
$ which -a firefox
All the directories will show now and the Output will be as follows:
Understanding the Exit Status
As you can see, using the Which command in Linux may seem trouble free but the case is different while working on bash scripts. So, it is better if you grasp the concept of the Exit Status of the command.
Let’s discuss the Exit Status for the command:
- Exit Status = 0 means that each argument of the command is executable.
- Exit Status = 1 means that among the many arguments given, one of them does not exist or is non-executable.
- Exit Status = 2 means that among many options one was unenforceable.
Sometimes in the case of paths and directories, you may have to bring out the name of a file. The basename command will help you while you are operating long paths and directories. This command will uproot the base working according to its name. So, it comes in use while examining paths and directories. If you learn how to use this command then you will be able to delete the file path or the suffix in any path. You can do so by writing just one command in the Terminal.
Conclusion on How to Use which Command in Linux
We hope that this blog on How to Use which Command in Linux helps you in understanding the Which command. Additionally, we have also given a detailed description of other related commands that will help you. So, this summarizes our blog, hope it was helpful to you. Thank you!