The which command in Linux returns the pathname of a file in the given environment. It takes command/s as arguments and searches for the corresponding name in the $PATH environment variable of executable files. In this article, I will present you various applications of the which command with practical examples.
A. Description
The which command can locate a command if it is passed as an argument.
B. Syntax
The syntax of the which command is very basic. Below is the syntax for the command.
which [OPTION] filename ...
C. Options
The which command has only one option. You will find the description for this option below.
- -a: Prints all the locations of each command.
You can learn more about the which command by checking its man page.
man which
Practical Examples of the “which” Command in Linux
There are multiple ways of using the which command to find the pathname of a command. In this section, you will find some practical examples of which command.
Example 1: Locating a Single Command Using the “which” Command in Linux
Using the which command you can locate any specific command. In this example, I will show you the location of the touch command. You can do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
which touch
➌ Now, press the ENTER button.
Output >
In this given image, you can see that I have printed the location of the touch command.
Similar Readings
- The “man” Command in Linux [6 Practical Examples]
- The “history” Command in Linux [6 Practical Examples]
- The “cal” Command in Linux [7 Practical Examples]
- The “bc” Command in Linux [10+ Practical Examples]
Example 2: Locating Multiple Commands Using the “which” Command in Linux
You can find the location of multiple commands using the which command. To do so you will just need to provide the list of commands after the which. In this example, I will find out the locations of the date, touch, and wget commands. You can follow the steps below to do the same.
Steps to Follow >
➊ At first launch the Ubuntu Terminal.
➋ Type the following command in the command line:
which date touch wget
➌ Press the ENTER button.
Output >
In the image below, you can see that I have shown the location of all the desired commands.
Example 3: Listing All Available Locations of a Command Using the “which” Command in Linux
Using the which command with the -a option, you can display all the pathnames of a command. In this example, I will show you all the locations of the touch command. You can do the same by following the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
which -a touch
➌ Hit the ENTER button.
Output >
In the given image, you can see that I have printed all the locations of the touch command.
Conclusion
In this article, I have tried to illustrate the frequent uses of the which command in Linux. The examples will help you to find locations of one or more commands at a time. I hope you had fun learning these practical examples that might lead you to become a power user of Linux.
Similar Readings
- The “neofetch” Command in Linux [12+ Practical Examples]
- The “tree” Command in Linux [14 Practical Examples]
- The “whatis” Command in Linux [12 Practical Examples]
- The “whereis” Command in Linux [10 Practical Examples]
- The “at” Command in Linux [7 Practical Examples]
- The “tee” Command in Linux [4 Practical Examples]