The “which” Command in Linux [3 Practical Examples]

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 ...
Note: In the syntax above, the OPTION enclosed by square brackets means it is not mandatory and three dots after the filename means multiple filenames can be used at a time

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.

man which
Note: Commands and their options are case-sensitive in Linux.

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

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:

  1. At first, open the Ubuntu Terminal.
  2. Type the following command in the command prompt and press ENTER:
    which touch

In this given image, you can see that I have printed the location of the touch command.Locating a command using the which command in Linux.

Note: The which command does not work on Shell Built-in commands. Hence, it will not show any output for the Shell Built-in commands

Similar Readings


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.Locating multiple commands using the which command in Linux.

3. Listing All Available Locations of a Command

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 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.Listing all available pathnames of a command using the which command in Linux.

Conclusion

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?

The 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 $?:

echo $?

What is the output of the “which” command when used with the -v option?

The 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.


Similar Readings

Rate this post
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment