9. find以及文件特殊权限

文件查找:locate,find

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

               实现工具:locate,find

     locate:依赖于事先构建好的索引库:

                       系统自动实现(周期性任务)

                       手动更新数据库(updatedb)

         工作特性:

                    查找速度快

                    模糊查找

                    非实时查找

               locate [OPTION]... [PATTERN]...

                   -b:只匹配路径中的基名

                   -c:统计处有多少符合条件的文件

                   -r:正则表达式

              注意:索引构建过程需要遍历整个根文件系统,极消耗资源

     find: 

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

            工作特性:

                  查找速度略慢

                  精确查找。

                  实时查找

          用法:

              find [OPTIONS] [查找起始路径] [查找条件] [执行的动作] 

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

                   查找条件:指定的查找标准,可根据,文件名,大小,类型,从属关系,权限等标准进行;默认为找出指定路径下的所有文件

                   处理动作:对符合查找条件的文件作出的操作,例如删除等操作;默认为输出到标准输出

         

          查找条件:

                   表达式:选项和测试

                   测试:结果通常为布尔型(“true”,“false”)

                        根据文件名查找。

                          -name "pattern"

                          -iname "pattern"  :忽略大小写

                                 支持glob风格的通配符:

                                       *  ? []  [^]

[root@myEcs test]# touch Passwd
[root@myEcs test]# find /etc/ -iname passwd                //实先创建一个Passwd的文件,不区分大小写查找
/etc/pam.d/passwd
/etc/passwd
/etc/test/Passwd
[root@myEcs test]# find /etc/ -name passwd                 // 区分大小写查找
/etc/pam.d/passwd
/etc/passwd
[root@myEcs test]# 
[root@myEcs test]# find /etc/ -name "passwd?"              //支持glob风格的通配符
/etc/passwd- 
[root@myEcs test]# 

                     -regex "pattern":基于正则表达式模式查找文件,匹配是整个路径,而非基名。

                      根据文件的属主查找:

                           -user 用户名:查找属主为指定用户的所有文件

                       -group 组名:查找属组为指定组的所有文件

                            -uid UID:查找属主为指定的UID的所有文件

                            -gid GID:查找属主为指定GID的所有文件

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

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

root@myEcs test]# find /tmp/ -user root -name aliyun_installer
/tmp/aliyun_assist_9760ca99bb98e898a422e558d5b2cb45/1.0.2.458/aliyun_installer
[root@myEcs test]# 

 

                      根据文件类型查找:

                            -type TYPE:

                                      f:普通文件

                                     d:目录文件

                                     l :符号链接文件

                                     b:块设备文件

                                     c:字符设备文件

                                     p:管道文件

                                     s:套接字文件

[root@myEcs ~]# find /dev/ -type d -ls                        //根据文件类型查找,后面跟处理动作ls
     3    0 drwxr-xr-x  19 root     root         2960 5月  9 17:57 /dev/
 11653    0 drwxr-xr-x   2 root     root           60 5月  8 21:52 /dev/net
 11651    0 drwxr-xr-x   2 root     root           60 5月  8 21:52 /dev/vfio
 11650    0 drwxr-xr-x   2 root     root           80 5月  8 21:52 /dev/snd
 11648    0 drwxr-xr-x   2 root     root           60 5月  8 21:52 /dev/mapper
[root@myEcs ~]# 

              组合测试:

                              与:-a,默认组合逻辑

[root@myEcs tmp]# find /tmp/ -nouser -type f -ls                       //不加-a默认组合条件 与。
1201238   20 -rw-r--r--   1 1014     1015        20311 5月 12 11:30 /tmp/devresize.py
1199673    4 -rw-r--r--   1 1014     1015          283 5月 11 16:36 /tmp/b.txt
1191902    4 -rw-r--r--   1 1014     1015           29 5月 11 16:07 /tmp/a.txt
[root@myEcs tmp]# find /tmp/ -nouser -a -type f -ls                     //加-a跟上面结果一样
1201238   20 -rw-r--r--   1 1014     1015        20311 5月 12 11:30 /tmp/devresize.py
1199673    4 -rw-r--r--   1 1014     1015          283 5月 11 16:36 /tmp/b.txt
1191902    4 -rw-r--r--   1 1014     1015           29 5月 11 16:07 /tmp/a.txt
[root@myEcs tmp]# 

 

                              或:o 两个条件,满足一个条件即可

                              非:-not,!

