Linux文件查找命令具体解释-which whereis find locate

2023-05-30,,

原创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个经典使用场景

http://blog.csdn.net/hello_hwc/article/details/40017833

Linux文件查找命令具体解释-which whereis find locate的相关教程结束。

《Linux文件查找命令具体解释-which whereis find locate.doc》

下载本文的Word格式文档,以方便收藏与打印。