In this tutorial, you will learn to use the which command. 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.
Prerequisites
- A system running Linux
- Access to the terminal (Ctrl + Alt + T)
Linux which Command Syntax
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 catOptions
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.
For example:
$ which -a touchExit 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 trFind 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 sortFind 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. 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
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.












