前言:

         实时查找工具,通过遍历指定路径下的文件系统完成文件查找。

工作特点:

查找速度略慢;

精确查找;

实时查找;

语法:

find [OPTION]... [查找路径] [查找条件] [处理动作]

查找路径:指定具体目标路径(可以指定多个);默认为当前目录;

查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件;

处理动作:对符合条件的文件做什么操作;默认输出至屏幕;

 

查找条件:

根据文件名查找:

-name "文件名称":支持使用glob

*, ?, [], [^]

-iname "文件名称":不区分字母大小写

-regex"PATTERN":以PATTERN匹配整个文件路径字符串,而不仅仅是文件名:(正则表达式)

[root@localhost ~]# find /etc -name "passwd"

/etc/pam.d/passwd

/etc/passwd

[root@localhost ~]# find /etc -name "*passwd"

/etc/pam.d/passwd

/etc/security/opasswd

/etc/passwd

         根据属主、属组查找:

         -user USERNAME:查找属主为指定用户的文件

[root@localhost ~]# ls -l /tmp/findfile/

总计 24

-rw-r--r-- 1 root   root   534 04-01 19:13 fstab

-rw-r--r-- 1 centos centos   47 04-01 19:16 issue

-rw-r--r-- 1 root   root  1672 04-01 19:13 passwd

[root@localhost ~]# find /tmp/findfile/-user centos

/tmp/findfile/issue

[root@localhost ~]# find /tmp/findfile/-user root

/tmp/findfile/

/tmp/findfile/passwd

/tmp/findfile/fstab

 

group GRPNAME: 查找属组为指定组的文件

[root@localhost ~]# find /tmp/findfile/-group root

/tmp/findfile/

/tmp/findfile/passwd

/tmp/findfile/fstab

 

-gid GroupID:查找属组为指定的GID号的文件

-uid UserID:查找属主为指定的UID号的文件

[root@localhost ~]# cat /etc/passwd | tail -n 1

centos:x:504:504::/home/centos:/bin/bash

[root@localhost ~]# find /tmp/findfile/ -gid 504

/tmp/findfile/issue

[root@localhost ~]# find /tmp/findfile/ -uid 504

/tmp/findfile/issue

[root@localhost ~]#

 

-nouser:查找没有属主的文件

[root@localhost ~]# ls -l /tmp/findfile/

总计 24

-rw-r--r-- 1 root  504  534 04-01 19:13 fstab

-rw-r--r-- 1  504  504  47 04-01 19:16 issue

-rw-r--r-- 1 root root 1672 04-01 19:13 passwd

[root@localhost ~]# find /tmp/findfile/ -nouser

/tmp/findfile/issue

 

-nogroup:查找没有属组的文件

[root@localhost ~]# ls -l /tmp/findfile/

总计 24

-rw-r--r-- 1 root  504  534 04-01 19:13 fstab

-rw-r--r-- 1  504  504  47 04-01 19:16 issue

-rw-r--r-- 1 root root 1672 04-01 19:13 passwd

 [root@localhost ~]# find/tmp/findfile/ -nogroup

/tmp/findfile/issue

/tmp/findfile/fstab

 

根据文件类型查找:

-type TYPE:

f: 普通文件

d: 目录文件

l: 符号链接文件

s:套接字文件

b: 块设备文件

c: 字符设备文件

p: 管道文件

[root@localhost ~]# ls -l /tmp/findfile/

总计 32

-rw-r--r-- 1 root  504  534 04-01 19:13 fstab

-rw-r--r-- 1  504  504  47 04-01 19:16 issue

-rw-r--r-- 1 root root 1672 04-01 19:13 passwd

drwxr-xr-x 2 root root 4096 04-01 19:32 test

[root@localhost ~]# find /tmp/findfile/ -type f

/tmp/findfile/issue

/tmp/findfile/passwd

/tmp/findfile/fstab

[root@localhost ~]# find /tmp/findfile/ -type d

/tmp/findfile/

/tmp/findfile/test

 

组合条件:

与:-a

或:-o

非:-not, !

!A -a !B = !(A -o B)

!A -o !B = !(A -a B)

[root@localhost ~]# find /tmp/findfile/ -not \( -user root -o -name'fstab' \) -ls

917568    8 -rw-r--r--   1 504     504            47  4月  1 19:16 /tmp/findfile/issue

[root@localhost ~]# find /tmp/findfile/ \( -not -user root -a -not -name'fstab' \) -ls

917568    8 -rw-r--r--   1 504     504            47  4月  1 19:16 /tmp/findfile/issue

根据文件大小来查找:

-size [+|-]#UNIT

常用单位:k, M, G

#UNIT: (#-1, #] :#-1到#的内容

-#UNIT:[0,#-1]

+#UNIT:(#,oo)

[root@localhost ~]# find /var/ -size 3k -exec ls -lh {} \;

-rw-r--r-- 1 root root 2.1K 2014-09-30 /var/cache/yum/extras/repomd.xml

-rw------- 1 root root 2.5K 03-07 22:52 /var/log/maillog.4

-rw------- 1 root root 2.6K 04-02 04:02 /var/log/maillog

根据时间戳:

以“天”为单位;

-atime [+|-]#, 

#: [#,#+1)

+#: [#+1,oo]

-#: [0,#)

-mtime

-ctime

以“分钟”为单位:

-amin

-mmin

-cmin

[root@localhost ~]# find /var/ -atime 2 -ls

2392999    8 -rw-r-----   1 root    lp             84  3月30 21:45 /var/cache/cups/job.cache

2392998    8 -rw-r-----   1 root    lp             75  3月30 21:45 /var/cache/cups/remote.cache

 

根据权限查找:

-perm [/|-]MODE

MODE: 精确权限匹配

/MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可;

-MODE:每一类对象都必须同时拥有为其指定的权限标准;