The which command in Linux returns the pathname of a file in the given environment. It takes commands 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 3 practical examples.
Linux “which” Command Syntax
The syntax of the which command is:
which [OPTION] filename ...
The “which” Command 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.
3 Practical Examples of the “which” Command in Linux
There are multiple ways of using the
which command to find the pathname of another specified command, and, this section provides you with 3 specific examples.
1. Locating a Single Command
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:
- At first, open the Ubuntu Terminal.
- Type the following command in the command prompt and press ENTER:
In this given image, you can see that I have printed the location of the touch command.
- 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]
2. Locating Multiple Commands
You can find the location of multiple commands using the
which command. To do so, you just need to provide the list of commands after which. In this example, I will find the locations of the date, touch, and wget commands. You can run the below command to do the same:
which date touch wget
In the image below, you can see that I have shown the location of all the desired commands.
3. Listing All Available Locations of a Command
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 running the below command:
which -a touch
In the given image, you can see that I have printed all the locations of the touch command.
In this article, I have tried to illustrate the frequent uses of the command
which. 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.
Frequently Asked Questions
What does the Linux “which” command do?
which command in Linux locates the executable file associated with the command given as an argument after
which itself. It allows users to search for the list of paths in the environment variable $PATH and returns the full path of the specified command. For instance, the command
which cat returns the path /usr/bin/cat as the final result on the prompt.
What is the exit status of the “which” command?
The exit status in Linux is a numerical value a command returns once it completes its execution. It denotes whether the command runs with success or throws any unwanted error.
The exit status of the
which command has 3 values where:
- 0– all arguments were found and executable.
- 1– means one or more arguments aren’t found or aren’t executable.
- 2– an invalid option has been specified.
To check the exit status of the which command after its execution, run the echo command with the special variable $?:
What is the output of the “which” command when used with the -v option?
which command displays additional information when used with the
-v option. For instance, running the command
which -v echo displays detailed information on the echo command since -v acts as a verbose output flag.
- 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]