码迷,mamicode.com
首页 > 其他好文 > 详细

命令-find

时间:2017-03-15 19:33:14      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:search   files   

find 命令


【NAME】

     find - search for files in a directory hierarchy

      搜索目录中的文件层次结构

功能:

      实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找;

工作特性:

      查找速度略慢;

      精确查找;

      实时查找;

SYNOPSIS

     find [OPTIONS] [查找起始路径] [查找条件] [处理动作]
          查找起始路径:指定具体搜索目标起始路径;默认为当前目录;
          查找条件:指定的查找标准,可以根据文件名、大小、类型、从属关系、权限等等标准进行;默认为找出指定路径下的所有文件;
          处理动作:对符合查找条件的文件做出的操作,例如删除等操作;默认为输出至标准输出;

          查找条件:
               表达式:选项和测试
               测试:结果通常为布尔型("true", "false")

OPTIONS

1.1 根据文件名查找

      -name  "pattern":指定字符串作为寻找文件或目录的范本样式。
      -iname "pattern":忽略字符大小写,指定字符串作为寻找文件或目录的范本样式。
           支持glob风格的通配符;
                 *, ?, [], [^]
      -regex pattern:基于正则表达式模式查找文件,匹配是整个路径,而非其名;

1.2 根据文件从属关系查找

      -user USERNAME:查找属主指定用户的所有文件;
      -group GRPNAME:查找属组指定组的所有文件;
 
      -uid UID:查找属主指定的UID的所有文件;
      -gid GID:查找属组指定的GID的所有文件;
 
      -nouser:查找没有属主的文件;
      -nogroup:查找没有属组的文件;

 1.3 根据文件的类型查找

      -type TYPE:
            f: 普通文件
            d: 目录文件
            l:符号链接文件
            b:块设备 文件
            c:字符设备文件
            p:管道文件
            s:套接字文件

1.4 组合测试

     与:-a, 默认组合逻辑;
     或:-o
     非:-not, !

1.5 根据文件的大小查找

      -size [+|-]#UNIT
            常用单位:k, M, G
            #UNIT:(#-1, #]
            -#UNIT:[0,#-1]
            +#UNIT:(#, oo)

1.5 根据时间戳查找

(1)以“天”为单位:
            -atime  [+|-]#       atime:访问时间
                  #:[#, #-1)
                  -#:(#, 0]
                  +#:(oo, #-1]
            -mtime:修改时间           
            -ctime:改变时间
(2)以“分钟”为单位:
            -amin
            -mmin
            -cmin

1.6 根据权限查找

      -perm  [/|-]mode
            mode:精确权限匹配;
            /mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足;
                  9位权限之间存在“或”关系;
      -mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足;
                  9位权限之间存在“与”关系;   

1.7 处理动作

      -print:输出至标准输出;默认的动作;
      -ls:类似于对查找到的文件执行“ls -l”命令,输出文件的详细信息;
      -delete:删除查找到的文件;
      -fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中;
      -ok COMMAND {} \;   :对查找到的每个文件执行由COMMAND表示的命令;每次操作都由用户进行确认;
            find ./ -nouser -a -nogroup -ok chown root:root {} \;
      -exec COMMAND {} \;  :对查找到的每个文件执行由COMMAND表示的命令;
            find ./ -perm /002 -exec mv {} {}.danger \;

      注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;
            但是有些命令不能接受过长的参数,此时命令执行会失败;另一种方式可规避此问题:
                  find | xargs COMMAND
其它选项:
      -ls:假设find命令的返回值为true,就将文件或目录列出到标准输出,格式类似ls命令加上"ils"参数,但每个名称之前都有"./"字符串。
      -maxdepth<目录层级>:设置最大目录层级。
      -mindepth<目录层级>:设置最小目录层级。

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