[root@myEcs tmp]# find /dev/ -not -type b -ls     // 查找不是块文件的文件

                    根据文件的大小查找:

                             -size [+]  # UNIT

                                   常用单位K,M,G

[root@myEcs tmp]# find . -size -44k -ls                                                               //小于44k的文件
1179649    4 drwxrwxrwt   7 root     root         4096 5月 13 21:10 .
1179655    4 drwxrwxrwt   2 root     root         4096 7月 11  2019 ./.X11-unix
1191968    4 -rw-r--r--   1 root     root          530 5月 11 18:28 ./sum_uid.sh
1179653    4 drwxrwxrwt   2 root     root         4096 7月 11  2019 ./.XIM-unix
1201238   20 -rw-r--r--   1 1014     1015        20311 5月 12 11:30 ./devresize.py
1199673    4 -rw-r--r--   1 1014     1015          283 5月 11 16:36 ./b.txt
1179656    4 drwxrwxrwt   2 root     root         4096 7月 11  2019 ./.Test-unix
1191902    4 -rw-r--r--   1 1014     1015           29 5月 11 16:07 ./a.txt
1179657    4 drwxrwxrwt   2 root     root         4096 7月 11  2019 ./.ICE-unix
1179654    4 drwxrwxrwt   2 root     root         4096 7月 11  2019 ./.font-unix
[root@myEcs tmp]# find . -size +44k -ls                                                              //大于44k的文件
1199681   48 -rw-------   1 root     root        47237 5月 13 21:10 ./messages.2
[root@myEcs tmp]# find . -size 44k -ls                                                               // 等于44k的文件
1191969 44 -rw------- 1 root root 44703 5月 13 21:03 ./messages
[root@myEcs tmp]#

 

 

                 根据时间戳查找:

                              以“天”为单位:

                                  -atime  [+ | -] number

                                               number:[number,number-1)

                                               -number:(number,0]

                                               +number:[number-1,+∞)

[root@myEcs tmp]# find /etc/ -atime +7 |head -3    //至少七天没有访问过的文件
/etc/chrony.keys
/etc/rwtab
/etc/ppp/ip-up.ipv6to4
[root@myEcs tmp]# 

 

                                  -mtime

[root@myEcs tmp]# find /etc/ -mtime -1|head -3     //查找在1天之内修改过的文件
/etc/
/etc/shadow-
/etc/group
[root@myEcs tmp]# 

 

                                  -ctime              

                            以“分钟”为单位:

                                  -amin

                                  -mmin

                                  -cmin

                根据权限查找:

                        -perm [/|-]mode

                                  mode:精确权限匹配。

[root@myEcs ~]# find /etc/ -perm 644|head -5
/etc/php-fpm.conf
/etc/magic
/etc/rwtab
/etc/logrotate.conf
/etc/subgid
[root@myEcs ~]# 

 

               处理动作:

                      -print:输出标准输出。默认动作

                      -ls :类似于对查找到的文件执行ls -l的命令,输出文件的详细信息

                      -delete:删除查找到的文件

                      -fls/PATH/TO/FILE:把查找到的所有文件的长格式信息保存至指定文件中。

                      -ok COMMAND {} \;  :表示对查找到的每个文件 执行由COMMAND表示的命令,每次操作都由用户进行确认

 

                      -exec COMMAND {} \; :表示对查找到的每个文件 执行由COMMAND表示的命令;每次操作不用用户确认。

[root@myEcs tmp]# find . -type f -name "*.txt" -ok chmod 777 {} \;      // 查找到的每一个文件执行777权限,由用户确认。
< chmod ... ./b.txt > ? y
< chmod ... ./a.txt > ? y
[root@myEcs tmp]# find . -type f -name "*.txt" -exec  chmod 755 {} \;    //查找到的每一个文件执行777权限,不由用户确认。
[root@myEcs tmp]# ll *.txt -rwxr-xr-x 1 1014 1015 29 5月 11 16:07 a.txt -rwxr-xr-x 1 1014 1015 283 5月 11 16:36 b.txt [root@myEcs tmp]# 

 

[root@myEcs tmp]# find . -type f -name "*.txt" -exec mv {} {}.bak \;     // {}表示找到的当前这个文件,修改找到的文件的文件名
[root@myEcs tmp]# ls *.bak
a.txt.bak  b.txt.bak
[root@myEcs tmp]# 

 

 

                    注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;但是有些命令不能接受过长的参数,此时执行命令会失败;另一种方式可以规避这个问题:

                  find | xargs 命令的用法

 

