How does one find the command path in linux?
In linux, we often use commands such as cd, grep, and vi. Sometimes we need to find the location of these commands. How can we do this?
In linux, there are two commands to complete this function:Which, whereis
Which is used to view when
The path of the command to be executed.
Whereis is used to view the path of a command or file,
Principle of the which command: In the PATH specified by the PATH variable, search for the location of a system command and return the first search result. That is to say, by using the which command, you can see whether a system command exists and where the command is executed.
Use instance of the which command:
$ Which grep
Whereis command principle: it can only be used for searching program names, and only binary files (parameter-B), man description files (parameter-m), and source code files (parameter-s) are searched ). If the parameter is omitted, all information is returned.
Examples of using the whereis command:
$ Whereis grep
The following is an example. Added multiple versions of java to your linux system. If you directly press "java-version" on the command line, you will get a result. However, do you know which path of java is being executed? If you want to know, you can use the which command:
Which java
The returned result is the first JAVA location in the PATH, that is, the default JAVA Command Execution location.
If you use the command: whereis java
You will get a lot of results, because this command lists all the paths containing java (files or folders.