Linux search commands and Linux search commands
Which command
Which is used to find system commands, such as ls and cat. It is searched from the folder specified by the PATH variable. When the specified folder contains a binary file with the same name as the command, the path of the file, that is, the path of the command.
However, it seems that the file must be a binary file. I tried to add a file to/usr/bin without searching which:
m@meng:/bin$ sudo touch newm@meng:/bin$ which newm@meng:/bin$
And ah...
By default, which returns only the first matching result. With the-a parameter, all matching results are returned.
Locate command
This command is used to search for a database in the system. If the file or command to be searched has corresponding records in the database, the corresponding results can be returned, otherwise, it cannot be found. This database is called "System File index database" (well, that's right, the name of this adult). It is created and maintained by the updatedb command. Run this command, the location of the database file is/var/lib/mlocate. db (My Ubuntu system is 14.04), and opening it with vim is a bunch of garbled characters, so I am dead of the mind to study it.
In short, the locate and the whereis commands to be mentioned below are all quickly searched by reading the database file. In the man manual, updatedb is usually run daily by cron (8) to update the default database. This means that updatedb updates the database at a fixed point every day. Therefore, the newly created file may not be found because it has not been added to the database. In this case, you need to manually enter updatedb to update the database.
The basic format of the locate command is: locate options pattern. pattern is an arbitrary string. Of course, we usually search for a file name or its path. This pattern supports regular expressions. man manual says: If-regex is not specified, PATTERNs can contain in globbing characters. If any PATTERN contains no globbing characters, locate behaves as ifPattern were * PATTERN *.
Then there are several common options:
--C: count, count, that is, to list only the number of matching items, not those matching results.
--E: exist: in all items that match the pattern, if an item does not exist (is deleted or renamed) at this time (when the locate command is run this time), it is no longer displayed. Only items that still exist are displayed.
m@meng:~$ locate -e m/new /home/m/newm@meng:~$ mv new oldm@meng:~$ locate -e m/new m@meng:~$
After the name is renamed, you cannot find it by searching for new with locate ..
- -I: case insensitive.
- -L or-n: Followed by a number to limit the number of matching items displayed.
- -B: Match only the base name against the specified patterns. This is a description in the man Manual. the question is what is the base name? In addition, the only example given on man is about-B. I think this option is very important, but I still cannot understand it:
To search for a file named exactly NAME (notNAME), Use
Locate-B '\ name'
Because \ is a globbing character, this disables the implicit replacement of NAMENAME.
Whereis command
Although both query the same database, the keywords used by whereis and locate are not the same. locate can search for arbitrary strings, but whereis can only search for executable binary files, such as system commands, or the name of the software installed through yum, apt-get, and so on, while the common file is basically ignored.
By default, the whereis command outputs information about the three locations of the Program (command or executable binary file): the binary file itself, man instructions, and source code. As follows:
m@meng:~/program/C_codes$ whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz
Generally, the source code of the system command is not installed by default, so it is not displayed.
There are several options to control the output or search items of whereis:
- -B: Only search for or display binary files;
- -M: only the man manual is found or only the man manual is displayed;
- -S: only find or only display the source code.
There are also several options to display the search range:
- -B: restrict the directories of binary files;
- -M: restrict the man manual directory;
- -S: restrict the directory of the source code.
I personally feel that there are still many restrictions on the use of the whereis command.
* Find command *
This is the most powerful file search command, but the speed is not as high as locate. The basic syntax is: find path option. I will not write it for the time being. I need to write an article to fix it.