EXAMPLES
实例1:根据文件名查找
  1. [root@zck ~]# find /etc/ -name "passwd"
  2. /etc/passwd
  3. /etc/pam.d/passwd
  4. [root@zck ~]# find /etc -name "passwd"
  5. /etc/passwd
  6. /etc/pam.d/passwd
  7. [root@zck ~]# mkdir /etc/test
  8. [root@zck ~]# touch /etc/test/Passwd
  9. [root@zck ~]# touch /etc/test/MPASSWD.txt
  10. [root@zck ~]# find /etc -iname "passwd" #-i,忽略大小写
  11. /etc/passwd
  12. /etc/pam.d/passwd
  13. /etc/test/Passwd
  14. [root@zck ~]# find /etc -name "passwd"
  15. /etc/passwd
  16. /etc/pam.d/passwd
  17. [root@zck ~]# find /etc -name "passwd*"
  18. /etc/passwd
  19. /etc/passwd-
  20. /etc/pam.d/passwd
  21. [root@zck ~]# find /etc -name "*passwd"
  22. /etc/passwd
  23. /etc/security/opasswd
  24. /etc/pam.d/passwd
  25. [root@zck ~]# touch /etc/test/npasswd
  26. [root@zck ~]# touch /etc/test/npasswdo
  27. [root@zck ~]# find /etc -name "*passwd"
  28. /etc/passwd
  29. /etc/security/opasswd
  30. /etc/pam.d/passwd
  31. /etc/test/npasswd
  32. [root@zck ~]# find /etc -name "passwd?"
  33. /etc/passwd-
  34. [root@zck ~]# find /etc -name "?passwd"
  35. /etc/security/opasswd
  36. /etc/test/npasswd
  37. [root@zck ~]# touch /etc/test/passwdx
  38. [root@zck ~]# find /etc -name "passwd?"
  39. /etc/passwd-
  40. /etc/test/passwdx
  41. [root@zck ~]# find /etc -name "passwd[[:alnum:]]" #查找passwd结尾即可是字母又可是数字的字符
  42. /etc/test/passwdx
  43. [root@zck ~]# cd /etc/test/
  44. [root@zck test]# find -name "passwd*" -o -iname "mpasswd.txt" #不加路径,默认是查找当前目录下文件
  45. ./MPASSWD.txt
  46. ./passwdx
  47. [root@zck test]# find -name "passwd*" -o -name "mpasswd.txt" #-o:表示或,两个参数只要符合一个即可
  48. ./passwdx
  49. [root@zck test]# find -name "passwd*" -o -iname "mpasswd*"
  50. ./MPASSWD.txt
  51. ./passwdx
实例2:根据文件从属关系查找
  1. [root@zck ~]# find /tmp -user "moosefs"   #查找/etc目录下,所属用户是"mosefs"的文件
  2. find: moosefs is not the name of a known user   #提示没有些用户的该用
  3. [root@zck ~]# find /tmp -user user1 -ls    #查找/tmp目录下,所属用户是"user1"的文件
  4. 206262276    0 -rw-rw-r--   1 user1    user1           0 Aug 22 11:03 /tmp/test1.txt
  5. 206262277    0 -rw-rw-r--   1 user1    user1           0 Aug 22 11:03 /tmp/test2.txt
  6. [root@zck ~]# find /tmp -group user3 -ls   #查找/tmp目录下,所属组是"user3"的文件
  7. 206262277    0 -rw-rw-r--   1 user3    user3           0 Aug 22 11:03 /tmp/test2.txt
  8. [root@zck ~]# find /tmp -nouser -ls   #查找/tmp目录下,没有所属用户的文件
  9. 206262276    0 -rw-rw-r--   1 1006     root            0 Aug 22 11:03 /tmp/test1.txt
  10. [root@zck ~]# find /tmp -nogroup -ls   #查找/tmp目录下,没有所属组的文件
  11. 206262277    0 -rw-rw-r--   1 root     1007            0 Aug 22 11:03 /tmp/test2.txt
  12. [root@zck ~]# find /tmp -uid 1006 -ls    #查找/tmp目录下,uid是1006的文件
  13. 206262276    0 -rw-rw-r--   1 1006     root            0 Aug 22 11:03 /tmp/test1.txt
  14. [root@zck ~]# find /tmp -gid 1007 -ls    #查找/tmp目录下,gid是1007的文件
  15. 206262277    0 -rw-rw-r--   1 root     1007            0 Aug 22 11:03 /tmp/test2.txt
  16. [root@zck ~]# find /tmp -uid +500 -ls #查找/tmp目录下,uid大于500的文件 138156780 0 drwx------ 2 roo roo 6 Aug 11 15:37 /tmp/.esd-1000 206262276 0 -rw-rw-r-- 1 1006 root 0 Aug 22 11:03 /tmp/test1.txt
