How to Use which Command in Linux

Wondering how to use which command in Linux?

What makes Linux different from other operating systems is the ability of its Command Line Interface (CLI). Any task that requires an intensive GUI method can be achieved in a few lines of commands written in the Linux Command Terminal.

Another useful feature is the option to make any file executable. Linux is not limited to restrictions via file extensions, allowing any file to become executable. Oftentimes you might find yourself in a situation where you’re left wondering about the location of an executable file. It may be to make changes or just out of curiosity. 

Whatever the reason, there is an in-built command that is there to solve your problem. The which command in Linux is responsible for looking up the path of any executable.

If you’re someone who wishes to learn how to use which command in Linux, then this guide is meant for you. We’ll cover the basics of the command, its syntax, and some relevant examples.

So without further ado, let’s begin!

Understanding PATH.

Before we learn how to use which command in Linux, it is important to understand how PATH works.

PATH can be seen as a collection of directories. Bash uses it to search for the locations of executable programs and commands. A PATH can be seen as an environment variable i.e., a variable with a name and a value associated with it. The value for PATH is the directory.

To see what’s included in your system’s PATH, follow these steps:

  1. First of all, open the Command Terminal by pressing the Ctrl + Alt + T keys.
  2. Once the Terminal is open, type the following:
$ echo $PATH

The output should be similar to the one shown in the image below.

which Command in Linux

Now that you’ve learned how to use PATH, you are ready to learn about the which command.

The which Command.

As stated earlier, the which command is responsible for searching the path of any executable program, script, or command.

The syntax for the which command is given below:

$ which <options> <file name>

The goto option for the which command is the -a option. Using this option will make the which command print out all paths that match the filename.

Using the which Command.

The which command is very flexible when it comes to arguments. It can be used to find the paths of executables and commands. 

To use the which command, follow these steps:

  1. To begin, press Ctrl + Alt + T on your keyboard to open the Command Terminal.
  2. Next, type the following:
$ which <file name>

In our case,

$ which firefox

The output should be similar to the one in the image below.

which Command in Linux

As you can see, the which command outputs the exact path to the file. To get a detailed idea of the functionality of this command, type the following in the Command Terminal:

$ info which

The contents of the output should be similar to the image below.

which Command in Linux


Using which with Multiple File Names.

The which command can also be used to find the paths for multiple files and commands simultaneously.

This saves the user from the hassle of writing the command more than once.

To use the which command, follow these steps:

  1. Firstly, open the Command Terminal. To do so, press Ctrl + Alt + T on your keyboard.
  2. Next, type the following command to search for the path of multiple files at the same time.
$ which <file name 1> <file name 2> <file name 3>... <file name N>

In our case:

$ which cat firefox

You should see an output similar to the image below.

which Command in Linux

It should be noted that the which command will output the first path that it finds. In case there are multiple paths, all but the first one will be ignored. This can be problematic in the case of multiple versions of a command.

Using which with the -a Option.

To counter the aforementioned problem, the -a option would prove to be useful.

The -a option stands for “all”. It is responsible to print all the paths that lead to a file.

To use the which command with the -a option, follow these steps:

  1. Start by navigating to the Command Terminal. You can achieve this with the help of the search bar or by pressing Ctrl + Alt + T on your keyboard.
  2. Once the Terminal opens, type the following:
$ which -a <file name>

In our case:

$ which -a firefox
ubuntu

This should display all directories, and your output should look similar to the one in the image below.

Understanding the Exit Status.

Working with the which command may seem easy, but in case you’re using it when working on bash scripts, it is a good idea to learn about the Exit Status of the command.

The Exit Status for the command is as follow:

  • Exit Status = 0 implies that every argument of the command was found to be executable.
  • Exit Status = 1 implies that one of the given arguments is not executable or doesn’t exist.
  • Exit Status = 2 implies that one of the options was unenforceable.

Talking about paths and directories, there may be times when you’d want to extract the name of a file. If you’re working with long paths and directories just to get to the command of your choice, the basename command could be of help. 

As the name suggests, this command extracts the base name and is, therefore, very useful when it comes to dealing with paths and directories. By learning how to use the basename command, you can remove the file path or the suffix in any path by typing a single command in the Terminal.

This concludes our guide on how to use which command in Linux. Feel free to tell us your questions or suggestions in the comment section below.

If this guide helped you, please share it. 🙂

Author

Leave a Reply

Your email address will not be published. Required fields are marked *