find、grep,which、whereis locate

1. find

find is the most common and powerful find command, you can use it to find any file you are looking for.

The usage format of find is as follows:

  $ find <specified directory> <specified condition> <specified action>

  - <specified directory>: The directory to be searched and all its subdirectories. Defaults to the current directory.

  - <specified condition>: The characteristics of the file to be searched.

  - <Specified Action>: Perform specific processing on search results.

If no parameters are added, find searches the current directory and its subdirectories by default, and does not filter any results (that is, returns all files), and displays them all on the screen.

Use examples of find:

  $ find . -name "my*"

Search the current directory (including subdirectories, the same below), all files whose file names begin with my.

  $ find . -name "my*" -ls

Searches the current directory for all files whose names begin with my, and displays their details.

  $ find . -type f -mmin -10

Search the current directory for all regular files that have been updated in the past 10 minutes. If the -type f parameter is not added, it will search for ordinary files + special files + directories.

 

2. locate

The locate command is actually another way of writing "find -name", but it is much faster than the latter because it does not search a specific directory, but a database (/var/lib/locatedb), which contains All local file information. The Linux system automatically creates this database and automatically updates it once a day, so the latest changed files cannot be found using the locate command. To avoid this situation, you can use the updatedb command to manually update the database before using locate .

An example of the use of the locate command:

  $ locate /etc/sh

Search for all files starting with sh in the etc directory.

  $ locate ~/m

Search for all files starting with m in the user's home directory.

  $ locate -i ~/m

搜索用户主目录下,所有以m开头的文件,并且忽略大小写。

3. whereis

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

whereis命令的使用实例:

  $ whereis grep

4. which

which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

which命令的使用实例:

  $ which grep

5. type

type命令其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令。

type命令的使用实例:

  $ type cd

系统会提示,cd是shell的自带命令(build-in)。

  $ type grep

系统会提示,grep是一个外部命令,并显示该命令的路径。

  $ type -p grep

加上-p参数后,就相当于which命令。

 

 

 

which       查看可执行文件的位置 
whereis    查看文件的位置 
locate       配 合数据库查看文件位置 
find          实际搜寻硬盘查询文件名称 

 

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

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命令那样,通 过遍历硬盘来查找,效率自然会很高。 
但是该数据库文件并不是实时更新,默认情况下时一星期更新一次,因此,我们在用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 可以找到可执行文件和别名(alias)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324842567&siteId=291194637