练习:

        1.查找/var目录下属主为root,且属组为mail的所有文件或者目录

[root@myEcs tmp]# find /var/ -user root -group mail -ls
131439    4 drwxrwxr-x   2 root     mail         4096 5月 13 20:48 /var/spool/mail
[root@myEcs tmp]# 

 

       2.查找/usr目录下不属于root,bin或hadoop的所有文件或目录;

[root@myEcs tmp]# find /usr/ -not -user root -not -user bin -not -user hadoop -ls

       3.查找/etc下最近一星期内其内容修改过,且属主不是root用户也不是hadoop用户的文件或目录

find /etc -mtime -7 -a -not -user root -a -not -user hadoop -ls

 

       4.查找当前系统上没有属主或属组,且最近一周内层被访问过的文件或目录

find / -nouser -o -nogroup -atime -7 -ls

 

       5.查找/etc目录下大于1M且类型为普通文件的所有文件

[root@myEcs ~]# find /etc/ -size +1M -type f -exec ls -lh {} \;
-rw------- 1 root root 3.8M 4月  26 2019 /etc/selinux/targeted/active/policy.kern
-rw-r--r-- 1 root root 1.4M 4月  26 2019 /etc/selinux/targeted/contexts/files/file_contexts.bin
-rw-r--r-- 1 root root 3.8M 4月  26 2019 /etc/selinux/targeted/policy/policy.31
-r--r--r-- 1 root root 7.6M 4月  21 15:04 /etc/udev/hwdb.bin

 

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

[root@myEcs ~]# find /etc/ -not -perm /222 -type f -ls
1446948    4 -r--r--r--   1 root     root          828 10月 19  2019 /etc/lvm/profile/metadata_profile_template.profile
1446946    4 -r--r--r--   1 root     root         3020 10月 19  2019 /etc/lvm/profile/command_profile_template.profile
1446945    4 -r--r--r--   1 root     root          339 5月 13  2019 /etc/lvm/profile/cache-smq.profile
1446944    4 -r--r--r--   1 root     root          531 5月 13  2019 /etc/lvm/profile/cache-mq.profile
1446947    4 -r--r--r--   1 root     root         2309 5月 13  2019 /etc/lvm/profile/lvmdbusd.profile
1446950    4 -r--r--r--   1 root     root           80 5月 13  2019 /etc/lvm/profile/thin-performance.profile
1446949    4 -r--r--r--   1 root     root           76 5月 13  2019 /etc/lvm/profile/thin-generic.profile
394127    4 -r--------   1 root     root           45 7月 11  2019 /etc/openldap/certs/password
394271    4 -r--r--r--   1 root     root           33 7月 11  2019 /etc/machine-id
393267    4 ----------   1 root     root         1646 5月 14 20:40 /etc/shadow-
395116    8 -r--r-----   1 root     root         4328 10月 30  2018 /etc/sudoers
397933    4 ----------   1 root     root          791 5月 16 17:27 /etc/gshadow
393259    4 ----------   1 root     root          781 5月 14 20:40 /etc/gshadow-
397935    4 ----------   1 root     root         1667 5月 16 17:27 /etc/shadow
394150    4 -r--r--r--   1 root     root           63 6月 19  2019 /etc/ld.so.conf.d/kernel-3.10.0-957.21.3.el7.x86_64.conf
394411    4 -r--r--r--   1 root     root           63 11月  9  2018 /etc/ld.so.conf.d/kernel-3.10.0-957.el7.x86_64.conf
393936  152 -r--r--r--   1 root     root       154279 7月 11  2019 /etc/pki/ca-trust/extracted/java/cacerts
393932  252 -r--r--r--   1 root     root       257889 7月 11  2019 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
393934  176 -r--r--r--   1 root     root       177221 7月 11  2019 /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
393933  208 -r--r--r--   1 root     root       211658 7月 11  2019 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
393935    0 -r--r--r--   1 root     root            0 7月 11  2019 /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
528435 7760 -r--r--r--   1 root     root      7942570 4月 21 15:04 /etc/udev/hwdb.bin

 

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

[root@myEcs ~]# find /etc/ -not -perm -111 -type f -ls

 

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

[root@myEcs ~]# find /etc/init.d/  -perm -113 -type f -ls

 

 

                           

  

posted @ 2020-05-09 21:35  附庸风雅  阅读(156)  评论(0)    收藏  举报