which Command in Linux

which Command in Linux Ubuntu, CentOS, Fedora & others

The which command in Linux is used to locate the full path of an executable file.

It searches through the directories listed in the PATH environment variable and returns the first occurrence of the specified command.

Here is the basic syntax of which command in Linux:

which -a [argument]

The command or commands you wish to locate are indicated by the [argument] variable.

Prerequisites to Use which command in Linux

Provide the options below to let this tutorial work correctly and move on.

  • A Linux VPS running a compatible Linux distribution (e.g., Ubuntu, Debian, CentOS).
  • A non-root user with sudo privileges.
  • Access to Terminal.

10 Practical Examples of which command in Linux

The knowledge of using which command in Linux is crucial for both novice and experienced Linux administrators, aiding in tasks ranging from simple command verification to complex system administration.

There is just one option for the which command:

-a: Outputs every command’s location and displays all matching paths.

For example, running the below command displays all paths where a Python executable is found:

which -a python

1. View the Exact Path of an Executable command

To locate executable file Linux on your system, simply specify the command’s name as an argument after the which command.

For example:

which ls

The path to the ls command executable file, which is stored in /usr/bin/ls, will be displayed in the output.

2. View Multiple Paths of all Executable commands

which takes in several arguments and returns the path to each in the predetermined order.

which dir diff info

After working through the provided list, the command prints the dir command, diff command, and info command results, separating each result with a new line character.

3. View All Instances List

which only displays the first match it locates in the variable directory list $PATH. For the given command, use the -a option to display all matches.

For example, you can run the command below to search for instances of the less command:

which -a less

You will get two outputs as a result. To view the file details and check if both versions are executable files, use ls command:

ls -lh /usr/bin/less
ls -lh /bin/less

4. View Symbolic Links

Listing every path that contains an instance of the designated program is possible by using the -a option.

Even though a system may have several copies of the same program, occasionally one of those instances is just a symbolic link and not a binary file.

For example, the atq command produces two instances when the following command is run:

which -a atq

You can use ls command to check the details for both files:

ls -lh /usr/bin/atq
ls -lh /bin/atq

5. Check if a Command is an Alias

which command is used to determine if a command is an alias:

which -a ll

If ll is an alias for ls -l, it won’t find any executable paths.

6. View Detailed Information

You can use which command to view additional information on a considered command.

For example, the below command gets more detailed information about echo command:

which -v echo

7. View Command Availability

which command in Linux is also used in scripts to check for command availability:

if which git >/dev/null; then
    echo "Git is installed"
else
    echo "Git is not installed"
fi

8. View Command Versions

To find a specific version of a command, you can use the which command as below:

which python3

9. View Related commands in the Path

By combining which command with other commands, you can find all commands related to that.

For example, using which command with grep as below, displays all commands related to Python in the Path:

which | grep python

10. Leave out the built-in shell

which only works with external commands, not shell built-ins like cd command or exit. It takes out the shell built-ins from its output.

For example, since read is a bash shell command, requesting the location of the read and man commands only returns the executable file path for the man command.

which read man

That’s it! Consider, which command lacks the --help option, in contrast to many other Linux commands.

So, to view the help and description of the command, use man which.

Also, you can run:

info which

What is which command Exit status?

While you learned about which command in shell scripting, consider that, using which command in a bash script requires checking its exit status:

0 – All arguments are found and executable.

1 – one or more arguments do not exist or are non-executable.

2 – When an invalid option is specified.

What is the Importance of using which command in Linux?

To know the answer, you must know what is which command in Linux.

The which command is invaluable for Linux users as it provides essential information about the location of executables on a system.

By quickly determining the exact path to a command, users can efficiently troubleshoot scripts, understand command execution, and verify the correct version of a tool is being used.

What does the which command do in Linux Ubuntu and CentOS?

The which command in Linux is used to locate the full path of an executable file.

It searches through the directories listed in the PATH environment variable and returns the first occurrence of the specified command.

How to install which command on CentOS?

The which command is used on CentOS, Fedora, and other Linux distributions.

To install which package from the default CentOS repositories, run:

sudo yum update -y
sudo yum install which -y

How to install which command on Ubuntu and Debian?

The which command is usually pre-installed on Ubuntu and Debian systems. So, run the command below to check if which is installed or not:

which which

Note: If which is installed, it will print the path to the which executable. If not, you’ll see no output.

How to check if which command is installed on Linux?

When you finish the installation, run the command below (here we use ls command) to help if which command is installed correctly:

which ls

If the output displays the full path to the used command (here, ls command), it means that the which command is installed and working correctly.

However, it typically looks like below:

/usr/bin/ls

What does the output of the which command mean?

The output of which is the full path to the executable file. This indicates where the system found the command to execute it.

Why can’t I find a command using which?

If you can’t find a command with which, it might be:

  • A built-in shell command (like cd or exit).
  • Not installed on your system.
  • Located in a directory not included in your PATH environment variable.

How do I fix a “command not found” error related to which?

For troubleshooting which command, ensure the command is installed correctly.

If it is, check your PATH environment variable to see if the command’s directory is included. You might need to add the directory to your PATH.

What is the difference between which and whereis?

To learn about difference between which and whereis, consider that while both are find commands, which searches the PATH for executables, while whereis searches for the command, its source code, and manual pages.

Conclusion

All the above-explained examples of which command in Linux help you to use this command and locate the executable file associated with a given command.

which command Linux retrieves the path to the first executable file that matches the provided command by searching through the directories specified in the user’s ”PATH” environment variable.

This command is especially helpful for debugging and comprehending how instructions are executed within the Linux operating system.

Leave a Reply

Your email address will not be published. Required fields are marked.