Linux Commands

Linux “Which” Command

In this guide, we will learn in-depth about the “which” command in Linux.

Prerequisites:

To perform the steps that are demonstrated in this guide, you need the following components:

  • A properly configured Linux system. For testing purposes, it’s recommended to use a Linux VM
  • Basic understanding of the command-line interface.

The “Which” Command

Any modern Linux system comes with a handful of preinstalled tools that can perform a wide range of tasks: system administration, automation, system monitoring, remote computing, and much more. The “which” command is one such tool.

The primary usage of the “which” command is to locate the location of an executable file (and scripts). Take a look at the following example:

$ which ls

 

Here:

  • We’re asking which to tell us the location of the “ls”
  • The “which” command searches for the “ls” command in PATH. Learn more about the PATH environment variable on Linux.
  • When a match is found, the location of the file is printed on the console (STDOUT).

Basic Usage

To locate the binary executable of a command/tool, use the “which” command as follows:

$ which <command_or_tool>

 

The “which” command also accepts multiple parameters. Check out the following example:

$ which ls man chmod python3

 

Here:

  • We’re asking which to locate the executable files for the “ls”, “man”, “chmod”, and “python3”
  • The output prints the location of these binaries, one line per entry.

Locating Multiple Executables

In a Linux system, there can be multiple copies of the same tool in the PATH locations. For example, /usr/bin, /usr/sbin, /bin, and /sbin have overlapping executable files:

$ ls -l /usr/bin

 

$ ls -l /usr/sbin

 

$ ls -l /bin

 

$ ls -l /sbin

 

Despite having multiple copies, whenever running a command, the shell program runs only a specific copy of the executable file (usually the one located under /usr/bin). By default, the “which” command reports this location of the “default” executable file.

However, we can instruct which to report the locations of all the matching copies of an executable file:

$ which -a shutdown

 

$ which -a shutdown chmod bash

 

Exit Codes

After running a query, the “which” command leaves an exit code behind. The value of the code indicates if the action is successful or not.

Here’s a list of all the exit codes:

  • 0: The arguments are valid and executable.
  • 1: One or more arguments were not found or non-executable.
  • 2: An invalid option is specified.

In Bash, after running any command, the exit code is stored in a variable. To view the value, use the following command:

$ which which

$ echo $?

 

$ which asdfg

$ echo $?

 

Knowing the exit codes can also be beneficial if you intend to incorporate the “which” command in your shell scripts. If you’re a beginner, check out this guide on Bash scripting for beginners.

Additional Documentation

Most of the Linux tools come with in-depth documentation that outlines all the available parameters. For more in-depth documentation, check out the man page:

$ man which

 

The PATH Environment Variable

Whenever running any query, the “which” command looks for the executable file(s) in the directories that are specified in the PATH environment variable. In this section, we have a quick look at working with PATH.

To view the content of the variable, run the “echo” command as follows:

$ echo $PATH

 

$ tr ':' '\n' <<< "$PATH"

 

To add a directory to the PATH, run the following command:

$ export PATH="<path_to_dir>:$PATH"

 

It temporarily updates the PATH variable. To make it a permanent change, you have to update it in the bashrc or /etc/environment.

Conclusion

In this guide, we discussed about using the “which” command in Linux. We demonstrated how to locate the various executable files and scripts from the PATH variable. In addition, we briefly touched on modifying the value of the PATH variable as necessary.

Interested in learning more about Linux tips and tricks? Check out the Linux commands sub-category featuring numerous concepts and tools.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.