which

Find and display the absolute path of the given command

Supplementary instructions

which command is used to find and display the absolute path of a given command. The directory that needs to be traversed when searching for the command is saved in the environment variable PATH. The which command will search for files that meet the conditions in the directory set by the environment variable $PATH. In other words, using the which command, you can see whether a certain system command exists and where the command is executed.

grammar

which(option)(parameter)

Options

-n<file name length>: Specify the file name length. The specified length must be greater than or equal to the longest file name among all files;
-p <file name length>: The same as the -n parameter, but the <file name length> here includes the path of the file;
-w: Specify the width of the field during output;
-V: Display version information.

Parameters

Instruction name: list of instruction names.

Example

Find files and display command paths:

[root@localhost ~]# which pwd
/bin/pwd

[root@localhost ~]# which adduser
/usr/sbin/adduser

Description: which searches for executable files based on the directory in the PATH variable configured by the user! Therefore, the commands found by different PATH configuration contents are of course different!

Use which to find cd

[root@localhost ~]# which cd
cd: shell built-in command

The commonly used command cd cannot be found! why? This is because cd is a built-in command in bash! But which defaults to looking for the directory specified in PATH, so of course it cannot be found!