How to use Linux locate command

How to use Linux locate command

01. Command Overview

The locate command is actually another way of writing find -name, but it is much faster than the latter because it does not search a specific directory, but a database /var/lib/locatedb, which contains all local file information. The Linux system automatically creates this database and updates it once a day, so the latest changed files cannot be found using the locate command. To avoid this situation, you can use the updatedb command to manually update the database before using locate.

02. Command format

locate [OPTION]… [PATTERN]…

03. Common options

Usage: locate [OPTION]... [PATTERN]...
Search for entries in the mlocate database.
 -A, --all only show entries that match all patterns -b, --basename base file name to match unique path names -c, --count only show the number of entries found -d, --database DBPATH replace the default database (/var/lib/mlocate/mlocate.db) with DBPATH
 -e, --existing only show entries for currently existing files -L, --follow follow creeping symbolic links if the file exists (default)
 -h, --help Show this help -i, --ignore-case Ignore uppercase and lowercase differences when matching patterns -l, --limit, -n LIMIT Limit output to LIMIT items (or count) 
 -m, --mmap ignore backward compatibility -P, --nofollow, -H do not follow creeping symbolic links when checking files -0, --null separate items with NULs in output -S, --statistics do not search items, show statistics about each used database -q, --quiet do not report error messages about reading databases -r, --regexp REGEXP search for the basic regular expression REGEXP instead of pattern --regex pattern is an extended regular expression -s, --stdio ignore backward compatibility -V, --version show version information -w, --wholename match full pathnames (default)

04. Reference examples

4.1 Find related files containing a certain string

[root@localhost ~]# locate ifconfig 
/home/oracle/app/oracle/product/11.2.0/dbhome_1/sysman/admin/scripts/ifconfig.sh
/usr/libexec/hypervkvpd/hv_set_ifconfig
/usr/sbin/ifconfig
/usr/sbin/pifconfig
/usr/share/man/de/man8/ifconfig.8.gz
/usr/share/man/fr/man8/ifconfig.8.gz
/usr/share/man/man8/ifconfig.8.gz
/usr/share/man/man8/pifconfig.8.gz
/usr/share/man/pt/man8/ifconfig.8.gz
/usr/share/man/zh_CN/man8/ifconfig.8.gz
[root@localhost ~]# 

4.2 Search for all files starting with sh in the directory

[root@localhost ~]# locate /bin/sh
/home/oracle/app/oracle/product/11.2.0/dbhome_1/perl/bin/shasum
/usr/bin/sh
/usr/bin/sha1sum
/usr/bin/sha224sum
/usr/bin/sha256sum
/usr/bin/sha384sum

4.3 Specify the display quantity

If there is too much content to display, you can use the -n option to limit the number of items displayed.

[root@localhost ~]# locate -n 3 passwd
/etc/passwd
/etc/passwd -
/etc/pam.d/passwd
[root@localhost ~]# 

4.4 Ignore case differences when matching patterns

When the search does not need to be case sensitive, use the -i option.

[root@localhost ~]# locate -i -n 5 passwd
/etc/passwd
/etc/passwd -
/etc/pam.d/passwd
/etc/security/opasswd
/home/oracle/app/oracle/product/11.2.0/dbhome_1/apex/images/htmldb/icons/32x32/item_passwd.gif
[root@localhost ~]#

4.5 Searching for basic regular expressions REGEXP to replace patterns

When you need to find information that meets specific rules, you can use the -r option to match the corresponding regular expression.

//Find files starting with /var/lib/rpm [root@localhost ~]# 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
//Find the file ending with etc.conf[root@localhost ~]# locate -r etc.conf$
/usr/lib/tmpfiles.d/etc.conf
[root@localhost ~]#

4.6 Find recently modified files

# Create a new file [root@localhost ~]# touch new_file
[root@localhost ~]# locate ~/new_file
[root@localhost ~]# updatedb
[root@localhost ~]# locate ~/new_file
/root/new_file
[root@localhost ~]# 
# Delete file [root@localhost ~]# rm -rf file.txt 
[root@localhost ~]# locate ~/file.txt
/root/file.txt
[root@localhost ~]# updatedb
[root@localhost ~]# locate ~/file.txt
[root@localhost ~]#

Note: The locate command cannot find files that have been recently changed. To avoid this, you can use the updatedb command to manually update the database before using locate.

4.7 Viewing Statistics

[deng@localhost test]$ locate -S 
Database /var/lib/mlocate/mlocate.db:
    18,935 folders 231,751 files 13,753,723 bytes in file names 5,707,750 bytes used to store database [deng@localhost test]$

4.8 View passwd statistics

[root@localhost /]# locate -c passwd
183
[root@localhost /]#

4.9 Update related configuration files

[root@localhost /]# vim /etc/updatedb.conf 
[root@localhost /]#

This is the end of this article about how to use the Linux locate command. For more information about the Linux locate command, please search 123WORDPRESS.COM’s previous articles or the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of two search commands in Linux: locate and find
  • One Linux command per day: locate command

<<:  MySql knowledge points: transaction, index, lock principle and usage analysis

>>:  Solution to the ineffective global style of the mini program custom component

Recommend

Detailed usage of js array forEach instance

1. forEach() is similar to map(). It also applies...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

Detailed explanation of commonly used CSS styles (layout)

Compatible with new CSS3 properties In CSS3, we c...

WeChat applet development form validation WxValidate usage

I personally feel that the development framework ...

Detailed explanation of Vue life cycle functions

Table of contents Lifecycle Functions Common life...

Implementation of TCPWrappers access control in Centos

1. TCP Wrappers Overview TCP Wrappers "wraps...

Solution to MySQL being unable to start due to excessive memory configuration

Problem Description MySQL reports an error when s...

Detailed explanation of the usage of scoped slots in Vue.js slots

Table of contents No slots Vue2.x Slots With slot...

Docker runs operations with specified memory

as follows: -m, --memory Memory limit, the format...

JavaScript to implement search data display

This article shares the data display code for Jav...