Every day a linux command locate command _linux Shell

Source: Internet
Author: User

Locate allows users to quickly search the file system for specific files. The approach is to create a database that includes all the file names and paths in the system, and then simply query the database when looking, without actually going into the file system. In the general distribution, the establishment of the database is put into the crontab to execute automatically.

1. Command format:

Locate [Select parameters] [style]

2. Command function:

The locate command can quickly find files when searching the database, the database is updated by the UpdateDB program, UpdateDB is set up by the cron daemon periodically, and the locate command searches the database faster than the whole data from the hard disk. But the worse is locate found the file if only recently established or renamed, may not be found, in the default value, UpdateDB will run once a day, can be modified crontab to update the set value. (Etc/crontab)

Locate specifies to search for a qualifying file, which stores files and directory names in a database, finds files or catalogs that conform to the template-style criteria, and can use special characters such as "*" or "?" , and so on) to specify the template style, such as the specified template is Kcpa*ner, locate will find all the start string is KCPA and the end of the NER file or directory, such as the name Kcpartner if the catalog name is Kcpa_ner, it lists all files that are included in the directory, including subdirectories.

Locate instructions and find the function of looking for a file is similar, but locate is through the update program to the hard disk of all the files and directory data first set up an index database, in the execution of LOACTE directly find the index, query speed will be faster, index database is generally managed by the operating system, However, you can also directly release the update force system to immediately modify the index database.

3. Command parameters:

-E will be excluded from the scope of the search.

-1 if it's 1. Then start Safe mode. In Safe mode, users do not see files that are not visible to the user. This will begin to slow down, since locate must obtain access to files in the actual file system.

-F excludes specific file systems, for example, we have no reason to put the files in the proc file system in the database.

-Q Quiet mode does not display any error messages.

-n Displays at most n outputs.

-R uses normal expressions to find conditions.

-o Specifies the name of the data inventory.

-d Specifies the path to the repository

-H Display Auxiliary messages

-V Display version message for program

4. Use instance:

Example 1: Find all files related to PWD

Command:

Locate pwd

Output:

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

Example 2: Search for all files in the ETC directory that start with SH

Command:

Locate/etc/sh

Output:

Peida-virtualbox ~ # locate/etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
peida-virtualbox ~ #

Example 3: Search for ETC directories, all files beginning with M

Command:

locate/etc/m

Output:

Peida-virtualbox ~ # locate/etc/m
/etc/magic
/etc/magic.mime
/etc/mailcap
/etc/mailcap.order
/etc/manpath.config
/etc/mate-settings-daemon

-E will be excluded from the scope of the search.

The meaning of-E is if you have just deleted a file, and the/var/lib/mlocate/mlocate.db database file has not yet been updated. Then after the direct use of locate filename, the file that has just been deleted will still be listed, but if you use-e, the command confirms that the file does not list the file path on the file system, so if there are many matching files, the-e option will slow down the query.

When running the locate command, the system complains: Locate:command not found. Check the relevant data and found that the reason is that the system does not have the execution file, you need to install. The solution is as follows:

Execute command: [root@centos62server ~]# yum Install locate/See which package the locate belongs to

The above is a small set to introduce a daily linux command locate command, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.