find:实时、精确、支持众多查找标准、遍历制定目录中的所有文件完成查找

find    查找路径    查找标准    查找到以后的处理动作

查找路径    默认为当前目录

查找标准    默认为路径下的所有文件(子目录中的内容也显示)

处理动作    默认为显示出来打印

匹配标准

   -name   'filename'  对文件名做精确匹配

   文件名通配   find /tmp/ -name "*.sh" -exec chmod o-x {} \;

       *   任意长度任意字符 开头 'passwd*'

       ?

       []

       find /tmp/ -name "hah[a-z].sh"

       -iname  'filename' 对文件名做精确匹配

       -regex  PATTERN       基于正则表达式进行文件名匹配

       -user   USERNAME    基于用户名进行查找

       -group  Group          基于属组名查找

       -uid    num               基于UID查找,主要用于用户被删除以后,以前用户的文件会变为UID

            -nouser             查找没有属主的文件

            -type   fdcblps     根据文件类型查找

            -size   [+|-]#k      find /etc -size +2k  -ls

                                #M

                                #G

组合条件

          -a      与   find /tmp -nouser -a -type d -ls            没有属主内容是目录的

          -o      或   find /tmp -nouser -o -type d -ls

         -not    非   find /tmp -not -type d                          查找非目录的文件

              find /tmp -not -type dc                                    查找非目录非套接字文件

              find /home -not -user usr1 -a -not -user usr2   不是usr1和usr2的文件

              find /home -not \( -user usr1 -o -user usr2 \)

              find /home -not -user usr1 -o -not -type d      属主不是usr1或者类型不是目录,

                                                                              (显示既是目录,属主又是usr1之外的文件)

              find /home -not \( -user usr1 -a -type d \)

       -mtime  访问

       -ctime  改变

       -atime  修改

       -mmin

       -cmin

       -amin

            [+|-]5

            5   五天前那天访问过

            -   五天之内曾经访问过

           +   至少五天没有访问过(5分钟之前访问过的)

      -perm   -mode   根据权限每一位精确查找,3个rwx位必须完全包含

             find /tmp -perm 644 精切匹配

             644包含755不包含750

             rwxr-xr-x     rw-r--r--  rwxr-x---

             find /tmp -perm -644

             find /tmp -perm -001    其他用户能执行的文件

             find /tmp -perm -007    其他用户能读写执行的文件

      -perm   /mode   匹配其中一个就可以   任意一位匹配满足条件rwx

             644包含755不包含750

             rwxr-xr-x     rw-r--r--  rwxr-x---

             find /tmp -perm /061    属组可读可写或其他人可执行的文件    

             find /tmp -perm /644    只要符合其中一位就可以

               如果文件权限是002,就无法匹配,因为三个位置没有一个相等的

处理动作

        -print  显示

        -ls     类似ls -l形式显示每个文件的详细

        -ok COMMAND {} \;       find /shell -user usr1 -exec chown root {} \; 提示是否执行

        -exec COMMAND {} \;     find /shell -user usr1 -exec chown root {} \;

        | xargs  find /etc/ -type f -a -size +2k -a -size -5k |  xargs echo {} >> /tmp/largefiles

练习

1
2
3
4
5
6
7
8
9
10
11
12
13
1、把在 /etc 目录中类型为普通文件大于2k小于3k的文件复制到 /tmp/haha 目录中
      find  /etc/  - type  f -a -size +2k -a -size -5k - exec  cp  {}  /tmp/haha  \;
2、把 /tmp 目录下文件类型为目录的文件属组和其他人赋予执行权限
      find  /tmp  - type  d - exec  chmod  go+x {} \;
3、把tmp下属组具有写权限的文件后面加.new
      find  /tmp  -perm -020 - exec  mv  {} {}.new \;
4、把 /tmp 目录下以.sh结尾的文件的其他人的执行权限去掉
      find  /tmp/  -name  "*.sh"  - exec  chmod  o-x {} \;
5、把在 /etc 目录中类型为普通文件大于2k小于3k的文件名写入 /tmp/largefiles 文件中
      - exec  用法    每个文件名一行
      find  /etc/  - type  f -a -size +2k -a -size -5k - exec  echo  {} >>  /tmp/largefiles  \;
      xargs  用法  一行文件, 文件名以空格隔开
      find  /etc/  - type  f -a -size +2k -a -size -5k |  xargs  echo  {} >>  /tmp/largefiles