实例3:根据文件类型查找
  1. [root@zck ~]# find /dev -type b -ls
  2. 10674 0 brw-rw---- 1 root cdrom 11, 0 Aug 12 14:39 /dev/sr0
  3. 10661 0 brw-rw---- 1 root disk 8, 3 Aug 12 14:39 /dev/sda3
  4. 10660 0 brw-rw---- 1 root disk 8, 2 Aug 12 14:39 /dev/sda2
  5. 10659 0 brw-rw---- 1 root disk 8, 1 Aug 12 14:39 /dev/sda1
  6. 10655 0 brw-rw---- 1 root disk 8, 0 Aug 12 14:39 /dev/sda
  7. [root@zck ~]# find /dev -type c -ls 19289 0 crw------- 1 root root 10, 57 Aug 12 14:39 /dev/vsock 19145 0 crw-rw---- 1 root tty 7, 134 Aug 12 14:39 /dev/vcsa6
实例4:根据文件的大小查找
  1. [root@zck ~]# find /tmp -size 180k #查找/tmp目录下大小是180k的文件
  2. /tmp/messages
  3. [root@zck ~]# find /tmp -size -180k #查找/tmp目录下小于180k的文件
  4. /tmp
  5. /tmp/.font-unix
  6. /tmp/.X11-unix
  7. /tmp/.X11-unix/X0
  8. /tmp/.Test-unix
  9. /tmp/.ICE-unix
  10. /tmp/.ICE-unix/2410
  11. /tmp/.XIM-unix
  12. /tmp/.esd-0
  13. /tmp/.esd-1000
  14. /tmp/test
  15. /tmp/test/a
  16. /tmp/test/c
  17. /tmp/test/d
  18. /tmp/test/f
  19. /tmp/test/g
  20. /tmp/test/b.danger
  21. /tmp/test/e.danger
  22. /tmp/test1.txt
  23. /tmp/test2.txt
  24. /tmp/.X0-lock
  25. [root@zck ~]# find /tmp -size +180k #查找/tmp目录下大于180k的文件
  26. /tmp/messages.2

实例5:根据时间戳查找
  1. [root@zck ~]# find /tmp -atime +7 -ls #查找/tmp目录下7天之前访问过的文件
  2. [root@zck ~]# find /tmp -mtime -1 -ls #查找/tmp目录下1天之内修改过的文件
  3. [root@zck ~]# find /tmp -amin +60 #查找/tmp目录下60分钟之前修改过的文件
  4. [root@zck ~]# find /tmp -mmin -60 #查找/tmp目录下60分钟之内修改过的文件
实例6:根据权限查找
  1. mode精确权限匹配:
  2. [root@zck ~]# mkdir /tmp/test
  3. [root@zck ~]# cd /tmp/test/
  4. [root@zck test]# touch a b c d e f g
  5. [root@zck test]# chmod 640 a
  6. [root@zck test]# chmod 666 b
  7. [root@zck test]# chmod 440 c
  8. [root@zck test]# chmod 775 d
  9. [root@zck test]# chmod 777 e
  10. [root@zck test]# ll
  11. total 0
  12. -rw-r-----. 1 root root 0 Aug 22 16:23 a
  13. -rw-rw-rw-. 1 root root 0 Aug 22 16:23 b
  14. -r--r-----. 1 root root 0 Aug 22 16:23 c
  15. -rwxrwxr-x. 1 root root 0 Aug 22 16:23 d
  16. -rwxrwxrwx. 1 root root 0 Aug 22 16:23 e
  17. -rw-r--r--. 1 root root 0 Aug 22 16:23 f
  18. -rw-r--r--. 1 root root 0 Aug 22 16:23 g
  19. [root@zck test]# find ./ -perm 644 -ls
  20. 205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
  21. 205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
  22. [root@zck test]# find ./ -perm 666 -ls
  23. 205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
  24. /mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足;
  25. [root@zck test]# find ./ -perm /666 -ls
  26. 205118792 0 drwxr-xr-x 2 root root 62 Aug 22 16:23 ./
  27. 205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 ./a
  28. 205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
  29. 205118798 0 -r--r----- 1 root root 0 Aug 22 16:23 ./c
  30. 205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 ./d
  31. 205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 ./e
  32. 205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
  33. 205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
  34. [root@zck test]# find ./ -perm /222 -ls
  35. 205118792 0 drwxr-xr-x 2 root root 62 Aug 22 16:23 ./
  36. 205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 ./a
  37. 205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
  38. 205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 ./d
  39. 205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 ./e
  40. 205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
  41. 205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
  42. -mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足;
  43. [root@zck test]# find ./ -perm -222 -ls #查找当前目录所有用户才有写权限的文件
  44. 205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
  45. 205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 ./e
  46. [root@zck test]# find ./ -not -perm -222 -ls #查找当前目录至少有一个用户有写权限的文件
  47. 205118792 0 drwxr-xr-x 2 root root 62 Aug 22 16:23 ./
  48. 205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 ./a
  49. 205118798 0 -r--r----- 1 root root 0 Aug 22 16:23 ./c
  50. 205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 ./d
  51. 205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
  52. 205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
实例5:查找/tmp目录下文件大小为0,或目录下没有任何子目录或文件的空目录
  1. [root@zck test]# find /tmp -empty -ls
  2. 135772013 0 drwxrwxrwt 2 root root 6 Jul 27 17:31 /tmp/.font-unix
  3. 68273776 0 drwxrwxrwt 2 root root 6 Jul 27 17:31 /tmp/.Test-unix
  4. 202211348 0 drwxrwxrwt 2 root root 6 Jul 27 17:31 /tmp/.XIM-unix
  5. 205130530 0 drwx------ 2 root root 6 Aug 11 15:46 /tmp/.esd-0
  6. 138156780 0 drwx------ 2 roo roo 6 Aug 11 15:37 /tmp/.esd-1000
  7. 206262276 0 -rw-rw-r-- 1 1006 root 0 Aug 22 11:03 /tmp/test1.txt
  8. 206262277 0 -rw-rw-r-- 1 root 1007 0 Aug 22 11:03 /tmp/test2.txt
  9. 205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 /tmp/test/a
  10. 205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 /tmp/test/b
  11. 205118798 0 -r--r----- 1 root root 0 Aug 22 16:23 /tmp/test/c
  12. 205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 /tmp/test/d
  13. 205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 /tmp/test/e
  14. 205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 /tmp/test/f
  15. 205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 /tmp/test/g

练习:
1、找出/tmp目录下属主为非root的所有文件;
  1. [root@CentOS7-171 ~]# find /tmp -not -user root -ls
  2. 139050703    0 drwxr-xr-x   2 tomcat   tomcat         18 Feb  5 22:40 /tmp/hsperfdata_tomcat
  3. 139052243   32 -rw-------   1 tomcat   tomcat      32768 Mar 10 15:16 /tmp/hsperfdata_tomcat/63194
  4. 136064490    0 drwxr-----   3 3003     3003           74 Mar  1 14:34 /tmp/skel
  5. 136064504    4 -rw-r-----   1 3003     3003           18 Mar  1 14:34 /tmp/skel/.bash_logout
  6. 136064508    4 -rw-r-----   1 3003     3003          193 Mar  1 14:34 /tmp/skel/.bash_profile
  7. 136064512    4 -rw-r-----   1 3003     3003          231 Mar  1 14:34 /tmp/skel/.bashrc
  8. 205899564    0 drwxr-----   4 3003     3003           37 Mar  1 14:34 /tmp/skel/.mozilla
  9. 1028311    0 drwxr-----   2 3003     3003            6 Mar  1 14:34 /tmp/skel/.mozilla/extensions
  10. 68961524    0 drwxr-----   2 3003     3003            6 Mar  1 14:34 /tmp/skel/.mozilla/plugins
  11. 1028313    4 -rw-r-----   1 archlinux mygrp         511 Mar  1 15:09 /tmp/inittab
