文件查找之locate、whereis和which

1.locate

  文件查找命令,所属软件包mlocate

       不同于find命令是在整块磁盘中搜索,locate命令在数据库文件(/var/lib/mlocate/mlocate.db)中查找,mlocate.db是定期更新的,不是实时更新的,但是可以通过updatedb命令手动触发更新

       find是默认精确匹配,locate则是默认模糊匹配

 

       updatedb命令

              用户更新/var/lib/mlocate/mlocate.db

              所使用配置文件/etc/updatedb.conf

              该命令在后台cron计划任务中定期执行

 

[root@VM_0_13_centos ~]# locate xml|more
/etc/firewalld/lockdown-whitelist.xml
/etc/firewalld/zones/public.xml
/etc/firewalld/zones/public.xml.old
/root/test/test.xml
/root/test/test1/1.xml
/root/test/test1/test11/11.xml
/root/test/test1/test12/12.xml
/root/test/test1/test13/13.xml
/root/test/test2/2.xml
/root/test/test2/test21/21.xml
/root/test/test2/test22/22.xml
/root/test/test2/test23/23.xml
/root/test/test3/3.xml
/root/test/test3/test31/31.xml
/root/test/test3/test32/32.xml
/root/test/test3/test33/33.xml
/usr/bin/iptables-xml
/usr/bin/xmlcatalog
/usr/bin/xmllint
/usr/bin/xmlwf
/usr/lib/firewalld/xmlschema

[root@VM_0_13_centos ~]# touch test_locate.txt
[root@VM_0_13_centos ~]# locate test_locate.txt
[root@VM_0_13_centos ~]# updatedb
[root@VM_0_13_centos ~]# locate test_locate.txt
/root/test_locate.txt

 

2.whereis

  查找某个命令的二进制程序文件、帮助文档、源代码文件

  选项以及含义

    -b    只返回二进制文件

    -m    只返回帮助文件

    -s    只返回源代码文件

[root@VM_0_13_centos ~]# whereis cp
cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz
[root@VM_0_13_centos ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz

 

3.which

  仅查找二进制程序文件以及其别名

[root@VM_0_13_centos ~]# which nginx
/usr/sbin/nginx
[root@VM_0_13_centos ~]# which cp 
alias cp='cp -i'
/usr/bin/cp

 

4.查找命令使用场景推荐

命令 适用场景 优缺点
find 查找某一类文件,比如文件名部分一致 功能强大、速度慢
locate 只能查找单个文件 功能单一、速度慢
whereis 查找程序的可执行文件、帮助文档等 不常用
which 只查找程序的可执行文件 常用于查找程序的可执行文件的绝对路径

 

posted @ 2019-03-09 13:09  小宇子2B  阅读(176)  评论(0)    收藏  举报