`

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

 
阅读更多

(转自)http://blog.csdn.net/doc_sgl/article/details/8674150

我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索。
1、which 
语法: 

[root@redhat ~]# which 可执行文件名称 

which是通过 PATH环境变量到该路径内查找可执行文件,每个用户的环境变量可能不同,所以搜索出来的结果也可能不同。可能用echo $PATH查看当前环境变量。-a可以列出所有PATH目录下存在的文件,而不仅仅是找到的第一个文件。默认只显示找到的第一个文件。注意:which只会搜寻可执行文件,普通文件即使在$PATH变量路径中,也是查找不到的。

例如: 
[root@redhat ~]# which passwd 
/usr/bin/passwd 


2、whereis 
语法: 

[root@redhat ~]# whereis [-bmsu] 文件名

查找名中含有查找串的文件

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

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

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

3、 locate 
语法: 

[root@redhat ~]# locate 文件或者目录名称 

查找含有查找字串的文件和目录,如果是目录,就会将目录下的所有文件列举一遍分别作为一个查找结果。

例 如: 
[root@redhat ~]# locate passwd 
/home/weblogic/bea/user_projects/domains/zhanggongzhe112/myserver/stage/_appsdir_DB_war/DB.war/jsp/as/user/passwd.jsp
/home/weblogic/bea/user_projects/domains/zhanggongzhe112/myserver/stage/_appsdir_admin_war/admin.war/jsp/platform/passwd.jsp
/lib/security/pam_unix_passwd.so 
/lib/security/pam_passwdqc.so 
/usr/include/rpcsvc/yppasswd.x 
/usr/include/rpcsvc/yppasswd.h 
/usr/lib/perl5/5.8.5/i386-linux-thread-multi/rpcsvc/yppasswd.ph 
/usr/lib/kde3/kded_kpasswdserver.la 
/usr/lib/kde3/kded_kpasswdserver.so 
/usr/lib/ruby/1.8/webrick/httpauth/htpasswd.rb 
/usr/bin/vncpasswd 
/usr/bin/userpasswd 
/usr/bin/yppasswd 
………… 

4、 find 
语法: 
[root@redhat ~]# 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。 

 

locate 是在数据库里查找,可以查找含有字符串的文件和目录
whereis 在数据库中查找,可以查找含有字符串的文件,可以找到可执行命令和man page 
find 就是根据条件查找文件

which 根据环境变量PATH查找,一般只查找可执行文件,可以找到可执行文件和别名(alias)

分享到:
评论

相关推荐

    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下的文件查找类命令.pdf

    Linux下的文件查找类命令 Linux 操作系统中,文件查找类命令是系统管理员不可或缺的基本技能之一。在 Linux 系统中,有多种文件查找类命令,每个命令都有其特点和应用场景。本文将对 Linux 下的文件查找类命令进行...

    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常见命令与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 which命令的具体使用

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

    linux 命令全集

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

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

    在linux中,关于文件查找有如下三种命令: which 、find 、 locate 。其中find是应用最多,也是今天为大家着重介绍的命令。 which:用于命令查找。(which is 或者 whereis vim ) locate:文件查找,依赖数据库。 ...

    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命令搜索工具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基础知识(4): 文件搜索命令

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

    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...

    150个常用的Linux命令汇总

    3. whereis:搜索文件 4. locate:搜索文件 用户管理命令 1. useradd:添加新用户 2. usermod:修改用户信息 3. userdel:删除用户 4. groupadd:添加新组 5. passwd:修改用户密码 6. chage:修改用户密码过期...

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

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

    Linux命令笔记

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

    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系统总复习.txt

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

    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@cyb ~]# find /etc -iname “hos*”(查找/etc下所有开头是hos的文件) 按...

Global site tag (gtag.js) - Google Analytics