Linux locate 命令
2022-05-12 本文已影响0人
追风骚年
在 Linux 中可能有时候需要查询磁盘上某个文件在什么位置,可能只是知道文件名,大多数情况通过 find
命令是可以解决问题,但是 find
命令在于每次都需要遍历磁盘文件树速度,所以文件越多速度就越慢,并且由于 find
功能尤为强大参数较多。有没有一个命令能快速查找,又简单明了,locate
就是解决这个问题的。
$ time find / -path *apache*
/var/lib/selinux/targeted/active/modules/100/apache
/var/lib/selinux/targeted/active/modules/100/apache/hll
/var/lib/selinux/targeted/active/modules/100/apache/lang_ext
/var/lib/selinux/targeted/active/modules/100/apache/cil
/usr/share/selinux/targeted/default/active/modules/100/apache
/usr/share/selinux/targeted/default/active/modules/100/apache/lang_ext
/usr/share/selinux/targeted/default/active/modules/100/apache/cil
/usr/share/bash-completion/completions/apache2ctl
/usr/lib/python3.6/site-packages/sos/report/plugins/apache.py
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.pyc
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.opt-1.pyc
real 0m1.254s
user 0m0.419s
sys 0m0.717s
$ time locate apache
/usr/lib/python3.6/site-packages/sos/report/plugins/apache.py
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.opt-1.pyc
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.pyc
/usr/share/bash-completion/completions/apache2ctl
/usr/share/selinux/targeted/default/active/modules/100/apache
/usr/share/selinux/targeted/default/active/modules/100/apache/cil
/usr/share/selinux/targeted/default/active/modules/100/apache/lang_ext
/var/lib/selinux/targeted/active/modules/100/apache
/var/lib/selinux/targeted/active/modules/100/apache/cil
/var/lib/selinux/targeted/active/modules/100/apache/hll
/var/lib/selinux/targeted/active/modules/100/apache/lang_ext
real 0m0.141s
user 0m0.130s
sys 0m0.007s
如果提示没有 .db 文件,
updatedb
可以生成 .db 文件,首次会比较慢
我的磁盘文件较少,这其中查询速度就已经相差一个数量级。
locate 还有一些其他参数
- 查询文件名
$ locate -b apache
/usr/lib/python3.6/site-packages/sos/report/plugins/apache.py
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.opt-1.pyc
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.pyc
/usr/share/bash-completion/completions/apache2ctl
/usr/share/selinux/targeted/default/active/modules/100/apache
/var/lib/selinux/targeted/active/modules/100/apache
b 是 basename
- 统计数量
$ locate -b -c apache
6
- 忽视大小写
$ locate -i apache
/usr/lib/python3.6/site-packages/sos/report/plugins/apache.py
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.opt-1.pyc
/usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/apache.cpython-36.pyc
/usr/share/bash-completion/completions/apache2ctl
/usr/share/selinux/targeted/default/active/modules/100/apache
/usr/share/selinux/targeted/default/active/modules/100/apache/cil
/usr/share/selinux/targeted/default/active/modules/100/apache/lang_ext
/var/lib/docker/overlay2/116ff1a457689fa9d31f045d4bb7d04d8791308e3ce6bb28b806d0634f5a580e/diff/usr/share/perl/5.20.2/CGI/Apache.pm
/var/lib/docker/overlay2/236df4c36580d0f590b7beb2421720a0afa9dc4459bc7fb7d9e6f9f1fc41d75c/diff/usr/share/common-licenses/Apache-2.0
/var/lib/docker/overlay2/2f0479cc846777a9c4f650680be86346cdda6ed1b040947e87324fab4e86034f/diff/usr/share/common-licenses/Apache-2.0
/var/lib/docker/overlay2/b899ddfdb2940ae3c37e654ed23c93c704365a9ea5599e0000123167a026b3d6/diff/usr/share/common-licenses/Apache-2.0
/var/lib/selinux/targeted/active/modules/100/apache
/var/lib/selinux/targeted/active/modules/100/apache/cil
/var/lib/selinux/targeted/active/modules/100/apache/hll
/var/lib/selinux/targeted/active/modules/100/apache/lang_ext
- 通过正则表达式查询
$ locate -i -r apache.py$
/usr/lib/python3.6/site-packages/sos/report/plugins/apache.py
Tip
但是 locate 也有一个问题在于如果文件被删除,locate 数据库还没有刷新的话,就会存在查找的文件已不再磁盘上,这个时候可以添加另外一个参数解决 -e
,但是更为合适做法是配置 cronjob,定时更新 locate 数据库。
$ updatedb
$ locate -S
数据库 /var/lib/mlocate/mlocate.db:
20,215 文件夹
174,859 文件
18,633,061 文件名中的字节数
4,898,343 字节用于存储数据库
updatedb
更新数据库,locate -S
数据库统计。