文件查找:

    在文件系统上查找符合条件的文件;

    文件查找:locate,find

        非实时查找(数据库查找):locate

        实时查找:find

    locate:

        依赖于事先构建的索引;索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新数据库(updatedb);

find:

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

工作特点:

    查找速度略慢;

    精确查找;

    实时查找;

语法:

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

       查找路径:指定具体目标路径:默认为当前目录;

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

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

     查找条件:

            根据文件名查找:

     -name "文件名称":支持使用glob(通配符)

             *,?,[],[^]

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

    -regex "PATTERN":以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称;

根据属主、属组查找:

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

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

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

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

根据文件类型查找:

    -type TYPE:

        f:普通文件

        d:目录文件

        l:符号链接文件

        s:套接字文件

        b:块设备文件

        c:字符设备文件

        p:管道文件

组合条件:

    与:-a

    或:-o

    非:-not,!

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

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

根据文件大小来查找:

    -size [+|-]#UNIT

        常用单位:k,M,G

        #UNIT:(#-1,#)

        -#UNIT:[0,#-1]

        +#UNIT:(#,oo)

根据时间戳:

    以“天”为单位;

    访问时间-atime [+|-]#,用户最近一次访问文件的时间

         #:[#,#+1)

        +#:[#+1,oo]

        -#:[)

    修改时间-mtime,文件内容最后一次被修改的时间

    变化时间-ctime,文件元数据(例如权限或所有权)

    以“分钟”为单位;

        -amin

        -mmin

        -cmin

根据权限查找:

    -perm [+|-]MODE

        MODE:精确权限匹配

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

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

       +mode有或关系,-MODE有与关系。

处理动作:

    -print:默认的处理动作,显示至屏幕

    -ls:类似于对查找到的文件执行"ls -l"的命令

    -delete:删除查找到的文件;

    -fls /path/to/somefile:查找到所有文件的长格式信息保存至指定文件中

    -ok COMMAND {} \;对查找到的每个文件执行由COMMAND指定的命令;

        对于每个文件执行命令之前,都会交互式要求用户确认;

    -exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令

        {}:用于引用查找到的文件名称自身;

注意:find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令;有些命令不能接受过多的参数,此时命令执行可能会失败,另一种方式可规避此问题;

find | xargs COMMAND

注意:3表示大于等于3,但小于4,+3大于等于4,-3等于大于等于0,小于3


示例:

查找属主为root的

[root@note1 ~]# find /home -user root
/home
/home/zdw/scr2.awk
/home/zdw/ip.txt
/home/zdw/config
[root@note1 ~]# find /home -user root -ls         -ls为查找会执行该命令
781830    4 drwxr-xr-x   6 root     root         4096 Sep  3 14:14 /home
782691    4 -rwxr--r--   1 root     root           77 May  3 15:59 /home/zdw/scr2.awk
782686    4 -rw-r--r--   1 root     root          898 Apr 30 10:28 /home/zdw/ip.txt
782692    4 -rw-r--r--   1 root     root          449 May  5 21:54 /home/zdw/config

查找属组为zjj的

[root@note1 ~]# cp /etc/fstab /tmp
[root@note1 ~]# chown root:zjj /tmp/fstab
[root@note1 ~]# find /tmp -group zjj -ls
791883    4 -rw-r--r--   1 root     zjj           779 Sep  4 12:23 /tmp/fstab

UID,GID查找

[root@note1 ~]# id zdw
uid=500(zdw) gid=500(zdw) groups=500(zdw)
[root@note1 ~]# find /tmp -uid 500 -ls          #uid是500的
782693    0 -rw-r--r--   1 zdw      zdw             0 May  9 15:46 /tmp/996.sh
[root@note1 ~]# find /tmp -gid 500 -ls           #gid是500的
782693    0 -rw-r--r--   1 zdw      zdw             0 May  9 15:46 /tmp/996.sh

查找没有属主和属组的(因为把用户名删除了,只显示UID)

