How to use the locate command in linux (quick file search)

Source: Internet
Author: User
Locate allows users to quickly search for specified files in the file system. The method is to first create a database that includes the names and paths of all files in the system. then, you only need to query the database when searching, instead of going into the file system. In general di

Locate allows users to quickly search for specified files in the file system. The method is to first create a database that includes the names and paths of all files in the system. then, you only need to query the database when searching, instead of going into the file system. In general distribution, database creation is automatically executed in crontab.

1. command format:

Locate [Select parameter] [style]

2. command functions:

The locate command can be used to quickly find files when searching for databases. the database is updated by the updatedb program, and the updatedb is periodically created by the cron daemon, the locate command is faster than searching for data from the entire hard disk, but worse is that the files found by the locate are created recently or renamed, it may not be found. in the specified value, updatedb runs once a day. you can modify crontab to update the set value. (Etc/crontab)

Locate is used to search for qualified files. it stores the files and directories in the database to find files or directories that meet the template style conditions, special characters (such as "*" or "?" ) To specify the template style. for example, if you specify the template as the kcpa * ner, locate will find all the files or directories whose start string is kcpa and end with the ner, for example, if the directory name is kcpartner and the directory name is kcpa_ner, all files including subdirectories in the directory are listed.

The locate command is similar to the find File query function. However, the locate program uses the update program to create an index database for all files and directories on the hard disk. when executing the loacte command, it directly finds the index, the query speed is faster. The index database is generally managed by the operating system, but you can also directly issue an update command to force the system to immediately modify the index database.

3. command parameters:

-E will be excluded from the search range.
-1 if it is 1. the security mode is enabled. In security mode, users do not see files that cannot be viewed by permissions. This slows down because the locate must obtain the permission information for the file in the actual file system.
-F exclude a specific file system. for example, we have no reason to put the files in the proc file system in the database.
-Q Quiet mode. no error message is displayed.
-N: displays up to n outputs.
-R uses the regular formula for search conditions.
-O specifies the data inventory name.
-D specifies the database path
-H: displays the secondary message
-V: displays the version information of the program.

4. example:

Instance 1: Search for all pwd-related files

Command:

Locate pwd

Output:


Copy codeThe code is as follows:
Peida-VirtualBox ~ # Locate pwd
/Bin/pwd
/Etc/. pwd. lock
/Sbin/unix_chkpwd
/Usr/bin/pwdx
/Usr/include/pwd. h
/Usr/lib/python2.7/dist-packages/twisted/python/fakepwd. py
/Usr/lib/python2.7/dist-packages/twisted/python/fakepwd. pyc
/Usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py
/Usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc
/Usr/lib/syslinux/pwd. c32
/Usr/share/help/C/empathy/irc-join-pwd.page
/Usr/share/help/ca/empathy/irc-join-pwd.page
/Usr/share/help/cs/empathy/irc-join-pwd.page
/Usr/share/help/de/empathy/irc-join-pwd.page
/Usr/share/help/el/empathy/irc-join-pwd.page

Instance 2: Search for all files starting with sh in The etc directory

Command:

Locate/etc/sh

Output:


Copy codeThe code is as follows:
Peida-VirtualBox ~ # Locate/etc/sh
/Etc/shadow
/Etc/shadow-
/Etc/shells
Peida-VirtualBox ~ #

Instance 3: Search for all files starting with m in The etc directory

Command:

Locate/etc/m

Output:


Copy codeThe code is as follows:
Peida-VirtualBox ~ # Locate/etc/m
/Etc/magic
/Etc/magic. mime
/Etc/mailcap
/Etc/mailcap. order
/Etc/manpath. config
/Etc/mate-settings-daemon

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.