您的位置:首页 > 运维架构 > Linux

linux如何查找文件或者目录的位置相关命令汇总

2013-07-25 11:03 1316 查看

1、使用locate命令。

locate命令基于数据库查找,第一次运行时需首先运行updatedb命令更新数据库。

语法:locate <filename>

例如查找stdio.h文件在如:

[python]
view plaincopyprint?

[root@localhost sys]# locate stdio.h 

/usr/include/stdio.h 
/usr/include/bits/stdio.h 
/usr/include/boost/iostreams/filter/stdio.hpp 
/usr/include/c++/4.4.4/tr1/stdio.h 

/usr/include/glib-2.0/glib/gstdio.h 

/usr/include/libgsf-1/gsf/gsf-infile-stdio.h 

/usr/include/libgsf-1/gsf/gsf-input-stdio.h 

/usr/include/libgsf-1/gsf/gsf-outfile-stdio.h 

/usr/include/libgsf-1/gsf/gsf-output-stdio.h 

/usr/lib/i686-redhat-linux5E/include/stdio.h 
/usr/lib/i686-redhat-linux5E/include/bits/stdio.h 
/usr/lib/perl5/CORE/nostdio.h 
/usr/share/man/man0p/stdio.h.0p.gz 

[root@localhost sys]# locate stdio.h
/usr/include/stdio.h
/usr/include/bits/stdio.h
/usr/include/boost/iostreams/filter/stdio.hpp
/usr/include/c++/4.4.4/tr1/stdio.h
/usr/include/glib-2.0/glib/gstdio.h
/usr/include/libgsf-1/gsf/gsf-infile-stdio.h
/usr/include/libgsf-1/gsf/gsf-input-stdio.h
/usr/include/libgsf-1/gsf/gsf-outfile-stdio.h
/usr/include/libgsf-1/gsf/gsf-output-stdio.h
/usr/lib/i686-redhat-linux5E/include/stdio.h
/usr/lib/i686-redhat-linux5E/include/bits/stdio.h
/usr/lib/perl5/CORE/nostdio.h
/usr/share/man/man0p/stdio.h.0p.gz


2、使用find命令。

语法:find <起始目录> <搜索条件> <动作>

默认起始目录是.(当前目录)。

默认动作是输出文件名。

[python]
view plaincopyprint?

[root@localhost sys]# find /usr/ -name stdio.h 

/usr/lib/i686-redhat-linux5E/include/stdio.h 
/usr/lib/i686-redhat-linux5E/include/bits/stdio.h 
/usr/include/c++/4.4.4/tr1/stdio.h 

/usr/include/stdio.h 
/usr/include/bits/stdio.h 

[root@localhost sys]# find /usr/ -name stdio.h
/usr/lib/i686-redhat-linux5E/include/stdio.h
/usr/lib/i686-redhat-linux5E/include/bits/stdio.h
/usr/include/c++/4.4.4/tr1/stdio.h
/usr/include/stdio.h
/usr/include/bits/stdio.h

搜索出来的结果有区别,原因何在???

3、whereis

语法:whereis <filename>

功能:除给出文件全路径外,同时给出可执行文件对应的源码及man page文件。

[python]
view plaincopyprint?

[root@localhost sys]# whereis stdio.h 

stdio: /usr/include/stdio.h /usr/share/man/man3/stdio.3.gz 

[root@localhost sys]# whereis stdio.h
stdio: /usr/include/stdio.h /usr/share/man/man3/stdio.3.gz


4、which

语法:which <filename>

功能:在PATH环境变量列出的路径中搜索指定的文件,给出文件全路径。

5.whatis

whatis command examples

Whatis command displays a single line description about a command.

[python]
view plaincopyprint?

[root@localhost ~]# whatis ifconfig 

ifconfig             (8)  - configure a network interface 

[root@localhost ~]# whatis ifconfig
ifconfig             (8)  - configure a network interface


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/y
af57
ppasswd.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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: