linux file search command --- locate

locate

locate search for files and directories, but much faster than the find command, he was not the find command, search for a specific directory, but searching a database, / var / lib / mlocate / mlocate.db. This database contains information on all local files, linux system will automatically create the database, and automatically updated once a day.

are whereis locate and use the same database, which is / var / lib / mlocate. When using locate, often less than the newly created file search, or search for files that have been deleted, it is because the database file is not updated, need to manually update. So before each use the locate command, first use updatedb to update the database.

locate the work process

Locate the entire work is actually made up of four parts:

/usr/bin/updatedb
/usr/bin/locate(mlocate)
/etc/updatedb.conf
/var/lib/mlocate/mlocate.db

updatedb: mainly used to update the database, this work can be done automatically via crontab;
mlocate / the locate: the completion of the inquiry procedure;
updatedb.conf: used to configure the database to which directories and files into which files to exclude and so on;
mlocate .db: file storage file information;

locate installation

There locate command system installation CD in the installation package, called mlocate-0.26-6.el7.x86_64.
yum can be installed directly.
During mlocate package installation, it will create a once every 24 hours to run updatedb cron job command. This ensures that the database is updated regularly. For more information about cron jobs, check the /etc/cron.daily/mlocate file.
You can use the command updatedb to update, the update process will take some time, depending on the speed of the system and the number of files and directories.

format

locate [options] parameters

Options

b: -basename, matching a unique path name of the base file name
-c: -count, only to find the number of entries displayed
-d: -database DBPATH with DBPATH instead of the default database (/var/lib/mlocate/mlocate.db)
-e: -existing show only file entries currently exists
-L: --follow when files exist follow symbolic links spread (default)
-h: -help display this help
ignore size when -ignore-case matching mode: -i write difference
-l: --limit, -n lIMIT lIMIT limit output of the project (or count)
-m: --mmap ignored backward compatibility
-P: -nofollow, -H when checking the file does not follow symbolic spread Links
-0: when --null output to NUL separate project
-S: --statistics not search for items displayed for each relevant statistical information database
-q: -quiet does not report error messages about reading databases
-r: -regexp REGEXP search for basic regular expression REGEXP instead of patterns
-regex mode is an extended regular expression
-s: --stdio ignored backward compatibility
-V: --version display version information
-w: -wholename match the full path name ( default)

Simple Case

Note: This case just to name a few common options, and the rest on their own practice
first look, create a new file can not find the phenomenon.

[root@linus ~]# locate my.cnf				#搜索my.cnf文件
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
[root@linus ~]# touch /root/my.cnf		#新建my.cnf文件
[root@linus ~]# locate my.cnf
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
[root@linus ~]# updatedb					#手动更新locate的数据库
[root@linus ~]# locate my.cnf				#查看到了
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
/root/my.cnf

Some common options

[root@linus ~]# locate -c my.cnf			#只显示查找到内容的数量
4

For filter has been deleted file, you need to add the -e option

[root@linus ~]# locate -e my.cnf
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
/root/my.cnf
[root@linus ~]# rm -rf /root/my.cnf 
[root@linus ~]# locate -e my.cnf
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
[root@linus ~]# locate  my.cnf				#如果不加-e选项,也会把删除的文件也显示出来,要么手动更新,要么加上-e选项
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
/root/my.cnf

Ignore case when searching

[root@linus ~]# touch HHH
[root@linus ~]# locate -i hhh		#没有更新数据库,也没有搜索出来
[root@linus ~]# updatedb			
[root@linus ~]# locate hhh			#没有加-i选项,只查找hhh,没有找到
[root@linus ~]# locate -i hhh
/root/HHH
发布了14 篇原创文章 · 获赞 4 · 访问量 526

Guess you like

Origin blog.csdn.net/qq_42534026/article/details/103877773