Linux which command
The Linux which command is used to locate the executable files or location of a program from the file system. It displays the path where the specified file or command is stored.
If you are curious to know where the specified program is stored, which command will help you to identify the path, it is quite straightforward to use.
The above command will display the path of specified filename or command.
The which command facilitates with only one option that is the '-a' option. It is used to display all the paths for the specified command. In some cases, our machine contains two different paths for the same program. For example, it can be "/usr/bin/program" or "/usr/local/bin/program". The '-a' option will display both the paths for the specified command.
Let's explore various usage of this command to better understand it:
How to use which command?
To display the path of any executable files such as programs and commands, simply execute the which command followed by the program name.
For example, to display the path of node executable files, execute the command as follows:
It will display the path of node executable files. Consider the below output:
Similarly, for Java executable files, execute the command as follows:
It will display the path of Java executable files.
Which command only displays the path of executable files, so execute commands only with the arguments available in the machine. If we pass any illegal argument (which our machine doesn't contain), it will not give any output.
Which command with multiple arguments
We can display multiple paths of executable files by providing multiple arguments at a time in a single command. To pass the multiple arguments in which command, separate them by a space. Execute the command as follows:
For example, to display the path of the node, cat, ls, and mkdir command, execute the command as follows:
The above command will display the path for all the specified commands in a new line. Consider the below output:
Exit status of which command
The Linux user has an exit status, or it is executed by the shell script. If the exit status is 0, it means the command is successfully executed without any error. If the exit status is the non-zero number (i.e., 1 to 255), it means the command had an error and was a failure.
The which command has the following three exit status:
0: If the exit status is 0, it means all arguments are found and executable.
1: If the exit status is 1, it means one or more arguments are non-existent or non-executable.
2: If the exit status is 2, it means an invalid option is specified.
If you stuck during using of which command, you can take help from your terminal by displaying the manual of the which command.
To display the manual, execute the man command as follows:
The above command will display the manual. Consider the below output:
To exit from the manual press 'q' key.
We can also execute the info command to display the manual:
The above command will display the same output as the man command.