[root@note1 ~]# chown zjj:zjj /tmp/fstab      #先把文件属主属组改为zjj
[root@note1 ~]# ll /tmp/fstab                        #查看
-rw-r--r-- 1 zjj zjj 779 Sep  4 12:23 /tmp/fstab
[root@note1 ~]# userdel zjj                             #删除zjj用户
[root@note1 ~]# ll /tmp/fstab                         #属主和属组变成数字
-rw-r--r-- 1 502 502 779 Sep  4 12:23 /tmp/fstab
[root@note1 ~]# find /tmp/fstab -nouser -ls        #查找没有属主的
791883    4 -rw-r--r--   1 502      502           779 Sep  4 12:23 /tmp/fstab
[root@note1 ~]# find /tmp/fstab -nogroup -ls     #查找没有属组的
791883    4 -rw-r--r--   1 502      502           779 Sep  4 12:23 /tmp/fstab

查找类型为目录的

[root@note1 ~]# find /tmp -type d -ls
781825    4 drwxrwxrwt   4 root     root         4096 Sep  4 12:23 /tmp
791845    4 drwxr-xr-x  17 root     root         4096 Aug 12 23:40 /tmp/mylinux
791849    4 drwxr-xr-x   2 root     root         4096 Aug 12 23:40 /tmp/mylinux/bin
791864    4 drwxr-xr-x   2 root     root         4096 Aug 12 23:40 /tmp/mylinux/sys

使用或,查找没有属主或属组的

[root@note1 ~]# find /tmp -nouser -o -nogroup -ls            #只对nogroup做ls
[root@note1 ~]# find /tmp \( -nouser -o -nogroup \) -ls     #对这二个条件做ls
791883    4 -rw-r--r--   1 502      502           779 Sep  4 12:23 /tmp/fstab

找出属主不是root,文件名不是fstab的文件

[root@note1 ~]# find /tmp \( -not -name 'fstab' -a -not -user root \) -ls
782693    0 -rw-r--r--   1 zdw      zdw             0 May  9 15:46 /tmp/996.sh
[root@note1 ~]# find /tmp -not \( -name 'fstab' -a -user root \) -ls         #把not移动外面,这个不成,要把-a换成-o,相当于集合的概念
781825    4 drwxrwxrwt   4 root     root         4096 Sep  4 12:23 /tmp
791809  128 -rw-r--r--   1 root     root       127304 May 20 10:41 /tmp/services_2016-05-20-10.tar.gz
[root@note1 ~]# find /tmp -not \( -name 'fstab' -o -user root \) -ls         #换成-o就成了
782693    0 -rw-r--r--   1 zdw      zdw             0 May  9 15:46 /tmp/996.sh

exec指定执行动作

[root@C7-1 ~]# find /var -size 3k -exec ls -lh {} \;
-rw-r--r--. 1 root root 2.3K 8月  27 01:54 /var/lib/yum/rpmdb-indexes/obsoletes
-rw-r--r--. 1 root root 3.0K 9月   4 13:49 /var/lib/NetworkManager/dhclient-3eb5d8c2-542d-4e69-b553-5b602aad1c86-eno16777736.lease
-rw-------. 1 root root 2.1K 8月  27 01:54 /var/log/yum.log
-rw-r--r--. 1 root root 2.7K 6月  10 2014 /var/cache/man/cat1/cp.1.gz

表示2天内修改过的文件

[root@note1 ~]# find /etc -mtime -2 -ls
390914    4 drwxr-xr-x  75 root     root         4096 Sep  4 15:38 /etc
390920    4 -rw-r--r--   1 root     root          779 Sep  4 15:23 /etc/fstab
397502    4 -rw-r--r--   1 root     root         1186 Sep  4 15:29 /etc/passwd
+8表示至少有9天没有动过
[root@note1 ~]# find /tmp -mtime +8 -ls

权限查找

精确查找,查找权限为600的文件

[root@C7-1 ~]# find /etc -perm 600 -ls
67108995    0 -rw-------   1 root     root            0 7月 21 23:35 /etc/crypttab
239216    4 -rw-------   1 root     root           78 6月 10  2014 /etc/ppp/chap-secrets

查找有写权限的

[root@C7-1 ~]# find /etc -perm +222 -ls      #C7使用+查找不出来,要使用下面的/(C7不成,C6可以)
[root@C7-1 ~]# find /etc -perm /222 -ls
67108993   12 drwxr-xr-x  75 root     root         8192 9月  2 23:11 /etc
67108994    4 -rw-r--r--   1 root     root          465 7月 21 23:35 /etc/fstab
67108995    0 -rw-------   1 root     root            0 7月 21 23:35 /etc/crypttab

只检查其他用户同时有写和执行,属主和属组不关心

[root@cat ~]# find /etc -perm -003 -ls
67108996    0 lrwxrwxrwx   1 root     root           17 7月 21 23:35 /etc/mtab -> /proc/self/mounts
 59793    0 lrwxrwxrwx   1 root     root           40 7月 21 23:35 /etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts

把查找出的有执行权限的文件属主改为zjj

[root@cat tm]# find /tm -perm -001 -ls -exec chown zjj {} \;
530199    4 drwxr-xr-x   3 root     root         4096 Aug 13 19:32 /tm
530200    4 drwxr-xr-x  14 root     root         4096 Aug 13 19:32 /tm/mylinux
[root@cat tm]# find /tm -perm -001 -ls
530199    4 drwxr-xr-x   3 zjj      root         4096 Aug 13 19:32 /tm
530200    4 drwxr-xr-x  14 zjj      root         4096 Aug 13 19:32 /tm/mylinux

查找5分钟内修改过的文件

[root@cat tm]# touch test
[root@cat tm]# find /tm -cmin -5 -ls
530199    4 drwxr-xr-x   3 zjj      root         4096 Sep  5 11:58 /tm
534803    0 -rw-r--r--   1 root     root            0 Sep  5 11:58 /tm/test

把符合条件的文件,改成*.new

[root@cat tm]# find /tm -cmin -5 -type f -exec mv {} {}.new \;   #第一个{}表示每一个被操作的文件,第二个{}代表要改名的。{}引用找到的文件
[root@cat tm]# find /tm -cmin -5 -type f
/tm/test.new


生产环境使用

    把shell目录下txt文件中包含zhaodongwei内容的替换成xiaoqiang

[root@bogon shell]# find . -type f -name "*.txt"|xargs sed -i 's#zhaodongwei#xiaoqiang#g'

[root@bogon shell]# cat 2.txt 

xiaoqiang.com

#使用-exec更改

[root@bogon shell]# find . -type f -name "*.txt" -exec sed -i 's#xiaoqiang#zhaodongwei#g' {} \;

[root@bogon shell]# cat 1.txt 2.txt 

zhaodongwei

zhaodongwei.com

    找到所有的txt和sh文件

1、文件名查找

[root@bogon shell]# find . \( -name "*.txt" -o -name "*.sh" \)

2、使用正则查找

[root@bogon shell]# find . -regex ".*\(\.txt\|\.sh\)$"

3、使用iregex忽略大小写正则查找

[root@bogon shell]# find . -iregex ".*\(\.txt\|\.SH\)$"

    查找不是txt的文件

 [root@bogon shell]# find . ! -name "*.txt"

    基于目录深度的搜索

[root@pop3 shell]# find . -maxdepth 1 -name "f*" -print 

./f1.sh

./f2.sh

./finduser.sh

./function.sh

./f3.sh

[root@pop3 shell]# find . -maxdepth 2 -name "f*" -print 

./f1.sh

./f2.sh

./shell/finduser.sh

./finduser.sh

./function.sh

./f3.sh

[root@pop3 shell]# find . -mindepth 2 -name "f*" -print       

./shell/finduser.sh

[root@pop3 shell]# find . -mindepth 1 -name "f*" -print 

./f1.sh

./f2.sh

./shell/finduser.sh

./finduser.sh

./function.sh

./f3.sh

    查找后删除指定的文件

[root@pop3 shell]# find . -type f -name "*.sh"

./finduser.sh

[root@pop3 shell]# find . -type f -name "*.sh" -delete