How to Use the which Command in Linux

Which Command In Linux

Linux gives you complete control over how you work, from customization to system management. One handy tool that can make your experience smoother is the which command. It helps you quickly find the exact location of an executable file by searching through the directories listed in your $PATH variable. In this guide, we’ll explain how the which command works, explore its syntax and options, and go through practical examples to help you use it effectively.

Understanding the which Command Syntax and Working

The which command in Linux helps find the executable file for a given command. It searches directories listed in the PATH environment variable. This command shows which file runs when you execute a command. It returns the following output:

  • 0 when all specified commands are found and executable.
  • 1 if one or more commands are missing or not executable.
  • 2 if an invalid option is used.

The general syntax to use which command in Linux is:

which [file1] [file2] ...

Replace [file1] and [file2] with the names of the executables you want to find.

Why Should We Use which Command

There are many reasons to use the which command in Linux. For example, it helps us determine if a command is an actual executable, an alias, or a symbolic link. Moreover, it displays the exact location of an executable file. It also enables us to identify missing or conflicting commands in the PATH variable. Moreover, using which command ensures the correct version runs when multiple versions exist.

How to Use Which Command in Linux

Let’s explore some examples to see how which command works in real use cases:

Example 1: Locate the Path of an Executable

We can use the which command to determine where a command’s executable file is stored:

which grep
Locate Path Of Executable

It returns the full path, such as “/usr/bin/grep”, indicating the exact location of the executable.

Example 2: Find Paths of Multiple Commands

We can specify multiple commands simultaneously with the which command to check their respective paths:

which awk sed cat touch

This command retrieves the paths of awk, sed and cat commands, respectively:

Find Path Of Multiple Commands

Example 3: Show all Instances of a Command

By default, which returns only the first match found in the PATH. However, we can execute the which command with the -a option to find all occurrences of a command:

which -a cat

This command lists all locations where cat is found in PATH:

Show All Instances

You can verify their details, by executing the ls command with the -lh option. This shows if both files are real executables or duplicates in different locations:

ls -lh /usr/bin/cat && ls -lh /bin/cat
Verify Executables

The screenshot above displays two identical commands in different locations, both 35KB in size and executable.

Example 4: Check if a Command Exists

We can use the which command to check the existence of a specific command:

which cats
Missing Commands

If the command does not exist, there is no output, as shown in the screenshot above, and the exit status is non-zero.

Example 5: Identify Symbolic Links

A system may have multiple versions of the same program, but some instances might be symbolic links instead of actual binary files. To find the symbolic links, simply run the which command with -a option:

which -a crontab
Check Symbolic Links

Next, inspect the file details with the ls command:

ls -lh /usr/bin/crontab && ls -lh /bin/crontab
Inspect File Details

If the output includes ->, it means the file is a symbolic link pointing to another location.

Example 6: Exclude Shell Built-in Commands

The which command only displays external executables and does not show shell built-in commands. It helps us identify where a program is installed on the system. For example, in the following command which only returns a path for ls since read is a built-in shell command:

which ls read
Exclude Shell Built In Commands

Which Command Limitations

The which command has several limitations that make it less reliable in certain situations. For example, it only shows external executables and does not detect built-in commands like cd or read. Additionally, it cannot differentiate between binary and symbolic links. The command strictly searches directories listed in the $PATH variable, meaning it cannot find programs stored elsewhere. Also, it didn’t provide details like file type, permissions, or version. Furthermore, if a command doesn’t exist, it does not display an error message but simply returns nothing.

To overcome these limitations, you can opt for an alternative command like whereis, type, or ls.

Where Vs. Which Command

The where and which commands both help locate files but serve different purposes. The where command is mainly used in Windows and some Unix-like systems. It finds both files and directories and lists all matches in the system’s PATH.

The which command is specific to Unix-like systems. It searches only for executable files in directories listed in the $PATH variable. By default, it returns the first match, but using which -a lists all matches.

For example, running where node command on Windows displays all locations where node is found. On Linux, which gcc returns the first path where the gcc compiler is located.

While the which command offers several features, it still lacks some capabilities. However, this can be overcome by using an alternative command, as discussed earlier. Moreover, the which command only detects executable files in your $PATH. If your script or program isn’t found, it may be missing execute permissions. Therefore, make sure your script is in a directory listed in $PATH and has execute permissions.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Anees Asghar Avatar