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

文件查找及find命令详解

时间:2015-09-03 21:51:31      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:find   linux   文本查找   

find

1、文件查找:

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


2、文件查找:locate, find

    locate: 非实时查找工具;依赖于事先构建的索引;索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新此数据库(updatedb);查找速度快;模糊查找;

find:实时查找;查找速度略慢;精确查找;


3、find命令使用方法:

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

    查找路径:默认为当前路径;

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

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


4、查找条件:

  • 根据文件名进行查找:

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

      *, ?, []

      -iname "文件名称":不区分字符大小写,支持使用glob;

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


  • 根据属主、属组查找:

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

      -group GROUPNAME: 

      -uid UserID: 查找文件的属主指定uid的文件;

      -gid GroupID: 

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

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

例子:

[root@localhost tmp]# find /tmp/ -user centos -ls
654082    0 -rw-------   1 centos   root            0 8月 25 21:08 /tmp/yum.log
[root@localhost tmp]# find /tmp/ -group centos -ls
654211    4 -rw-r--r--   1 root     centos        126 9月  1 12:46 /tmp/111
[root@localhost tmp]# find /tmp/ -user centos -a -group centos -ls
654210   20 -rw-r--r--   1 centos   centos      19697 4月 10 00:44 /tmp/functions
[root@localhost tmp]# find /tmp/ -uid 3008 -ls
654210   20 -rw-r--r--   1 3008     3008        19697 4月 10 00:44 /tmp/functions
654082    0 -rw-------   1 3008     root            0 8月 25 21:08 /tmp/yum.log
[root@localhost tmp]# find /tmp/ -nouser -ls
654210   20 -rw-r--r--   1 3008     3008        19697 4月 10 00:44 /tmp/functions
654082    0 -rw-------   1 3008     root            0 8月 25 21:08 /tmp/yum.log
[root@localhost tmp]# find /tmp/ -nouser -a -nogroup -ls
654210   20 -rw-r--r--   1 3008     3008        19697 4月 10 00:44 /tmp/functions


  • 根据文件类型进行查找:

      -type TYPE

      f: 普通文件

      d: 目录

      l: 符号链接

      b: 块设备

      c: 字符设备

      p: 命名管道

      s: 套接字

例子:

[root@localhost tmp]# find /tmp/ -type l -ls
654205    0 lrwxrwxrwx   1 root     root           22 8月 25 21:24 /tmp/grub.conf -> ../boot/grub/grub.conf
[root@localhost /]# find /tmp/ -type d -ls
915714    4 drwxr-xr-x   9 root     root         4096 9月  3 18:09 /tmp/
915721    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/89123jk4
915715    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/jkashd
915719    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/sdfhk
915718    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/1892373
915717    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/kjashd
915720    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/90124h3kjshdr
915716    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/kjashdkj


  • 组合查找条件:

      与条件:-a

      或条件:-o

      非条件:-not, !

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

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

例子:

[root@localhost tmp]# find /tmp/ -type f -a -nouser -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
[root@localhost tmp]# find /tmp/ \( -nouser -o -user lisir \) -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
915723    4 -rw-r--r--   1 lisir    lisir01       957 8月 25 13:35 /tmp/fstab
915722   20 -rw-r--r--   1 lisir    3012        19697 4月 10 00:44 /tmp/functions
[root@localhost tmp]# find /tmp/ \( -nouser -a -group hadoop \) -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
[root@localhost tmp]# find /etc ! -name "*.conf" -a -not -type f -ls 
392449   12 drwxr-xr-x 117 root     root        12288 9月  3 18:14 /etc
393096    4 drwxr-xr-x   2 root     root         4096 8月 25 21:12 /etc/purple
393295    0 lrwxrwxrwx   1 root     root           15 8月 25 21:13 /etc/rc.sysinit -> rc.d/rc.sysinit
394019    4 drwxr-xr-x   2 root     root         4096 8月 25 21:16 /etc/pcmcia
393055    4 drwxr-xr-x   2 root     root         4096 8月 25 21:12 /etc/obex-data-server
393034    4 drwxr-xr-x   3 root     root         4096 8月 25 21:11 /etc/ghostscript
393040    4 drwxr-xr-x   2 root     root         4096 8月 25 21:11 /etc/ghostscript/8.70
392886    4 drwxr-xr-x   2 root     root         4096 8月 25 21:11 /etc/wpa_supplicant
392787    4 drwxr-xr-x   2 root     root         4096 8月 25 21:11 /etc/ssl


  • 根据文件大小来查找:

      -size [+|-]#UNIT

      单位:k, M, G

      +#UNIT: (#,+oo)

      -#UNIT:[0,#-1]

例子:

[root@localhost /]# find /etc/ -size 3k -exec ls -lh {} \;
-rw-r--r--. 1 root root 2.9K 8月  21 2010 /etc/pinforc
-rwxr-xr-x. 1 root root 2.6K 4月  10 00:44 /etc/sysconfig/network-scripts/ifup-tunnel
-rwxr-xr-x. 1 root root 2.4K 4月  10 00:44 /etc/sysconfig/network-scripts/ifup-post
-rw-r--r--. 1 root root 2.2K 7月  24 16:22 /etc/sysconfig/nfs
-rw-r--r--. 1 root root 2.6K 7月  24 19:32 /etc/sysconfig/cpuspeed
-rw-r--r--. 1 root root 2.6K 5月  20 22:18 /etc/sysconfig/raid-check
-rw-------. 1 root root 2.3K 8月  25 21:25 /etc/lvm/backup/VolGroup
-r--r--r--. 1 root root 2.2K 7月  24 12:04 /etc/lvm/profile/command_profile_template.profile


  • 根据时间戳:

    (1)以“天”为单位

      -atime [+|-]#   

      :[#,#+1)

      +#: [#+1,oo]

      -#: [0,#)

      #:   

      -mtime

      -ctime

      +3大于三天 -3三天以内  3大于等三4天以内

    (2)以“分钟”为单位

      -amin

      -mmin

      -cmin

注:a表示最近访问文件的时间、m表示最近修改文件内容的时间、c表示最近文件有所改变的状态 ,如文件修改,属性\属主 改变 ,节点 ,链接变化等

例子:

[root@localhost /]# touch -m -t 201509031828 /tmp/lisir
[root@localhost /]# stat /tmp/lisir
  File: "/tmp/lisir"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d    Inode: 915725      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-09-03 18:28:23.640000731 +0800
Modify: 2015-09-03 18:28:00.000000000 +0800
Change: 2015-09-03 18:28:23.640000731 +0800
[root@localhost /]# find /tmp/ -mtime -3 -ls
915725    0 -rw-r--r--   1 root     root            0 9月  3 18:28 /tmp/lisir


  • 根据权限:

      -perm [/|-]MODE

      MODE: 精确权限匹配

      /MODE或+MODE:任何一类对象(u,g,o)的任何一位权限符合条件即可;隐含或条件;

      /444或+444:表示只要符合u,g,o任何一个权限有读取即可

      -MODE:为每一类对象指定的每一位权限都必须同时存在方为符合条件;隐含与条件;

      -444:表示必须符合u,g,o每一位权限都有读即可

例子:

[root@localhost soft]# find /soft/ -perm /222 -ls
2354690    4 drwxr-xr-x   2 root     root         4096 9月  3 18:38 /soft/
2354691    0 -rwx------   1 root     root            0 9月  3 18:38 /soft/a
2354692    0 -rwxrwx---   1 root     root            0 9月  3 18:38 /soft/b
2354693    0 -rwxrwxrwx   1 root     root            0 9月  3 18:38 /soft/c
2354694    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/d
2354695    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/e
[root@localhost soft]# find /soft/ -perm -222 -ls
2354693    0 -rwxrwxrwx   1 root     root            0 9月  3 18:38 /soft/c
[root@localhost soft]# find /soft/ ! -perm -222 -ls
2354690    4 drwxr-xr-x   2 root     root         4096 9月  3 18:38 /soft/
2354691    0 -rwx------   1 root     root            0 9月  3 18:38 /soft/a
2354692    0 -rwxrwx---   1 root     root            0 9月  3 18:38 /soft/b
2354694    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/d
2354695    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/e


5、处理动作:

    -print: 默认处理动作;

    -ls:类似于对查找到的每个文件做"ls -l"的操作;

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

    -fls /path/to/somefile:查找到的文件的详细路径信息保存至指定文件中;


    -ok COMMAND {} \;

      对每个文件执行指定的命令之前需要用户事先确认;

    -exec COMMAND {} \;

      无需用户确认;

例子:

[root@localhost soft]# find . -perm -222 -exec mv {} {}.a \;
[root@localhost soft]# ll
总用量 0
-rwx------. 1 root root 0 9月   3 18:43 a
-rwxrwx---. 1 root root 0 9月   3 18:43 b
-rwxrwxrwx. 1 root root 0 9月   3 18:43 c.a
[root@localhost soft]# find . -perm -222 -exec chmod 777 {} \;
[root@localhost soft]# ll
总用量 0
-rwx------. 1 root root 0 9月   3 18:43 a
-rwxrwx---. 1 root root 0 9月   3 18:43 b
-rwxrwxrwx. 1 root root 0 9月   3 18:43 c.a


技术分享


练习:

1、查找/var目录属主为root,且属组为mail的所有文件;

   答:find /var -user root -group mail -ls

2、查找/usr目录下不属于root、bin或hadoop的所有文件;
    答:find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls

3、查找/etc目录下最近一周内其内容修改过,且属主不为root或hadoop的所有文件;
     答:find /etc -ctime -7 -a -not -user root -not -user hadoop -ls

4、查找当前系统上没有属主或属组,且最近一周内曾被访问过的所有文件;

     答:find / \( -nouser -o -nogroup \) -a -atime -7 -ls

5、查找/etc目录下大于20k且类型为普通谁的的所有文件;

     答:find /etc -type f -a -size +20k -exec ls -lh {} \;

6、查找/etc目录下所有用户都没有写权限的文件;

     答:find /etc -not -perm /222 -ls

7、查找/etc目录下至少有一类用户没有执行权限的文件;

    答:find /etc -not -perm -111 -ls

8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户拥有写权限的文件

    答:find /etc/init.d/ -perm /111 -a -perm /002 -ls


本文出自 “做自己想做的事!” 博客,请务必保留此出处http://807257775.blog.51cto.com/2448194/1691132

文件查找及find命令详解

标签:find   linux   文本查找   

原文地址:http://807257775.blog.51cto.com/2448194/1691132

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