Several search commands for Linux

Source: Internet
Author: User

which command
Which is used to find system commands, such as LS, cat, etc., which are searched from the folder specified by the path variable, and when the specified folder has a binary file with the same name as the command, the path to the file is given, that is, the path to the command.
However, it seems that it must be a binary file, I tried to add a file in/usr/bin, and then which did not search it out:

m@meng:/bin$ sudo touch newm@meng:/bin$ which newm@meng:/bin$ 

And then the egg ah ...
Which by default, only the first match is returned, plus the-a parameter, which returns all matching results.

Locate Command
This command locates a file by searching for a database in the system, and if the file or command you are looking for has a corresponding record in the database, you can return the result, otherwise it will not be found. This database, called the "System File Index Database" (well, yes, the name of the adult), it is established and maintained by the UpdateDB command, man once this command, you can see the location of this database file is/var/lib/mlocate/ Mlocate.db (My is Ubuntu system 14.04), with vim Open is a bunch of garbled, I also died that research its heart.
in short, locate and the Whereis command mentioned below are all implemented by reading this database file for quick lookups. Man manual says: UpdateDB is usually run daily by cron (8) to update the default database. This means that the UpdateDB will update the database at a fixed point every day. Therefore, it is possible that the newly created file has not been added to the database and therefore cannot be found, and this time it is necessary to manually enter UpdateDB to update the database. The basic format of the
Locate command is: Locate Options Pattern,pattern is an arbitrary string, and of course we are looking for a file name or its path. This pattern is supported by regular expressions, the man manual says: If–regex is not specified, PATTERNs can contain globbing characters. If any pattern contains no globbing characters, locate behaves as if the PATTERN were *pattern* . The
is then a few common options:
--c:count, counting, which lists only the number of matches, and does not list those matching results.
--e:exist, in all matching pattern entries, if an item does not exist (deleted or renamed) at this time (the Locate command is running), it will no longer appear. Only those 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 renaming, search for new with locate will not be found.

    • -I: Ignore case.
    • -L or-N: followed by a number to limit the number of occurrences of the display.
    • -b:match only the base name against the specified patterns. This is the description in the Man manual, and the question is what is base name? And the only example given on man is about-B, and I feel that this option is important, but I can't understand it now:
      To search for a file named exactly name (not name), use
      Locate-b ' \name '
      Because \ is a globbing character, this disables the implicit replacement of name by name.

whereis Command
While querying the same database, Whereis and locate find the same keywords, locate can find arbitrary strings, but Whereis can only look for executable binaries, such as system commands, or through Yum, Apt-get such as the name of the software installed, and the ordinary file it is basically ignored.
By default, the Whereis command outputs three location information about a program (command or executable binary): The binary itself, the man description document, and the source code. As follows:

m@meng:~/program/C_codes$ whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz

The source code for general system commands is not installed by default, so it is not displayed.
There are several options to control the output of Whereis or find items:

    • -B: Only find or display binary files;
    • -M: Only find or display the man manual;
    • -S: Only find or show source code only.
      There are also several options for displaying the range of lookups:
    • -B: Limit the directory where binary files are found;
    • -M: Limit the directory to find the man manual;
    • -S: Limit the directory to find source code
      There are many restrictions on the use of WHEREIS commands.

* Find command *
This is the most powerful file Search command, but not as fast as locate. Its basic syntax is: Find path option other. Do not write it for the time being, need to write a special article can be done.

Several search commands for Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.