2、找出/tmp目录下文件名中不包含fstab字符串的文件;
  1. [root@CentOS7-171 ~]# find -not -name "*fstab*" -ls
3、找出/tmp目录下属主为非root,而且文件名不包含fstab字符串的文件;
  1. [root@CentOS7-171 ~]# find /tmp -not -user root -a -not -name "*fstab*" -ls
  2. 139050703 0 drwxr-xr-x 2 tomcat tomcat 18 Feb 5 22:40 /tmp/hsperfdata_tomcat
  3. 139052243 32 -rw------- 1 tomcat tomcat 32768 Mar 10 15:20 /tmp/hsperfdata_tomcat/63194
  4. 136064490 0 drwxr----- 3 3003 3003 74 Mar 1 14:34 /tmp/skel
  5. 136064504 4 -rw-r----- 1 3003 3003 18 Mar 1 14:34 /tmp/skel/.bash_logout
  6. 136064508 4 -rw-r----- 1 3003 3003 193 Mar 1 14:34 /tmp/skel/.bash_profile
  7. 136064512 4 -rw-r----- 1 3003 3003 231 Mar 1 14:34 /tmp/skel/.bashrc
  8. 205899564 0 drwxr----- 4 3003 3003 37 Mar 1 14:34 /tmp/skel/.mozilla
  9. 1028311 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/extensions
  10. 68961524 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/plugins
  11. 1028313 4 -rw-r----- 1 archlinux mygrp 511 Mar 1 15:09 /tmp/inittab

  1. [root@CentOS7-171 ~]# find /tmp -not \( -user root -o -name "*fstab*" \) -ls
  2. 139050703 0 drwxr-xr-x 2 tomcat tomcat 18 Feb 5 22:40 /tmp/hsperfdata_tomcat
  3. 139052243 32 -rw------- 1 tomcat tomcat 32768 Mar 10 15:21 /tmp/hsperfdata_tomcat/63194
  4. 136064490 0 drwxr----- 3 3003 3003 74 Mar 1 14:34 /tmp/skel
  5. 136064504 4 -rw-r----- 1 3003 3003 18 Mar 1 14:34 /tmp/skel/.bash_logout
  6. 136064508 4 -rw-r----- 1 3003 3003 193 Mar 1 14:34 /tmp/skel/.bash_profile
  7. 136064512 4 -rw-r----- 1 3003 3003 231 Mar 1 14:34 /tmp/skel/.bashrc
  8. 205899564 0 drwxr----- 4 3003 3003 37 Mar 1 14:34 /tmp/skel/.mozilla
  9. 1028311 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/extensions
  10. 68961524 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/plugins
  11. 1028313 4 -rw-r----- 1 archlinux mygrp 511 Mar 1 15:09 /tmp/inittab
4、查找当前系统上没有属主或属组,且最近-周内曾被访问过的文件或目录;
  1. [root@CentOS7-171 test]# find / \( -nouser -o -nogroup \) -atime -7 -ls

5、查找/etc/目录下大于1M,且类型为普通文件的所有文件;
  1. [root@CentOS7-171 test]# find /etc/ -size +1M -type f -exec ls -lh {} \;
  2. -r--r--r--. 1 root root 6.7M Aug 31 2016 /etc/udev/hwdb.bin
  3. -rw-r--r--. 1 root root 1.4M Nov 22 2015 /etc/gconf/schemas/ekiga.schemas
  4. -rw-r--r--. 1 root root 3.7M Nov 21 2015 /etc/selinux/targeted/policy/policy.29
  5. -rw-r--r--. 1 root root 1.4M Mar 6 2015 /etc/brltty/zh-tw.ctb

6、查找/etc/目录下所有用户都没有写权限的文件;
  1. [root@CentOS7-171 test]# find /etc/ -not -perm /222 -type f -ls

7、查找/etc/目录至少有一类用户没有执行权限的文件;
  1. [root@CentOS7-171 test]# find /etc -not -perm -111 -type f -ls

8、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的所有文件;
  1. [root@CentOS7-171 test]# find /etc/init.d/ -perm -113 -type f -ls

命令-find

标签:search   files   

原文地址:http://zhucke.blog.51cto.com/180229/1906808

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!