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

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
Note: Commands and their options are case-sensitive in Linux. So, be careful while using them

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


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

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

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