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:
|
<<: MySql knowledge points: transaction, index, lock principle and usage analysis
>>: Solution to the ineffective global style of the mini program custom component
Unfortunately, the MYSQL_DATA_TRUNCATED error occ...
1. What is it? MySQL is the most popular relation...
Table of contents Overview 1. Clearly understand ...
Table of contents Unary Operators Boolean Operato...
introduction Our company is engaged in the resear...
CentOS8 was released a few days ago. Although it ...
Mouse effects require the use of setTimeout to ge...
1 Overview System centos8, use httpd to build a l...
1. Introduction to mysqlbackup mysqlbackup is the...
Table of contents Preface Why does limit deep pag...
1. E-Commerce Icons 2. Icon Sweets 2 3. Mobile Ph...
In CSS3, the transform function can be used to im...
Learning objectives: The two functions parseInt()...
mysql full backup 1. Enable binary log and separa...
The first step is to prepare an icon making softwa...