08.Linux文件查找 (find)

08.Linux文件查找

1.find查找概述

1.1 为什么需要查找

  • 1.很多时候我们可能会忘了文件所在的位置,此时就
    需要通过 find 来查找;
  • 2.有时候需要通过内容查找到对应的文件,此时就需
    要通过 find 来查找;

1.2 为什么是find

  • 因为 find 命令可以根据不同的条件来进行查找文件
  • 比如:
    • 文件名称、
    • 文件大小、
    • 文件时间、
    • 属主属组、
    • 权限等等、
  • 可以通过如上几种方式查找文件,从而实现精准定位

1.3 find命令语法

命令 路径 选项 表达式 动作
find [path] [options] [expression] [action]
查找 地区 妹子 18-28 ???

2.find查找示例

2.1 find基于名称查找

#1.创建文件
touch /etc/sysconfig/network-
scripts/{ifcfg-eth1,IFCFG-ETH1}

#2.查找/etc目录下包含ifcfg-eth1名称的文件
[root@xuliangwei ~]# find /etc -name
"ifcfg-eth1"

#3.-i 忽略大小写
[root@xuliangwei ~]# find /etc -iname
"ifcfg-eth1"

#查找/etc目录下包含ifcfg-eth名称所有文件
[root@xuliangwei ~]# find /etc/ -name
"ifcfg-eth*"
[root@xuliangwei ~]# find /etc -iname
"ifcfg-eth*"

2.2 find基于大小查找

  • +5 : 大于5M
  • -5 : 小于5M
  • 5 : 等于5M
#1.查找大于5M的文件
[root@xuliangwei ~]# find /etc -size +5M
#2.查找等于5M的文件
[root@xuliangwei ~]# find /etc -size 5M
#3.查找小于5M的文件
[root@xuliangwei ~]# find /etc -size -5M
###查找大于5m小于7m
[root@node ~]# dd if=/dev/zero of=/etc/big
count=6M bs=1
[root@node ~]# find /etc -size +5M -a -
size -7M | xargs ls -lh
-rw-r--r--. 1 root root 6.0M 7月  16 15:27
/etc/big

2.3 find基于类型查找

# f 文件
 # find /dev -type -f 
 
# d 目录
 # find /dev -type -d
 
# l 链接
# find /dev -type -l

# b 块设备
# find /dev -type -b

# c 字符设备
# find /dev -type -c

# s 套接字
# find /dev -type -s

# p 管道文件
# find /dev -type -p

2.4find基于时间查找

#1.创建测试文件(后期shell会讲)
[root@xuliangwei ~]# for i in {01..10};do
date -s 201904$i && touch file-$i;done

#2.查找7天以前的文件(不会打印当天的文件)  7天以前
的查找出来,然后删除 == 保留了最近的7天的数据
[root@xuliangwei ~]# find ./ -iname "file-
*" -mtime +7

#3.查找最近7天的文件,不建议使用(会打印当天的文件)
 将最近7天的数据给列出来,如果删除;
[root@xuliangwei ~]# find ./ -iname "file-
*" -mtime -7

#4.查找第7天文件(不会打印当天的文件)
[root@xuliangwei ~]# find ./ -iname "file-
*" -mtime 7

# 查找最近120分钟发生变化的文件
[root@node ~]# find ./ -type f -mmin -122
./file.txt
# 查找多少分钟内发生过修改的文件
[root@node ~]# find ./ -type f -mmin -122
./file.txt
# 查找系统有那些命令在最近多长时间内,发生过变化()
[root@node ~]# find /bin/ /sbin/ -type f -mmin -5
/bin/python2.7
/bin/gg
#面试题: 查找/var/log下所有以.log结尾的文件,并保留最近7天的log文件。
[root@xuliangwei ~]# find /var/log -type f
-name "*.log" -mtime +7 -delete

2.5find基于用户查找

-user
-group
-nouser
-nogroup
#查找属主是jack
[root@xuliangwei ~]# find /home -user jack
#查找属组是admin
[root@xuliangwei ~]# find /home -group
admin
#查找属主是jack, 属组是admin
[root@xuliangwei ~]# find /home -user jack
-group admin
#查找属主是jack, 并且属组是admin
[root@xuliangwei ~]# find /home -user jack
-a -group admin
#查找属主是jack, 或者属组是admin
[root@xuliangwei ~]# find /home -user jack
-o -group admin
#查找没有属主
[root@xuliangwei ~]# find /home -nouser
#查找没有属组
[root@xuliangwei ~]# find /home -nogroup
#查找没有属主或属组
[root@xuliangwei ~]# find /home -nouser -o
-nogroup

2.6 find基于权限查找

  • =perm [ / | -] MODE

    • MODE : 精确权限匹配 644
    • -MODE : 每一类对象都必须同时拥有指定的权限:(并且的关系)
    • /MODE : 任何一类(UGO)只要有一位匹配即可;(或者的关系)
    # 精确
    [root@web ~]# find /root -type f -perm
    644 -ls
    #-包含(u涵盖6,并且g涵盖4,并且o涵盖4)
    [root@web ~]# find /root -type f -perm
    -644 -ls
    #/或者(U为6 或者g为4 或者o为0)
    [root@web ~]# find /root -type f -perm
    /640 -ls
    # 特殊权限(最低权限是4000 4755 也满足需求;)
    [root@web ~]# find /usr/bin/ /usr/sbin/ -
    type f -perm -4000 -ls
    [root@web ~]# find /usr/bin/ /usr/sbin/ -
    type f -perm -2000 -ls
    [root@web ~]# find /usr/bin/ /usr/sbin/ -
    type f -perm -1000 -ls
    

2.7 find基于逻辑符

符号 作用
-a 与(并且)
-o 或(或者)
-not | ! 非(取反)
#1.查找当前目录下,属主不是root的所有文件
[root@xuliangwei ~]# find . -not -user root
[root@xuliangwei ~]# find . ! -user root
#2.查找当前目录下,属主属于hdfs,并且大小大于1k的文件
[root@xuliangwei ~]# find . -type f -a -user hdfs -a -size +1k
#3.查找当前目录下的属主为root或者以xml结尾的普通文件
[root@xuliangwei ~]# find . -type f -a \( -user root -o -name '*.xml' \)

3.find 动作处理

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

3.1 find结合exec

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

3.2 find结合xargs

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

3.3 find结合grep

  • 当忘记重要配置文件存放路径是,可以通过搜索关键字获取文件其路径
[root@xuliangwei ~]# find /code -type f |
xargs grep -R "MySQL_PASSWORD"
[root@node test]# find /etc/ -type f |
xargs grep -R "oldxu.com"
/etc/pass: oldxu.com
posted @ 2021-07-18 14:42  GaoBeier  阅读(168)  评论(0编辑  收藏  举报