目录
一、概述
locate 命令是在 Linux 和类 Unix 系统中快速查找文件路径的强大工具。与 find 命令相比,locate 的速度通常更快,因为它不直接扫描文件系统,而是查询一个预先构建的索引数据库。
二、工作原理
locate 命令依赖于一个名为 mlocate 的程序来维护索引数据库。该数据库通常每天通过 cron 作业自动更新一次。这意味着 locate 可能不会立即反映文件系统的最新更改。
三、安装
大多数 Linux 发行版都默认安装了 mlocate。如果没有安装,你可以通过包管理器安装它。
1、在 Debian 或 Ubuntu 上:
sudo apt-get install mlocate
2、在 Fedora 或 CentOS 上:
(1)命令
sudo dnf install mlocate
(2)执行效果
执行上述命令后,结果如下:
[root@ecs-52a1 /]#
[root@ecs-52a1 /]# yum install mlocate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mlocate.x86_64 0:0.26-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================
Installing:
mlocate x86_64 0.26-8.el7 base 113 k
Transaction Summary
==============================================================================================================================================================
Install 1 Package
Total download size: 113 k
Installed size: 379 k
Is this ok [y/d/N]: y
Downloading packages:
mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mlocate-0.26-8.el7.x86_64 1/1
Verifying : mlocate-0.26-8.el7.x86_64 1/1
Installed:
mlocate.x86_64 0:0.26-8.el7
Complete!
[root@ecs-52a1 /]# locate --h
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.
-A, --all only print entries that match all patterns
-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information
-w, --wholename match whole path name (default)
Report bugs to mitr@redhat.com.
[root@ecs-52a1 /]#
[root@ecs-52a1 /]#
四、更新索引
1、首次使用
首次使用 locate 之前,或者当你需要更新索引数据库时,可以运行以下命令:
sudo updated
否则,当执行命令的时候,会出现如下错误:
2、更新索引后的效果
更新索引后,可以很快查到需要的文件,如下图所示:
五、基本用法
1、基本语法
locate [OPTION]... [PATTERN]...
查找包含特定字符串的文件名
如果想查找包含字符串 example 的所有文件,可以使用:
locate example
2、选项
locate 命令支持一些选项来调整其行为,包括:
- -i: 忽略大小写差异。
- -r: 使用正则表达式进行匹配。
- -0: 输出结果以 null 字符终止,这对于包含特殊字符的文件名特别有用。
- -b: 匹配词边界,确保匹配的是完整的单词。
- -d: 指定使用的数据库文件。
- -c: 显示匹配项的数量,但不显示实际文件名。
3、帮助获取
在命令行中输入指令:
locate --h
出现如下图所示的帮助信息:
六、示例
1、忽略大小写查找文件
locate -i example.txt
2、使用正则表达式查找以 .txt 结尾的文件
locate -r '\.txt$'
3、输出结果以 null 字符终止
查找包含 example 的文件名,并输出结果以 null 字符终止
locate -0 example
七、结合其他命令使用
locate 命令经常与其他命令一起使用来执行更复杂的任务。
例如,可以使用 xargs 命令来处理 locate 的输出结果,查找所有 .txt 文件,并删除它们,使用如下命令:
locate .txt | xargs rm
注意,在执行类似上述删除操作时要格外小心,因为这可能会导致误删重要文件。
八、其他事项
(1)locate 不会实时更新索引,因此可能无法找到最近修改或创建的文件。
(2)由于 locate 速度快,它非常适合用于脚本中。
(3)locate 是一个非常有用的命令,用于快速查找文件路径。虽然它不总是提供即时的结果,但在日常使用中非常方便。如果你需要查找文件系统的实时状态,可以考虑使用 find 命令。
文章正下方可以看到我的联系方式:鼠标“点击” 下面的 “威迪斯特-就是video system 微信名片”字样,就会出现我的二维码,欢迎沟通探讨。