We often have to look for a file in
linux, but we don’t know where it is, we can use some of the following commands to search for it.
whichto see the location of the executable file.
whereisto see the location of the file.
locateworks with the database to see the location of the file.
findto actually search the hard disk for the file name.
The purpose of the
which command is to search for the location of a system command in the path specified by the PATH variable and return the first search result. In other words, using the
which command, you can see if a system command exists and exactly where it is executed.
1. Command format.
which executable file name
2. Command function.
which command will search for the location of a system command in the path specified by the PATH variable and return the first search result.
3. Command parameters.
-n specifies the file name length, the specified length must be greater than or equal to the longest file name among all files. -p is the same as the -n parameter, but here the path to the file is included. -w Specifies the width of the field for output. -V Display version information
4. Examples of use.
Example 1: Find files, display command paths
Output: `which lsmod
[root@localhost ~]# which pwd /bin/pwd [root@localhost ~]# which adduser /usr/sbin/adduser [root@localhost ~]#
which searches for a runnable file based on the directory in the PATH variable configured by the user! So of course, different PATH configurations will find different commands!
Example 2: Use which to find which
[root@localhost ~]# which which alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' /usr/bin/which [root@localhost ~]#
There are two
whichs, one of which is
alias. This is called a “command alias”, meaning that typing
which is equivalent to the command that follows it!
Example 3: Find the cd command
cd This commonly used command could not find ah! Why? This is because
cd is a built-in command in bash! But
which defaults to the directory specified in the PATH, so of course it can’t be found!