Which command in Ubuntu

Using Which Command in Ubuntu

Pranav Krishna
Pranav Krishna

Table of Contents

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:

which apt

For which the output might be this:

wihch apt (displays the path where the executable is located)
The output of which apt (displays the path where the executable is located)

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 $PATH.

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 -a with 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)
No output is generated if an argument is not an executable file.

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
output of 'which' for multiple arguments
Output for multiple arguments

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]
All occurrences of the ip command using `which -a ip`
All occurrences of the ip command

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.

Output of which ll (Ubuntu at the top - none, Fedora at the bottom - alias)
The output of which ll (Ubuntu at the top - none, Fedora at the bottom - alias)

If you're into aliases, use the 'type' command.

Also, the newer versions of 'which' command from other distributions can print its version:

The 'which' command prints its version (missing in Ubuntu)
The 'which' command prints 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 echo $?).

Exit Codes in Linux [Explained]
Unraveling the mystery of exit codes in Linux. Learn what are the exit codes and why and how they are used.
Code Description Example
0 Ran successfully Able to locate all the filenames given
1 Exited with failure Filename not found (even one failure returns code 1)
2 Wrong usage of command Using illegal options (-v, etc)

When the command runs successfully, it returns a zero as the exit code (all found)

Exit code 0 (all successful)
Exit code 0 (all successful)

Even if one filename can't be located, exit code 1 (failure to locate all arguments) is returned.

Exit code 1 (at least one failure)
Exit code 1 (at least one failure)

As already known, invalid options return exit code 2. In Ubuntu, the options supported is just -a.

Exit code 2 (invalid options)
Exit code 2 (invalid options)


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.

Pranav Krishna

Pranav Krishna

This is Pranav Krishna, a Proud Linux user in twelfth grade.