`
king_tt
  • 浏览: 2083758 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Linux下which、whereis、locate、find 区别

 
阅读更多

我们经常在linux要查找某个文件或命令,但不知道放在哪里了,可以使用下面的一些命令来搜索。
which 查看可执行文件的位置
whereis 查看文件的位置
locate 配合数据库查看文件位置
find 实际搜寻硬盘查询文件名称

1、which
语法:which 可执行文件名称
例如:
[root@redhat ~]# which passwd
/usr/bin/passwd
which是通过 PATH 环境变量到该路径内查找可执行文件,所以基本的功能是寻找可执行文件

2、whereis
语法:whereis [-bmsu] 文件或者目录名称
参数说明:
-b : 只找二进制文件
-m: 只找在说明文件manual路径下的文件
-s : 只找source源文件
-u : 没有说明文档的文件
例如:
[root@redhat ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz
将和passwd文件相关的文件都查找出来

[root@redhat ~]# whereis -b passwd
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd
只将二进制文件 查找出来

和find相比,whereis查找的速度非常快,这是因为linux系统会将系统内的所有文件都记录在一个数据库文件中,当使用whereis和下面即将介绍的locate时,会从数据库中查找数据,而不是像find命令那样,通过遍历硬盘来查找,效率自然会很高。
但是该数据库文件并不是实时更新,默认情况下时一星期更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。

3、 locate
语法:locate 文件或者目录名称
例 如:
[root@redhat ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/cron.daily/passwd
/etc/init/passwd.conf
/etc/init.d/passwd
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswd
…………


4、 find
语法:find 路径 参数
参数说明:
时间查找参数:
-atime n :将n*24小时内存取过的的文件列出来
-ctime n :将n*24小时内改变、新增的文件或者目录列出来
-mtime n :将n*24小时内修改过的文件或者目录列出来
-newer file :把比file还要新的文件列出来
名称查找参数:
-gid n :寻找群组ID为n的文件
-group name :寻找群组名称为name的文件
-uid n :寻找拥有者ID为n的文件
-user name :寻找用户者名称为name的文件
-name file :寻找文件名为file的文件(可以使用通配符)
例如:
[root@redhat ~]# find / -name zgz
/home/zgz
/home/zgz/zgz
/home/weblogic/bea/user_projects/domains/zgz
/home/oracle/product/10g/cfgtoollogs/dbca/zgz
/home/oracle/product/10g/cfgtoollogs/emca/zgz
/home/oracle/oradata/zgz


[root@redhat ~]# find / -name '*zgz*'
/home/zgz
/home/zgz/zgz1
/home/zgz/zgzdirzgz
/home/zgz/zgz
/home/zgz/zgzdir
/home/weblogic/bea/user_projects/domains/zgz
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00006
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00002
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00004
/home/weblogic/bea/user_projects/domains/zgz/zgz.log
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00008
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00005


当我们用whereis和locate无法查找到我们需要的文件时,可以使用find,但是find是在硬盘上遍历查找,因此非常消耗硬盘的资源,而且效率也非常低,因此建议大家优先使用whereis和locate。


总结:
which 只能查可执行文件和别名(alias) ,并在PATH变量里面寻找

whereis 只能查二进制文件(含可执行文件)、说明文档,源文件等,从linux文件数据库(/var/lib/slocate/slocate.db 或 /var/lib/mlocate/mlocate.db)寻找,所以有可能找到刚刚删除,或者没有发现新建的文件

locate 在数据库里查找,数据库大至每天更新一次,文件名是部分匹配(见 3 locate passwd 的结果:opasswd
find 最强大,什么都能查,根据条件查找文件,在硬盘上查找,效率很低



参考推荐:

Linux下which、whereis、locate、find 命令的区别

Linux Shell 常用命令与目录分区的学习总结

linux中的find 命令

Linux xargs命令

linux软链接和硬链接


分享到:
评论

相关推荐

    which,whereis,locate,find的用法与区别

    which,whereis,locate,find的用法与区别

    linux下which、whereis、locate、find命令的区别.docx

    linux下which、whereis、locate、find命令的区别.docx

    Linux下which、whereis、locate、find 命令的区别

    本文档详细介绍了在Linux系统下which、whereis、locate、find 命令的区别,具体的应用方法,结合案例,非常的实用

    Linux教程,主要内容:Linux 命令、Linux 系统运维、软件运维、精选常用Shell脚本.zip

    查看 Linux 命令帮助信息 - 关键词:help, whatis, info, which, whereis, man Linux 文件目录管理 - 关键词:cd, ls, pwd, mkdir, rmdir, tree, touch, ln, rename, stat, file, chmod, chown, locate, find, cp, ...

    Linux which命令的具体使用

    whereis 查看文件的位置。 locate 配合数据库查看文件位置。 find 实际搜寻硬盘查询文件名称。 01. 命令概述 查找环境变量中的文件 which 命令用于查找并显示给定命令的绝对路径,环境变量 PATH 中保存了查找...

    Linux系统配置及服务管理:文件查找

    (which is 或者 whereis vim ) locate:文件查找,依赖数据库。 而今天的主角find查找命令是在linux中最重要也是应用最多的,通常的用法是在指定目录里查找文件。 find的语法是 :find [ path…] [options] ...

    linux 命令全集

    这是因为 Linux 系统会将系统内的所有档案都记录在一个数据库档案里面,而当使用 whereis 或者是底下要说的 locate 时,都会以此数据库档案的内容为准,因此,有的时后你还会发现使用这两个执行档时,会找到已经被杀...

    Linux常见命令与shell脚本

    1.24 whereis和which查找命令所在目录 13 1.25 grep搜索文件内容 13 1.26 tar文档管理 14 1.27 gzip/gunzip 和 bzip2/bunzip2文件压缩/解压缩 15 1.28 unzip winzip文件解压缩 17 1.29 其他常用命令 17 2 vi编辑器 ...

    Linux命令搜索工具linux-command.zip

    diff、diffstat、file、find、git、gitview、ln、locate、lsattr、mattrib、mc、mcopy、mdel、mdir、mktemp、mmove、mread、mren、mshowfat、mtools、mtoolstest、mv、od、paste、patch、rcp、rhmask、rm、slocate...

    云计算Linux文件查找与压缩干货

    # whereis vim 二、任意文件 find 语法 find [path...] [options] [expression] [action] 命令 路径 选项 表达式 动作 ①按文件名: [root@qianfeng ~]# find /etc -name "hosts" ...

    Linux系统总复习.txt

    whereis 帮助文档 find -name -iname -size -user -atime -ctime -mtime -type -inum -perm -exec(查询结果中直接执行) locate 在数据库中按文件名查找 updatadb 强制更新数据库 grep -i -v -i 忽略大小写 -v ...

    Linux中的文件查找

    #whereis vim 任意文检查找find 语法 find [path…] [options] [expression] [action] 命令 路径 选项 表达式 动作 按文件名查找: [root@cyb ~]# find /etc -iname “hos*”(查找/etc下所有开头是hos的文件) 按...

    Linux命令大全(CHM格式离线版)

    whereis which cat chattr chgrp chmod chown cksum cmp cp cut indent 磁盘管理 cd df dirs du edquota eject lndir ls mcd mdeltree mdu mkdir mlabel mmd mmount mrd mzip pwd quota quotacheck quotaoff quotaon...

    linux-tutorial:Linux教程,主要内容:Linux命令,Linux系统运维,软件运维,精选常用Shell脚本

    help-关键词: help , whatis , info , which , whereis , man cd , ls , pwd , mkdir , rmdir , tree , touch , ln , rename , stat , file , chmod , chown , locate , find , cp , mv , ...

    Linux命令大全完整版

    whereis 104 which 105 cat 105 chattr(change attribute) 106 chgrp(change group) 106 chmod(change mode) 107 chown(change owner) 108 cksum(check sum) 109 cmp(compare) 109 cp(copy) 110 cut 111 indent...

    Linux基础知识(4): 文件搜索命令

    文章目录1 文件搜索——locate2 命令搜索——whereis与which3 文件名搜索——find命令4 字符串搜索——grep 注:转载请标明原文出处链接:https://xiongyiming.blog.csdn.net/article/details/105856510 1 文件...

    Linux命令大全

    whereis which cat chattr chgrp chmod chown cksum cmp cp cut indent 磁盘管理 cd df dirs du edquota eject lndir ls mcd mdeltree mdu mkdir mlabel mmd mmount mrd mzip pwd quota quotacheck quotaoff quotaon...

    Linux文件查找,打包,压缩,解压

    或者 whereis vim 任意文件 find 语法 find [path...] [options] [expression] [action] 命令 路径 选项 表达式 动作 按文件名 [root@qianfeng ~]# find /etc -name hosts [root@qianfeng ~]# find /etc -iname

    Linux—文件查找

    #whereis vim 二、任意文件 find 语法 find [path…] [options] [expression] [action] 命令 路径 选项 表达式 动作 按文件名: [root@qianfeng ~]# find /etc -name hosts [root@qianfeng ~]# find /etc -iname

    Linux命令笔记

    4:whereis 搜索命令所在目录级帮助文档路径 语法:whereis[命令名称] 5:grep 在文件中搜索字串匹配的行并输出 语法:grep -iv[指定字串][文件] 6:uname 显示当前操作系统名称 常用命令/压缩解压 1:gzip 压缩文件 ...

Global site tag (gtag.js) - Google Analytics