设为首页 收藏本站
查看: 492|回复: 0

[经验分享] 文件查找及find命令详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-6 14:54:05 | 显示全部楼层 |阅读模式
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:查找没有属组的文件;
例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@localhost tmp]# find /tmp/ -user centos -ls
654082    0 -rw-------   1 centos   root            0 8月 25 21:08 /tmp/yum.log
[iyunv@localhost tmp]# find /tmp/ -group centos -ls
654211    4 -rw-r--r--   1 root     centos        126 9月  1 12:46 /tmp/111
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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: 套接字
例子:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@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
[iyunv@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)
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@localhost tmp]# find /tmp/ -type f -a -nouser -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
[iyunv@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
[iyunv@localhost tmp]# find /tmp/ \( -nouser -a -group hadoop \) -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
[iyunv@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]
例子:
1
2
3
4
5
6
7
8
9
[iyunv@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表示最近文件有所改变的状态 ,如文件修改,属性\属主 改变 ,节点 ,链接变化等
例子:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@localhost /]# touch -m -t 201509031828 /tmp/lisir
[iyunv@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
[iyunv@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每一位权限都有读即可
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@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
[iyunv@localhost soft]# find /soft/ -perm -222 -ls
2354693    0 -rwxrwxrwx   1 root     root            0 9月  3 18:38 /soft/c
[iyunv@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 {} \;
      无需用户确认;
例子:
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@localhost soft]# find . -perm -222 -exec mv {} {}.a \;
[iyunv@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
[iyunv@localhost soft]# find . -perm -222 -exec chmod 777 {} \;
[iyunv@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




wKiom1XaZYDRBaG6AAKDc_jZeNg591.jpg

练习:
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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-110228-1-1.html 上篇帖子: Linux系统启动流程 下篇帖子: btrfs(Btree FS)文件系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表