Linux locate file system quick search

Source: Internet
Author: User
    • Locate command
    • 51cto

 

Original article link


Part 1: Use locate Manual
$ Man locate
Locate (1) locate (1)
Name
Locate-find files by name
Synopsis
Locate [Option]... pattern...
Part 2: locate Search Mechanism

Specifically, whereis is used to find the target from the path of your environment variable (path and other major paths. Fast and limited scope. Can only be usedProgramOnly binary files (parameter-B), man description files (parameter-m), andSource codeFile (parameter-S ). If the parameter is omitted, all information is returned.

Example:

$ Whereis grep
Grep:/bin/grep/usr/share/man/Man1/grep.1.gz
$ Whereis-B grep
Grep:/bin/grep
$ Whereis-M grep
Grep:/usr/share/man/Man1/grep.1.gz
$ Whereis-s grep
Grep:

 

The locate command is used to find the target from the database generated by the updatedb command..To retrieve all files, you must regularly run updatedb to update the database to ensure real-time content.

Example:

$ Locate XYZ. py # Find an inaccessible file XYZ. py. The result is not found.
$ Touch XYZ. py # manually create XYZ. py and
$ Locate XYZ. py # search by using locate, still not found
$ Sudo updatedb # update a database
$ Locate XYZ. py # locate again, okay
/Home/Jia/XYZ. py

 

Find is the most powerful, complex, and flexible search command. You can use it to find any file you want.

 

The which command searches for the location of a system command in the path specified by the PATH variable and returns the first search result. That is to say, by using the which command, you can see whether a system command exists and where the command is executed.
$ Which find
/Usr/bin/find
$ Which CP
/Bin/CP
$ Which grep
/Bin/grep

 

The type command is actually not a search command. It is used to identify whether a command is provided by shell or an independent binary file outside the shell. If a command is an external command, use the-p parameter to display the path of the command, which is equivalent to the which command.
$ Type CD

CD is a shell builtin
$ Type python
Python is/usr/bin/Python
$ Type-P Python
/Usr/bin/Python
$ Which Python
/Usr/bin/Python

 

Part 3: Options

param 1:
-B, -- basename
match only the base name against the specified patterns.
This is the opposite of -- wholename.
# Query files in the file system with a file name containing pattern
$ locate-B 'ue '| less
param 2:
-C, -- count
instead of writing file names on standard output, write
the number of matching entries only.
# query the number of files in the file system with a file name containing pattern
$ locate-C 'ue'

Param 3:
-D, -- database dbpath
Replace the default database with dbpath. dbpath is
:-Separated list of database file names. If more
One -- database option is specified, the resulting path is
A concatenation of the separate paths.

An empty database file name is replaced by the default
Database. A database file name-refers to the standard
Input. Note that a database can be read from the Stan repository?
Dard input only once.
# Specify the path of the database (the default database is generally used)

 

Param 4:

-E, -- existing
Print only entries that refer to files existing at
Time locate is run.
# Display the list of matched files when executing the locate command.

Param 5:
-L, -- follow
When checking whether files exist (if the -- existing
Option is specified), follow trailing symbolic links.
This causes broken symbolic links to be omitted from
Output.
This is the default behavior. The opposite can be speci?
Fied using -- nofollow.
# Statistics include linked files

Param 6:
-I, -- ignore-case
Ignore case distinctions when matching patterns.
# Case-insensitive file name matching

Param 7:
-L, -- limit,-n Limit
Exit successfully after finding limit entries. If
-- Count option is specified, the resulting count is also
Limited to limit.
# Only matching results of a specified number of entries are displayed during file name matching

 

Param 8:

-W, -- wholename

Match only the whole path name against the specified Pat submit?
Terns.
This is the default behavior. The opposite can be speci?
Fied using -- basename.
# Full name matching is performed when the file name is matched. That is, locate-W 'UE '. Only files with the file name UE are found. files with the file name UE are not counted.

Use locate to find files

The locate database not only contains commands, but also the names of each node in the file system, you can use locate to find commands, devices, manual pages, data files, or any files that can be identified by name in the file system. Example:

    1. $ Locate E100
    2. /Lib/modules/2.6.20-1.2949.fc7/kernel/Drivers/NET/e100.ko
    3. /Lib/modules/2.6.20-1.2949.fc7/kernel/Drivers/NET/e1000/e1000.ko

In the above example, we will find the e100.ko and e1000.ko kernel modules. The locate command uses the-I option, which is case insensitive. Otherwise, it is case sensitive, as shown in the following example:

    1. $ Locate-I itco_wdt
    2. /Lib/modules/2.6.20-1.2949.fc7/kernel/Drivers/Char/watchdog/itco_wdt.ko

The following example adds a regular expression with locate:

 

    1. $ locate-r/ls $ search for objects ending with/ls $
    2. /bin/ls
    3. /usr/share/locale/l10n/ls
    4. $ locate-r mkfs * 3 search for files whose names contain mkfs and 3
    5. /sbin/mkfs. ext3
    6. /usr/share/man/man8/mkfs.ext3.8.gz
    7. $ locate-R ^/boot/GRUB/me search for files starting with/boot/GRUB/mE
    8. /boot/GRUB/menu. lst

The RPM package mlocate (some Linux releases are slocate) provides a cron job that runs the updatedb command once a day to update the file locate database. Because the file you are looking for may have been deleted after the last database update, you can use locate-e to check whether the file found in the database still exists:

    1. $ Locate-e myfilename

Run the updatedb command to update the locate database immediately:

    1. # Updatedb

 

[Responsible editor: Yunxia Tel :( 010) 68476606]

 

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.