The "which" command is used to find the location of the file being executed. Its purpose is to print the complete path of the executable binary of the given command.
As an example, let's locate the 'apt' package manager:
For which the output might be this:
Let's learn more about the behaviour of which command.
The 'which' Command
The 'which' command can take in multiple arguments and locate each one of the executable files from the folders specified in
It works by searching for the executable file in the folders present in the
$PATH variable and prints the path of the first occurrence as output.
Its syntax is as follows:
which [options] [arguments]
- options - The only option is
-awith Ubuntu ('all' tag), which is used to print all possible locations of the arguments
- arguments - These are the names to be checked by the command (outputs path if it's an executable, otherwise not)
Examples of which command
If you did not know, every command you run in the terminal is just an executable file present in an accessible folder. Here are some examples:
which apt sudo exa ip
Those are the first occurrences of each command listed one after another.
Here, three arguments (sudo, ip, apt) are recognised executable files whose locations are printed. But, one of them (exa) does not refer to an executable, or the command cannot find its path. Hence, there are only three outputs instead of four.
If the command you're searching for can't be found, you can add its parent directory to the
$PATH folder list.
Printing all occurrences
Some commands have multiple implementations and the binaries can be located in multiple folders. By default, which command will only show the first such occurrence.
To print every instance of the entered argument(s), you can use the
-a option before the name.
which -a [args]
More features missing in Ubuntu's version?
While experimenting with this command, I learnt that Ubuntu's version is underpowered when compared to what is shipped with other distributions.
In Fedora's version, this command can look up aliases too.
If you're into aliases, use the 'type' command.
Also, the newer versions of 'which' command from other distributions can print its version:
Exit codes of which command
It's important to know what exit code this command returns in particular situations so that it can be used in scripts.
The exit codes for different situations are given below (which you can check by using
||Ran successfully||Able to locate all the filenames given|
||Exited with failure||Filename not found (even one failure returns code 1)|
||Wrong usage of command||Using illegal options (-v, etc)|
When the command runs successfully, it returns a zero as the exit code (all found)
Even if one filename can't be located, exit code 1 (failure to locate all arguments) is returned.
As already known, invalid options return exit code 2. In Ubuntu, the options supported is just
I hope you have learned how to use which command from this quick tutorial. It's a simple command that can locate certain files in the specified paths. Do let me know how you use this command in your scripts.