Read in 2 minutes
Linux Which Command
In this tutorial, we will cover the Linux
which command is used to identify the location of a given executable that is executed when you type the executable name (command) in the terminal prompt. The command searches for the executable specified as an argument in the directories listed in the PATH environment variable.
What is PATH
PATH is an environmental variable that tells the shell and other programs which directories to search for executable files. It consists of a list of colon-separated absolute paths to directories containing the executables.
To view the contents of your PATH variable, use the echo command with
$PATH as an argument:
The output will look something like below:
How to Use the which Command
The syntax for the
which command is as follows:
which [OPTIONS] FILE_NAME...
For example, to find the full path of the ping command, you would type the following:
The output will be something like this:
You can also provide more than one arguments to the
which netcat uptime
The search is done from left to right, and if more than one matches are found in the directories listed in the
PATH path variable,
which will print only the first one. To print all matches, use the
which -a touch
The output will show two full paths to the touch command:
Usually one of the executables is only a symlink to the other one, but in some cases, you may have two versions of the same command installed in different locations or totally different commands using the same name.
which command is used to locate a command by searching the command executable in the directories specified by the environmental variable