文件查找

    Linux上的所有资源都以文件的形式存在,如果是手工查找的话,势必会浪费太多的时间,这里推荐俩款大家用于查找的工具。

文件查找

   文件查找经常用到的俩款软件,locate和find

   二者区别

locate:1) 非实时查找;
        2) 依赖于索引,而索引构建非常占用资源,索引的创建是在系统空闲时系统自动进行,可以           用updatedb命令更新索引(/var/lib/mlocate/mlocate.db),极度耗费系统资源;
        3) 查找速度快;        
        4) 非精准查找(会匹配到文件路径)。
find :  1) 实时查找(系统上实时的有就是有没有就是没有);        
        2) 查找速度慢
        3) 精准查找 (查找结果绝对符合查找条件才予显示);



  

    1、locate

    2、find

       <1>语法格式

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

       <2>查找路径:默认是当前目录,可指定

       <3>查找条件

          根据文件名和inode查找      

              -name  "文件名称":支持使用globbing:*, ?, [], [^],加引号表示使用globbing

              -iname "文件名称":不区分字母大小写,支持globbing,加引号表示使用globbing

[root@localhost /tmp]#ls
f1  F1  f2
[root@localhost /tmp]#find -iname "F*"
./f1
./f2
./F1

              -inum n          :按inode号查找

              -samefile name   :相同inode号的文件

[root@localhost /tmp]#touch f1
[root@localhost /tmp]#ln f1 f2
[root@localhost /tmp]#find -samefile f1
./f1
./f2

              -links n         :链接数为n的文件

          根据属主、属组查找

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

              -group GRPNAME  : 查找属组为指定组(GID)的文件

              -uid UserID     :查找属主为指定的UID号的文件

              -gid GroupID    :查找属组为指定的GID号的文件

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

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

          根据文件类型查找:

              -type TYPE:

                  f: 普通文件

                  d: 目录文件

                  l: 符号链接文件

                  s:套接字文件

                  b: 块设备文件

                  c: 字符设备文件

                  p: 管道文件

          根据文件大小来查找:

              -size [+|-]#

              常用单位:k, M, G

                  # : 7k, (6, 7]

                  -#:-7k,[0,6]

                  +#:+7k,(7,∞)

          根据时间戳:

              -atime[+|-]#

              以“天”为单位;

                  # : 7, 7天前访问的

                  +#: +7,一周前访问的

                  -#: -7,一周之内访问的

                  -mtime同理

                  -ctime同理

              以“分钟”为单位:

                  -amin同理

                  -mmin同理

                  -cmin同理

          根据权限:

              -perm [/|-]MODE             

                  /MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可。

                      注:centos6:+MODE

                  -MODE:每一类对象都必须同时拥有指定权限,与关系

                  0    : 表示不关注

          组合条件

              -a     :与

              -o     :或

              not(!) : 非



    

    3、处理动作                  

       -print              :默认的处理动作,显示至屏幕

       -ls                 :类似于对查找到的文件执行“ls -l”命令

注意:一个命令作用在多个条件时,要加括号,否则命令将默认只作用在相邻的条件上

[root@localhost ~]#find \( -user root -a -type f \) -ls

当然,下面的写法可以避免这种错误:

[root@localhost ~]#find -user root -ls -a -type f -ls

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

       -flsfile            :查找到的所有文件的长格式信息保存至指定文件中

       -ok COMMAND {} \;   :对查找到的每个文件执行由COMMAND指定的命令,并且都会交互式要求用户确认

[root@localhost /tmp]#ll
total 12
-rw-rw-rw-+ 2 root root 0 Aug 16 10:16 f1
-rw-rw-rw-+ 1 root root 0 Aug 16 10:17 F1
-rw-rw-rw-+ 2 root root 0 Aug 16 10:16 f2
[root@localhost /tmp]#find -iname "f*" -ok -delete {} \;
< -delete ... ./f1 > ?

       -exec COMMAND {} \; :对查找到的每个文件直接由COMMAND指定的命令

[root@localhost /tmp]#ll
total 28
-rw-rw-rw-+ 2 zhao zhao 30 Aug 14 16:43 1
-rw-rw-rw-+ 2 zhao zhao 30 Aug 14 16:43 2
-rw-rw-rw-+ 1 root root  0 Aug 16 09:51 f1
-rw-rw-rw-+ 1 root root  0 Aug 16 10:10 test
drwxrwxrwt+ 3 root root 21 Aug 15 08:32 VMwareDnD
[root@localhost /tmp]#find -perm -002 -exec mv {} {}.danger \;
[root@localhost /tmp]#ls
1.danger  2.danger  f1.danger  test.danger  VMwareDnD.danger




注意:find在查找链接文件时,加/和不加/的区别

[root@localhost /tmp]#ll
total 0
drwxr-xr-x. 2 root root 18 Aug 16 15:59 a
lrwxrwxrwx. 1 root root  1 Aug 16 15:59 b -> a
[root@localhost /tmp]#find b/ -perm -113
b/b.txt
[root@localhost /tmp]#find b -perm -113
b

也就是说:如果目录下有文件写成ls /dir==ls /dir/,而如果像链接文件这样,ls /etc/init.d 不等于 ls /etc/init.d/




注意:排除目录查找

查找q目录下的所有.txt文件,但是跳过子目录e

[root@localhost q]# ls
e  w
[root@localhost q]# cd e
[root@localhost e]# ls
e.txt
[root@localhost q]# find . -path './e' -a -prune -o -name *.txt -print
./w/w.txt
./e





课后强化练习

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

[root@localhost /tmp]#find /var -user root -a -group mail

2、查找/var目录下不属于root、lp、gdm的所有文件

find /var -user root -a -user lp -a -user gdm

3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件

[root@localhost /tmp]#find /var -ctime -7 -a -user root -a -user postfix

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

[root@localhost /tmp]#find / \( -nouser -o -nogroup \) -a -atime -7

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

[root@localhost /tmp]#find /etc -size +1M -a -type f

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

[root@localhost /tmp]#find /etc -not -perm /222

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

[root@localhost /tmp]#find /etc -not -perm -111

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

[root@localhost /tmp]#find /etc/init.d/ -perm -111 -a -perm -002

或者

[root@localhost /tmp]#find /etc/init.d/ -perm -113

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值