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
- 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]
[argument] variable specifies the command or commands you want to find.
For example, the following command outputs the location of the cat command:
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
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.
which -a touch
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
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
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.
which nc mount sort
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
which -a less
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
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
-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
Again, use the
ls command to check the details for both files. Run:
ls -lh /usr/bin/atq
ls -lh /bin/atq
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.
which read man
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.