The which command locates the executable command or file location in the userβs environmental path. It will give you the complete path of executable command or file pointing towards in your file system.
Tutorial Details
Description | Which |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | which |
Internet Required | No |
Syntax of the Which Command
The command takes two arguments: one is the option, and the other is the executable command or file name.
$ which [OPTION] [FILENAME]
You can specify multiple executable commands or file names by using the space as a separator.
Using the Which Command
Letβs say you want to find the full path of the ls command; for that, use the which command and specify βls
β as an argument.
$ which ls
Output:
As you can see above, the full path of the ls command is β/usr/bin/ls
β in your system.
Another example,
If you want to find out the complete path of the echo command, then specify βecho
β as an argument to which command.
$ which echo
Output:
The complete path of the echo command is β/usr/bin/echo
β in your system.
Note that the which command only takes the command or file name that is executable from your terminal.
If you specify a command that is not executable, it will not print its location as shown.
$ which wrongcommand
Output:
Because the path used to search for files is located in the environmental files, if you search for files or a command that is not executable from your terminal, it will throw you an exit status code.
Using the Which Command with Multiple Arguments
You can provide multiple executable commands or files as an argument to locate their paths.
$ which ls echo
Output:
As you can see above, you can use space as a separator to get the paths of multiple commands at the same time.
Display All Pathnames with Which Command
By default, the which command will only print the active executable command or file you use to execute from your terminal.
However, if the same executable command or file exists in multiple locations, like β/bin/
β or β/usr/bin/program
β or β/usr/local/bin/program
β, you can use the β-a
β flag with an argument to display both pathnames.
Finding all paths for a single executable command or file:
$ which -a ls
Output:
Finding all paths for multiple executable commands or files:
$ which -a ls echo
Output:
Exit Status of Which Command?
The exit status of which command might come into use when you write a shell script to control the follow of your script by the exit status.
The which command has three exit statuses, followed by:
0
β All specified commands and files are found.1
β If one or more specified commands or files are nonexistent or not executable.2
β If an invalid option is specified.
Thatβs all you need to know about this command.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.