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

Linux文件查找命令详解-which whereis find locate

2014-11-01 12:17 966 查看
原创BLog,转载请注明出处

http://blog.csdn.net/hello_hwc?viewmode=contents



which命令

首先查看man which的说明

which - shows the full path of (shell) commands.


在$PATH目录下查找命令的绝对路径,PATH配置不同,查找的结果也不同

查看系统的PATH

[root@localhost testForCsdn]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
使用举例

[root@localhost testForCsdn]# which ls
alias ls='ls --color=tty'
        /bin/ls
过滤掉alias中的信息

[root@localhost testForCsdn]# which --skip-alias ls
/bin/ls


whereis命令

man whereis
 whereis  -  locate  the binary, source, and manual page files
       for a command
从db中查找系统特定二进制文件,源文件,和manual page文件

说明:db是系统文件信息的快照,保存在本地数据库里,查找速度会很快、db每隔一段时间更新,如果要手动更新,使用命令

updatedb
选项

-b : 只找二进制文件

-m: 只找在说明文件manual路径下的文件

-s : 只找source源文件

-u : 没有说明文档的文件

举例

1、列出与init相关的文件

[root@localhost testForCsdn]# whereis init
init: /sbin/init /etc/init.d /usr/share/man/man8/init.8.gz


2、只查找二进制文件

[root@localhost testForCsdn]# whereis -b init
init: /sbin/init /etc/init.d


locate

从db中,这里的db和whereis中的db一样,找出系统中与指定名称所有的相关文件

常用选项

-b 仅仅匹配base name

-c 统计数目

-r 正则匹配

-w 匹配完整的路径名称

-d 指定DBPATH,不用默认的/var/lib/mlocate/mlocate.db

举例

[root@localhost ~]# locate -c init
601
[root@localhost ~]# locate -bc init
486
<pre name="code" class="plain">[root@localhost ~]# locate init | mroe



find

从磁盘上查找文件,查找时可以指定路

1、-name通过名称查找

[root@localhost ~]# find /etc -name init
/etc/sysconfig/init


2、-size通过大小查找

[root@localhost ~]# find testForCsdn/ -size -2048
testForCsdn/
testForCsdn/file.txt
testForCsdn/file.softlink
testForCsdn/file.hardlink


3、 -user通过所有者查找

[root@localhost ~]# find testForCsdn/ -user root
testForCsdn/
testForCsdn/file.txt
testForCsdn/file.softlink
testForCsdn/file.hardlink


4、-ctime/mtime/atime -cmin/mmin/amin

c change 如权限改变

a access 访问

m modify 修改

time按照天为单位

min按照分钟为单位

查找一小时内访问过的文件

[root@localhost ~]# find testForCsdn/ -amin -60
testForCsdn/
testForCsdn/file.txt
testForCsdn/file.softlink
testForCsdn/file.hardlink


5、-type 按照类型来查找

常用类型

f 二进制

l 软连接

d 目录

[root@localhost ~]# find testForCsdn/ -type l
testForCsdn/file.softlink


6、-inum

查找指定inode的文件

查找指定inode然后产出

查找指定inode然后删除该文件

find / -inum  15 –exec rm –rf {} \;


7、-a -r

a and

r or

多个条件同时查找

查找大小小于2M并且类型是软连接的文件

[root@localhost ~]# find testForCsdn/ -size -2048 -a -type l
testForCsdn/file.softlink


8、exec对查找的结果进行处理

查找大小小于2M并且类型是软连接的文件,然后显示详细信息

[root@localhost ~]# find testForCsdn/ -size -2048 -a -type l -exec ls -l {} \;
lrwxrwxrwx 1 root root 8 Oct 24 20:32 testForCsdn/file.softlink -> file.txt
解释下:这里的{}是查找的结果,分号;代表结果。用\进行转义

Grep

附上之前写的一篇链接

Grep的9个经典使用场景

/article/1330142.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: