Linux Command Locate

Source: Internet
Author: User

Original: 53285624

Briefly

Locate can quickly search the file system for a specific file. The method is to first establish an index database that includes all the file names and paths within the system, and when looking for it, simply query the index database without actually delving into the file system. Locate because it is only to retrieve the index database, it is much faster than the find overall retrieval.

In the general distribution, the establishment of the database is placed in the crontab automatic execution. Because the database used by locate is updated by Crondaemon periodic builds. is usually updated once a week, so the newly created file will not be found in the near today, or you will find a file that has been deleted. Then, you can manually update the database with the UpdateDB command, or re-adjust the update cycle yourself.

    • Briefly
    • Command Introduction
    • Command options
    • Usage examples

Command Introduction
    • Command name
      Locate

    • Basic syntax
      Locate [OPTION] ... [PATTERN] ...

    • Function description
      Search for entries in the Mlocate database

Command options
Options Description
-A, --all Show only entries that match all patterns
-b, --basename Match only the base name of the path name
-c, --count Show only the number of entries found
-d, --database DBPATH Use DBPATH instead of default data (i.e.:/var/lib/mlocate/mlocate.db)
-e, --existing Show only file entries that currently exist
-L, --follow Follow the spread of symbolic links when a file is present (default)
-h, --help Display Help information
-i, --ignore-case Ignore case differences when matching patterns
-l, --limit, -n LIMIT Limit to the output (or count) of the limit project
-m, --mmap Ignored for backwards compatibility
-P, --nofollow, -H Do not follow the spread of symbolic links when checking files
-0, --null Separating items with NUL at output
-S, --statistics Do not search for items, display statistics about each used database
-q, --quiet Do not report error messages about reading the database
-r, --regexp REGEXP Search for basic regular expressions REGEXP instead of patterns
--regex Pattern is an extended regular expression
-s, --stdio Ignored for backwards compatibility
-V, --version Display version Information
-w, --wholename Match full path name (default)
Usage examples

1. Search the directory for all files starting with SH

Locate /etc/sh/etc/shadow/etc/shadow-/etc/shells
 

2. Find the relevant file containing a string

[Email protected] ~]$Locate passwd/etc/passwd/etc/passwd-/etc/pam.d/passwd/etc/security/opasswd/usr/bin/gpasswd/usr/bin/grub2-mkpasswd-PBKDF2/usr/bin/kpasswd/usr/bin/lppasswd/usr/bin/passwd/usr/bin/smbpasswd# Partial display result omitted/usr/share/Mans/MAN8/CHPASSWD.8. GZ/usr/share/Mans/man8/saslpasswd2.8. GZ/usr/share/Mans/MAN8/SMBPASSWD.8. GZ/usr/share/Mans/PT_BR/MAN1/GPASSWD.1. GZ/usr/share/Mans/RU/MAN1/GPASSWD.1. GZ/usr/share/Mans/RU/MAN8/CHPASSWD.8. GZ/usr/share/Mans/ZH_CN/MAN1/GPASSWD.1. GZ/usr/share/Mans/ZH_CN/MAN8/CHPASSWD.8. GZ/usr/share/Mans/ZH_TW/MAN8/CHPASSWD.8. GZ/usr/share/vim/vim74/ftplugin/passwd. Vim/usr/share/vim/vim74/syntax/passwd. Vim
 

3. Limit the number of displays

If you see too much content, you can use -n options to limit the number of displays.

Locate 5 passwd/etc/passwd/etc/passwd-/etc/pam.d/passwd/etc/security/opasswd /usr/bin/gpasswd
 

4. Case-insensitive when searching

The option is used when the lookup does not need to be case sensitive -i .

Locate 5 passwd/etc/passwd/etc/passwd-/etc/pam.d/passwd/etc/security/opasswd /usr/bin/gpasswd
 

5. Using Regular expressions

When you need to find information that conforms to a specific rule, you can use -r options to match the corresponding regular expression.

# Find in/var/lib/rpm opening file [[email protected]~]$Locate-R ^/var/lib/rpm/var/lib/rpm/var/lib/rpm- State/var/lib/rpm/. Dbenv.lock/var/lib/rpm/. Rpm.lock/var/lib/rpm/Basenames/var/lib/rpm/Conflictname/var/lib/rpm/Dirnames/var/lib/rpm/Group/var/lib/rpm/Installtid/var/lib/rpm/Name/var/lib/rpm/Obsoletename/var/lib/rpm/Packages/var/lib/rpm/Providename/var/lib/rpm/Requirename/var/lib/rpm/Sha1header/var/lib/rpm/SIGMD5/var/lib/rpm/triggername/var/lib/rpm/__db.001/var/lib/rpm/__db.002/var/lib/rpm/__db.003/var/lib/rpm-state/gconf# Find toZipend of file [[email protected]~]$Locate-RZip$/home/wang/doc.Zip/usr/bin/Funzip/usr/bin/gpg-Zip/usr/bin/Gunzip/usr/bin/gzip/usr/bin/Mzip/usr/bin/Unzip/usr/bin/Zip/usr/lib64/libreoffice/share/config/images.Zip/usr/lib64/libreoffice/share/config/images_crystal.Zip/usr/lib64/libreoffice/share/config/images_galaxy.Zip/usr/lib64/libreoffice/share/config/images_hicontrast.Zip/usr/lib64/libreoffice/share/config/images_oxygen.Zip/usr/lib64/libreoffice/share/config/images_sifr.Zip/usr/lib64/libreoffice/share/config/images_tango.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-blue.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-green.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/glas-red.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/round-gorilla.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/round-white.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/simple.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-blue.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-gray.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-green.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-red.Zip/usr/lib64/libreoffice/share/config/wizard/web/buttons/square-yellow.Zip/usr/local/lib/python3.5/test/zip_cp437_header.Zip/usr/local/lib/python3.5/test/zipdir.Zip/usr/local/lib/python3.5/test/test_importlib/namespace_pkgs/missing_directory.Zip/usr/local/lib/python3.5/test/test_importlib/namespace_pkgs/nested_portion1.Zip/usr/local/lib/python3.5/test/test_importlib/namespace_pkgs/top_level_portion1.Zip/usr/share/bash-completion/completions/gzip
 

6. Find the most recently changed files

You cannot find the most recently changed files using the Locate command. To avoid this situation, you can manually update the database by using the UpdateDB command before using locate.

# New file [[email protected]~]#TouchNew_file[[email protected]~]#Locate~/New_file[[email protected]~]# Updatedb[[email protected]~]#Locate~/New_file/root/New_file[[email protected]~]# # deleting files [[email protected]~]#RM-rffile. txt [[email protected]~]#Locate~/file. txt/root/file. Txt[[email protected]~]# Updatedb[[email protected]~]#Locate~/file. Txt[[email protected]~]#
 

Linux Command Locate

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.