We often want to find a file in linux, but don't know where it is, you can use the following commands to search:
- which looks at the location of the executable.
- whereis looks at the location of the file.
- locate works with the database to view the file location.
- find actually searches the hard disk for file names.
The function of the which command is to search the location of a system command in the path specified by the PATH variable and return the first search result. That is to say, using the which command, you can see whether a system command exists, and where the command is executed.
1. Command format:
which executable name
2. Command function:
The which command searches the location of a system command in the path specified by the PATH variable and returns the first search result.
3. Command parameters:
-n Specifies the filename length, which must be greater than or equal to the longest filename among all files.
-p is the same as the -n parameter, but here includes the path to the file.
-w Specifies the width of the column on output.
-V show version information
4. Example of use:
Example 1: Find files and display command paths
Order:
which lsmod
output:
[root@localhost ~]# which pwd /bin/pwd [root@localhost ~]# which adduser /usr/sbin/adduser [root@localhost ~]#
illustrate:
which searches for executable files according to the directory in the PATH variable configured by the user! Therefore, the commands found by different PATH configuration contents are of course different!
Example 2: Using which to find out which
Order:
which which
output:
[root@localhost ~]# which which alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' /usr/bin/which [root@localhost ~]#
illustrate:
There are actually two which , one of which is alias This is the so-called "command alias", which means that entering which will be equal to the string of commands that follow!
Example 3: Find the cd command
Order:
which cd
output:
[linux@localhost ~]$ which cd /usr/bin/which: no cd in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/linux/bin) [linux@localhost ~]$
illustrate:
The commonly used command cd can't be found ! why? This is because cd is a built-in command of bash! But which defaults to looking for the directory specified in the PATH, so of course it must not be found!