> 请关注本头条号,每天坚持更新原创干货技术文章。> 如需学习视频,请在微信搜索公众号“智传网优”直接开始自助视频学习。
1. locate命令简介
Linux locate命令用于查找符合条件的文档,他会去保存文档和目录名称的数据库内,查找合乎范本样式条件的文档或目录。速度非常快,但不是数据库只是缓存,并不是实时。如果是实时,请先更新数据库文件,执行命令updatedb,然后再查找。
![b7b4632986948fcb86d574e4f0d61ec1.png](https://img-blog.csdnimg.cn/img_convert/b7b4632986948fcb86d574e4f0d61ec1.png)
2. 安装mlocate工具
以CentOS为例,最小化安装默认并不包含该组件:
[root@zcwyou ~]# yum -y install mlocate
有以下输出即可证明安装成功
总下载量:86 k
Installed size: 279 k
下载软件包:
mlocate-0.22.2-6.el6.x86_64.rpm | 86 kB 00:00
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
正在安装 : mlocate-0.22.2-6.el6.x86_64 1/1
Verifying : mlocate-0.22.2-6.el6.x86_64 1/1
已安装:
mlocate.x86_64 0:0.22.2-6.el6
完毕!
![be54502d2b01f35156e3d62b4c9c98ed.png](https://img-blog.csdnimg.cn/img_convert/be54502d2b01f35156e3d62b4c9c98ed.png)
3. 更新数据库
[root@zcwyou ~]# updatedb
原理是更新文件信息数据库,CentOS7数据库的位置在/var/lib/locatedb,CentOS6数据库位置在/var/lib/mlocate/mlocate.db,这个数据库中含有本地所有文件信息。
![e344101f5505f7d4624561df29b9b85f.png](https://img-blog.csdnimg.cn/img_convert/e344101f5505f7d4624561df29b9b85f.png)
4. 在数据库里查询文件信息
查找以tar.gz结尾的文件
[root@zcwyou ~]# locate *.tar.gz
查找结果
/root/wordpress-4.9.4-zh_CN.tar.gz
![b42c07f61cb03ad0208e53e5f5030dc5.png](https://img-blog.csdnimg.cn/img_convert/b42c07f61cb03ad0208e53e5f5030dc5.png)
查找以exe结尾的文件
[root@zcwyou ~]# locate *.exe
查询结果
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/pip/_vendor/distlib/t32.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/pip/_vendor/distlib/t64.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/pip/_vendor/distlib/w32.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/pip/_vendor/distlib/w64.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/setuptools/cli-32.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/setuptools/cli-64.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/setuptools/cli.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/setuptools/gui-32.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/setuptools/gui-64.exe
/opt/eff.org/certbot/venv/lib/python3.4/site-packages/setuptools/gui.exe
/usr/lib/python2.6/site-packages/pip/_vendor/distlib/t32.exe
/usr/lib/python2.6/site-packages/pip/_vendor/distlib/t64.exe
/usr/lib/python2.6/site-packages/pip/_vendor/distlib/w32.exe
/usr/lib/python2.6/site-packages/pip/_vendor/distlib/w64.exe
/usr/lib/python2.6/site-packages/pip-10.0.1-py2.6.egg/pip/_vendor/distlib/t32.exe
/usr/lib/python2.6/site-packages/pip-10.0.1-py2.6.egg/pip/_vendor/distlib/t64.exe
/usr/lib/python2.6/site-packages/pip-10.0.1-py2.6.egg/pip/_vendor/distlib/w32.exe
/usr/lib/python2.6/site-packages/pip-10.0.1-py2.6.egg/pip/_vendor/distlib/w64.exe
/usr/lib64/python2.6/distutils/command/wininst-6.0.exe
/usr/lib64/python2.6/distutils/command/wininst-7.1.exe
/usr/lib64/python2.6/distutils/command/wininst-8.0.exe
/usr/lib64/python2.6/distutils/command/wininst-9.0-amd64.exe
/usr/lib64/python2.6/distutils/command/wininst-9.0.exe
/usr/lib64/python3.4/distutils/command/wininst-10.0-amd64.exe
/usr/lib64/python3.4/distutils/command/wininst-10.0.exe
/usr/lib64/python3.4/distutils/command/wininst-6.0.exe
/usr/lib64/python3.4/distutils/command/wininst-7.1.exe
/usr/lib64/python3.4/distutils/command/wininst-8.0.exe
/usr/lib64/python3.4/distutils/command/wininst-9.0-amd64.exe
/usr/lib64/python3.4/distutils/command/wininst-9.0.exe
![fe33493868c0b3f8839f155cf8db7fec.png](https://img-blog.csdnimg.cn/img_convert/fe33493868c0b3f8839f155cf8db7fec.png)
点击了解更多,快速查看更多的技术文章列表。
同步于:
https://www.linuxrumen.com/cyml/260.html