How to Use the which Command in Linux

July 21, 2022

Introduction

The which command allows users to search the list of paths in the $PATH environment variable and outputs the full path of the command specified as an argument. The command works by locating the executable file matching the given command.

In this tutorial, you will learn to use the which command.

How to use the which command in Linux, with examples.

Prerequisites

  • A system running Linux
  • Access to the terminal (Ctrl + Alt + T)

Linux which Command Syntax and Options

The syntax for the which command is:

which -a [argument]

Arguments

The [argument] variable specifies the command or commands you want to find.

For example, the following command outputs the location of the cat command:

which cat
Find the location of the cat command executable file using which.

Options

The which command has only one option, -a. It is optional and used to print all the matches it finds.

The command searches for matches from left to right. If there are multiple matches found in the directories listed in $PATH, which prints only the first one. The -a option instructs which to print all the matches.

Important: On many Linux distributions, which excludes the shell built-in commands and does not output their location.

For example:

which -a touch
List all instances of a command using which.

Having multiple matches sometimes means one match is a symlink to the other. However, it is possible to have two versions of the same command in different locations or two different commands using the same name.

Note: Unlike many other commands, which has no --help option. To see the command description and help, run man which.

Exit Status

The which command returns one of the following values that indicate its exit status:

  • 0. All arguments were found and executable.
  • 1. One or more arguments don't exist or aren't executable.
  • 2. An invalid option has been specified.

Linux which Command Examples

The following examples showcase how the which command works and how to use the available option.

1. Display the Path of Any Executable File

To display the path of any command, pass the command name as an argument after which.

For example:

which tr
Find the tr command executable with which.

The output shows the path to the tr command executable file, located in /usr/bin/tr.

2. Display Multiple Paths of Executable Files

which accepts multiple arguments and outputs the path to each one in the specified order.

For example:

which nc mount sort
Find the paths to multiple commands using which.

The command works through the supplied list and outputs the results for the nc command, mount command, and sort command, separating each result with a newline character.

3. List All Instances

which only shows the first match it finds in the $PATH variable directory list. Use the -a option to show every match for the specified command.

For example, searching for instances of the less command outputs two results when using the -a option:

which -a less
Display the path to all instances of a command.

Use the ls command to check file details and determine if both versions are executable files. Run:

ls -lh /usr/bin/less
ls -lh /bin/less
Check the details of a command's executable file using the ls command.

The output shows two identical versions of the same command in two locations, both 176 KB large, and both executable.

Note: The /bin directory contains executables that can be used by the system administrator and any other user, and which are required for emergency system repairs. The /usr/bin directory is the primary directory for executable commands on the system.

4. Find Symbolic Links

Using the -a option lists all the paths containing an instance of the specified program. While multiple versions of the same program can exist on a system, sometimes one of the instances is only a symbolic link and not a binary file.

For example, running the following command outputs two instances of the atq command:

which -a atq
Find all instances of the atq command.

Again, use the ls command to check the details for both files. Run:

ls -lh /usr/bin/atq
ls -lh /bin/atq
Finding symbolic links using the ls command.

The output shows that both files are symbolic links (->) only 2 bytes large and pointing to the at command.

5. Exclude Shell Built-ins

As previously mentioned, the which command excludes shell built-ins from its output.

For example, asking for the location of the read and man commands only outputs the location for the man command executable file, as read is a bash shell command.

which read man
The which command excluding a shell built-in from its output.

Conclusion

This tutorial showed how to use the which command in Linux to find the path to a command's executable binary. See and download our Linux commands cheat sheet for other essential Linux commands and examples of using them.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Use the locate Command in Linux
January 25, 2022

The locate command is a utility that allows users to quickly search for files or directories in Linux. This tutorial shows how to install and effectively use the locate command.
Read more
How to Find Files in Linux With the find Command
August 29, 2024

The find command is a useful command-line tool in Linux. This tutorial shows how to use the find command to locate and manage files in Linux.
Read more
How to Use the nslookup Command
January 13, 2022

This tutorial will introduce nslookup, a cross-platform command for querying servers and obtaining domain records.
Read more
Chown Command: Change Owner of File in Linux
October 3, 2024

The chown command changes user ownership of a file, directory, or link in Linux. This tutorial shows how to change user ownership of a file, directory, or link in Linux.
Read more