Linux文件查找之find

1,find能干啥?

  • Linux中的find命令强无敌,find可以根据不同的条件来查找文件。
  • 比如:文件名称,文件大小,文件时间,属主属组,权限等等。

2,find命令语法

命令 路径 选项 表达式 动作
find [path..] [options] [experssion] [action]
查某个人 地区 是找妹子吗? 要18的还是48的? 找ta干嘛

3,find查找示例

  • 3.1,find基于名称查找示例:
[root@Linux.net: ~]#touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}  #创建文件
[root@Linux.net: ~]#find /etc -name "ifcfg-eth1"    #查找/etc 目录下包含ifcfg-eth1名称的文件,会在终端打印其绝对路径
/etc/sysconfig/network-scripts/ifcfg-eth1
[root@Linux.net: ~]#find /etc/ -iname "ifcfg-eth1" #忽略大小写
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1

[root@Linux.net: ~]#find /etc/ -name "ifcfg-eth*" #查找/etc目录下包含ifcfg-eth名称的所有文件
[root@Linux.net: ~]#find /etc/ -iname "ifcfg-eth*" #查找/etc目录下包含ifcfg-eth名称的所有文件,忽略大小写

3.2find基于大小查找

  • +5:大于5M
  • -5:低于5M
  • 5 :等于5M
[root@Linux.net: ~]#find /etc/ -size +5M  #查大于5M的文件
[root@Linux.net: ~]#find /etc/ -size 5M   #查等于5M的文件
[root@Linux.net: ~]#find /etc/ -size -5M   #查小于5M的文件

[root@Linux.net: ~]#dd if=/dev/zero of=/etc/big count=6M bs=1  #自己创造一个6M的文件

[root@Linux.net: ~]#find /etc/ -size +5M -a -size -7M | xargs ls -lh #查找大于5M小于7M的文件

3.3,find基于类型查找

[root@Linux.net: ~]#find /root/ -type f  #文件
[root@Linux.net: ~]#find /root/ -type d  #目录
[root@Linux.net: ~]#find /root/ -type l  #连接
[root@Linux.net: ~]#find /root/ -type b  #块设备
[root@Linux.net: ~]#find /root/ -type c  #字符设备
[root@Linux.net: ~]#find /root/ -type s  #嵌套字
[root@Linux.net: ~]#find /root/ -type p  #管道文件

3.4,find基于时间查找

[root@Linux.net: ~]#find ./ -iname "file-*" -mtime +7  #查找7天以前的文件(不会打印当天的文件)
[root@Linux.net: ~]#find ./ -iname "file-*" -mtime -7 #查找最近7天的文件
[root@Linux.net: ~]#find ./ -iname "file-*" -mtime 7  #查找第7天的文件

#查找/var/log 下所有以.log结尾的文件,并保留最近7天的log文件
[root@Linux.net: ~]#find /var/log -type f -name "*.log" -mtime +7 -delete #如果保留最近7天的文件,把7天以前的所有文件找到删除就好了。



[root@Linux.net: ~]#find ./ -type f -mmin -120 #以分钟为单位
[root@Linux.net: ~]#find ./ -type f -mmin +120 
[root@Linux.net: ~]#find ./ -type f -mmin 120 


3.5,find基于用户查找

[root@Linux.net: ~]#find /home/ -user carol  #查找属主carol的用户
[root@Linux.net: ~]#find /home/ -group admin #查找属组是admin的用户
[root@Linux.net: ~]#find /home/ -user carol -a -group admin #查找属主是carol,属组是admin的用户
[root@Linux.net: ~]#find /home/ -user carol -o  -group admin #查找属主是carol或者属组是admin的用户
[root@Linux.net: ~]#find /home/ -nouser   #查找没有属主的用户
[root@Linux.net: ~]#find /home/ -nogroup   #查找没有属组的用户
[root@Linux.net: ~]#find /home/ -nouser -o nogroup  #查找没有属主或属组的用户

3.6,find基于权限查找

  • -perm [/|-] MODE
  • MODE:精确权限匹配 644
  • -MODE:每一类对象都必须同时拥有指定的权限,(并且的关系)
  • /MODE: 任何一类(UGO)只要有一位匹配即可;(或者的关系)
[root@Linux.net: ~]#find /root/ -type f -perm 644 -ls  #查/root 下权限为644的文件
[root@Linux.net: ~]#find /root/ -type f -perm -644 -ls #-表示包含,(u涵盖6,并且g涵盖4,并且o涵盖4)
[root@Linux.net: ~]#find /root/ -type f -perm /644 -ls #/表示或者 (u为6或者g为4,或者o为0)

##特殊权限 (最低权限是4000 4755 也满足要求)
[root@Linux.net: ~]#find /usr/bin/ /usr/bin/ -type f -perm -4000 -ls 
[root@Linux.net: ~]#find /usr/bin/ /usr/bin/ -type f -perm -2000 -ls
[root@Linux.net: ~]#find /usr/bin/ /usr/bin/ -type f -perm -1000 -ls

3.7,find逻辑运算符

符号 作用
-a
-o
-not !
[jack@Linux root]$ find /. -not -user caorl  #查找/.目录属主不是caorl的所有文件
[jack@Linux root]$ find /. !-user caorl  #查找/.目录属主不是caorl的所有文件
[root@Linux.net: ~]#find . -type f -a -user carol -a -size +1k  #查找当前目录下属主属于carol并且文件大小大于1K的所有文件

[root@Linux.net: ~]#find . -type f -a ( -user root -o -name ‘*.xml’ ) #查找当前目录下的属主为root或者以xml结尾的普通文件

4,find动作处理

  • 查到目标文件后,find对文件进行处理的默认动作是 -print
动作 含义
-print 打印查找到的内容(默认)
-ls 以长格式显示的方式打印查找到的内容
-delete 删除查找到的文件(仅能删除空目录)
-ok 后面跟自定义shell命令(会提示是否操作)
-exec 后面跟自定义shell命令(标准写法-exec;)

4.1find结合exec

  • 使用-exec实现文件拷贝和文件删除
[root@Linux.net: ~]#find /etc/ -name "ifcfg*" -exec cp -rvf {} /tmp ;
[root@Linux.net: ~]#find /etc/ -name "ifcfg*" -exec rm -f {} /tmp ;
[root@Linux.net: ~]#find /etc/ -name "ifcfg*" -exec mv  {} /tmp ;

4.2,find结合xargs

  • xargs 将前者命令查找到的文件作为一个整体传递给给后者命令的输入,其操作的性能极高。
  • |xargs rm -f 1 2 3 4 5 6 100 10000
  • exec 是将文件一个一个的处理,所以处理性能极低;
#分别用-exec和xargs来删除100000个文件
[root@Linux.net: opt]#touch file-{1..100000}
[root@Linux.net: opt]#find /opt/ -name "file-*" -exec rm -f {} ;
[root@Linux.net: opt]#find /opt/ -name "file-*" -xargs rm -f 
[root@Linux.net: ~]#find /usr/sbin/ -type f -perm -4000 |xargs -I {} cp -rv {} /tmp/ #文件拷贝

4.3,find结合grep

  • 当忘记重要配置文件存储路径时,可以通过搜索关键字获取文件的路径
[root@Linux.net: ~]#find /code -type f | xargs grep -R "MySQL_PASSWORD"
[root@Linux.net: ~]#find /etc/ -type f |xargs grep -R "carol.net"

原文地址:https://www.cnblogs.com/qinghuani/p/15024312.html