Sep 23, 2023 3 min read

Linux Which Command

The Linux which command will be covered in this tutorial that is used to identify the location of the executable that is executed.

Linux Which Command
Table of Contents

Introduction

The Linux which command will be covered in this tutorial.

When you type the executable name (command) at the terminal prompt in Linux, which command is used to identify the location of the executable that is executed. The command looks in the folders listed in the PATH environment variable for the executable supplied as an input.

In this tutorial, you will understand what is which command in Linux. We will also address a few FAQs on Linux which Command.

What is PATH

PATH is a Linux environment variable that instructs the shell and other programs where to look for executable files. It consists of a list of colon-separated absolute paths to the executable folders.

Use the echo command with $PATH as an input to see the contents of your PATH variable:

echo $PATH

You will get an output like below:

Output

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

How to Use the which Command

The following is the syntax for the which command:

which [OPTIONS] FILE_NAME...

To find the complete path of the ping command, for example, type the following:

which ping

You will get an output like below:

Output

/bin/ping

You can also use the which command with many arguments:

which netcat uptime

Full paths to both netcat and uptime executables will be included in the output:

Output

/bin/netcat
/usr/bin/uptime

The search is done from left to right, and if there are several matches in the directories provided in the PATH path variable, just the first one will be printed. Use the -a option to print all matches:

which -a touch

Two complete pathways to the touch command will be shown in the output:

/usr/bin/touch
/bin/touch

Normally, one of the executables is only a symlink to the other, but in other circumstances, you might have two versions of the same command installed in different locations, or two completely different programs with the same name.

FAQs on Linux which Command

What does the "which" command display if a command is not found? 

If the "which" command cannot locate the specified command, it will not display anything. It will exit silently without any output or error message.

Can the "which" command locate aliases or shell functions? 

No, the "which" command is designed to find the location of executable files, such as binaries or scripts. It does not work with aliases or shell functions.

Can the "which" command locate built-in shell commands? 

No, the "which" command is not able to locate built-in shell commands. It only works with external executables located in directories listed in the system's PATH variable.

How does the "which" command handle multiple instances of a command? 

The "which" command displays the first instance of a command found in the system's PATH variable. If there are multiple instances with the same name, it shows the one with the highest priority.

Can I use the "which" command with wildcards? 

No, the "which" command does not support the use of wildcards. It expects an exact command name to locate.

Does the "which" command display all possible locations of a command? 

No, the "which" command only displays the first instance found based on the current system's PATH settings. To see all possible locations, you can use the "whereis" command or "find" command.

Is the "which" command case-sensitive? 

No, the "which" command is not case-sensitive. It can locate commands regardless of their case. For example, "which LS" and "which ls" will produce the same output.

Conclusion

The which command locates a command by looking for its executable in the directories supplied by the PATH environmental variable.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.