• 11-Linux文件查找find的使用


    1. find查找概述

    为什么要有文件查找, 因为很多时候我们可能会忘了某个文件所在的位置, 此时就需要通过find来查找.  
    find命令可以根据不同的条件来进行查找文件, 例如:文件名称、文件大小、文件修改时间、属主属组、权限、等等方式.
    同时find命令是Linux下必须掌握的.
    
    • 1
    • 2
    • 3
    find 命令的基本语法如下:
    
    • 1
    命令路径选项表达式动作
    find[path…][options][expression][action]
    查找地区妹纸18-25岁约?

    2. 示例

    以下列出所有find常用的选项
    
    • 1
    2.1 指定文件名
    选项:
    	-name 指定文件的名称
    	-i 忽略大小写
    
    • 1
    • 2
    • 3
    # 1.创建文件  
    [root@kid ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth111,IFCFG-ETH111}  
    
      
    # 2.查找/etc目录下包含ifcfg-eth111名称的文件  
    [root@kid ~]# find /etc -name "ifcfg-eth111"  
    /etc/sysconfig/network-scripts/ifcfg-eth111
    
    # -i 忽略大小写  
    [root@kid ~]# find /etc -iname "ifcfg-eth111"  
    /etc/sysconfig/network-scripts/ifcfg-eth111
    /etc/sysconfig/network-scripts/IFCFG-ETH111
    
    
    # 查找/etc目录下包含ifcfg-eth名称所有文件  
    [root@kid ~]# find /etc/ -name "ifcfg-eth*"  
    /etc/sysconfig/network-scripts/ifcfg-eth111
    
    # 忽略大小写
    [root@kid ~]# find /etc -iname "ifcfg-eth*"  
    /etc/sysconfig/network-scripts/ifcfg-eth111
    /etc/sysconfig/network-scripts/IFCFG-ETH111
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2.2 指定文件大小
    选项:
    	-size 指定文件的大小
    	      +:      大于 
    	      -:      小于
    	      无符号:  等于
    	       c:      字节
     	       k:      KB
               M:      MB
               G:      GB
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    # 1.查找大于5M的文件  
    [root@kid ~]# find /etc -size +5M  
    /etc/udev/hwdb.bin
      
    # 2.查找等于5M的文件  
    [root@kid ~]# find /etc -size 5M  
      
    # 3.查找小于5M的文件  
    [root@kid ~]# find /etc -size -5M  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2.3 指定文件类型
    文件类型:
    b block special       块设备 
    c character special   字符设备 
    d directory           目录 
    f ASCII text          文件    
    s sticky directory    套接字  
    l symbolic link ...   链接     
    p fifo (named pipe)   管道文件  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
     # f 文件  
    [root@kid ~]# find /root -type f  
    # d 目录  
    [root@kid ~]# find /dev -type d  
    # l 链接  
    [root@kid ~]# find /dev -type l  
    # b 块设备  
    [root@kid ~]# find /dev -type b  
    # c 字符设备  
    [root@kid ~]# find /dev -type c  
    # s 套接字  
    [root@kid ~]# find /dev -type s  
    # p 管道文件  
    [root@kid ~]# find /run -type p
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    2.4 指定时间查找
    选项:
    	-mtime n 指定查询数据
    	+:     n天前
    	-:     n天后
    	无符号: 第n天
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 创建测试文件 $i 变量
    [root@kid ~]# for i in {01..31}; do date -s  202208$i && touch file-$i;done 
    
    • 1
    • 2
    # 查找7天以前的文件 
    [root@kid ~]# find ./ -iname "file-*" -mtime +7  
    ./file-01
    ...
    ./file-27  (今天是9-4号)
    
    # 查找最近7天的文件, 不建议使用(7天内不包含第七天)
    [root@kid ~]# find ./ -iname "file-*" -mtime -7  
    ./file-29
    ./file-30
    ./file-31
    
      
    # 查找第7天文件
    [root@kid ~]# find ./ -iname "file-*" -mtime 7  
    ./file-28
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    # 实际使用方案 本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件 
    find /backup/ -iname "*.bak" -mtime +7 -delete  
    find /backup/ -iname "*.bak" -mtime +90 -delete  
    
    • 1
    • 2
    • 3
    2.5 指定用户查找
    选项:
        -user    指定属主
        -group   指定属组
        -nouser  没有属主
        -nogroup 没有属组
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 查找home下属主是qq的文件
    [root@kid ~]# find /home -user qq
    /home/qq/a.txt
    ...
    
    # 查找属主是root 
    [root@kid ~]# find /home -user root
    
    # 查找属组是root 
    [root@kid ~]# find /home -group root
    
    # 查找属主是root, 属组是root 
    [root@kid ~]# find /home -user root -group root  
     
    # 查找没有属主  
    [root@kid ~]# find /home -nouser  
    
    # 查找没有属组  
    [root@kid ~]# find /home -nogroup  
    
    # 查找没有属主或属组  
    [root@kid ~]# find /home -nouser -o -nogroup  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    2.6 指定权限查找
    选项:
    	-perm 权限位 指定权限查询
    
    • 1
    • 2
    # 精切匹配644权限  
    [root@kid ~]# find . -perm 644 -ls  
      
    # 包含444权限即可  
    [root@kid ~]# find . -perm -444  -ls  
    
    # 查找全局可写(每位权限必须包含w)  
    [root@kid ~]# find . -perm -222 -ls
    
    # 包含setuid  
    [root@kid ~]# find  /usr/sbin -perm -4000 -ls
    
    # 包含setgid  
    [root@kid ~]# find  /usr/sbin -perm -2000 -ls  
    
    # 包含sticky  
    [root@kid ~]# find  /usr/sbin -perm -1000 -ls  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3. find动作处理

    3.1 动作及含义
    比如查找到一个文件后, 需要对文件进行如何处理, find的默认动作是 -print 输出.
    
    • 1
    动作含义
    -print打印查找到的内容(默认)
    -ls以长格式显示的方式打印查找到的内容
    -delete删除查找到的文件(仅能删除空目录)
    -ok后面跟自定义 shell 命令(会提示是否操作)
    -exec后面跟自定义 shell 命令(标准写法 -exec 😉
    3.2 示例
    # 1.使用-print打印查找到的文件  
    [root@kid ~]# find /etc -name "ifcfg*"  
    /etc/sysconfig/network-scripts/ifcfg-ens32
    
    [root@kid ~]# find /etc -name "ifcfg*" -print  
    /etc/sysconfig/network-scripts/ifcfg-ens32
    
    #2.使用-ls打印查找到的文件, 以长格式显示  
    [root@kid ~]# find /etc -name "ifcfg*" -ls  
    8414899    4 -rw-r--r--   1 root     root          365 Sep  3 20:31 /etc/sysconfig/network-scripts/ifcfg-ens32
    
    # 3.使用-delete删除文件, 目录仅能删除空目录  
    [root@kid ~]# mkdir dir1
    [root@kid ~]# find /root -name "dir1" -delete  
    [root@kid ~]# ll dir1
    ls: cannot access dir1: No such file or directory
    
      
    # 4.使用-ok实现文件拷贝, 但会提示是否拷贝  
    [root@kid ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; 
    < cp ... /etc/sysconfig/network-scripts/ifcfg-ens32 > ? ok (需要输入ok)
    ...
     
    # 5.使用-exec实现文件拷贝和文件删除.  
    [root@kid ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;  
    [root@kid ~]# find /tmp -name "ifcfg*" -exec rm -f {} \;  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4. 配合xargs使用

    xargs将前者命令查找到的文件作为一个整体传递后者命令的输入.
    
    • 1
    # 创建文件
    [root@kid ~]# touch file{1..2}.txt  
    [root@kid ~]# ls
    file1.txt  file2.txt
    
    # 将查询到的文件删除
    [root@kid ~]# find . -name "file1.txt" |xargs rm -f  
    
    # 移动文件
    [root@kid ~]# find . -name "file2.txt" |xargs -I {} cp -rvf {} /var/tmp  
    ‘./file2.txt’ -> ‘/var/tmp/file2.txt’
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5. 逻辑运行符

    符号作用
    -a
    -o
    -not!
    # 1.查找当前目录下, 属主不是qq的所有文件  
    [root@kid ~]# find . -not -user qq
    
    [root@kid ~]# find . ! -user qq  
    
    • 1
    • 2
    • 3
    • 4
    用户不存在会先提示用户不存在
    find: ‘hdfs’ is not the name of a known user
    
    • 1
    • 2
    # 2.查找当前目录下, 属主属于qq, 且大小大于300字节的文件  
    [root@kid ~]# find . -type f -a -user qq -a -size +300c           
    
    • 1
    • 2
    # 3.查找当前目录下的属主为qq或者以xml结尾的普通文件 
    [root@kid ~]# find . -type f -a \( -user qq -o -name '*.xml' \)  
    
    • 1
    • 2
    使用() 需要使用\转义, 否则提示
    -bash: syntax error near unexpected token
    
    • 1
    • 2

    6. 相关练习题

    1.查找/tmp目录下, 属主不是root, 且文件名不以f开头的文件  
    2.查找/var目录下属主为root, 且属组为mail的所有文件  
    3.查找/var目录下不属于root、lp、gdm的所有文件  
    4.查找/var目录下最近一周内其内容修改过, 同时属主不为root, 也不是postfix的文件  
    5.查找/etc目录下大于1M且类型为普通文件的所有文件  
    6./etc/中的所有目录(仅目录)复制到/tmp下, 目录结构不变  
    7./etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666  
    8.保留/var/log/下最近7天的日志文件,其他全部删除  
    9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除  
    10.解释如下每条命令含义  
    mkdir /root/dir1  
    touch /root/dir1/file{1..10}  
    find /root/dir1 -type f -name "file5"  
    find /root/dir1 ! -name "file5"  
    find /root/dir1 -name "file5" -o -name "file9"  
    find /root/dir1 -name "file5" -o -name "file9" -ls  
    find /root/dir1 \( -name "file5" -o -name "file9" \) -ls  
    find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;  
    find /root/dir1  ! \( -name "file4" -o -name "file8" \) -exec rm -vf {}  \; 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ————————————————
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    文章的段落全是代码块包裹的, 留言是为了避免文章提示质量低.
    ————————————————

  • 相关阅读:
    MySQL 约束条件,关键字练习,其他语句
    LeetCode第1题:两数之和
    AtCoder Beginner Contest 342 (ABCDEFG题)视频讲解
    android 各种偶现问题记录
    Java类中this关键字和static关键字的用法详解
    【随记】在WSL2下安装ROS
    前端js调试如何复制console.log打印的对象或数组
    (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
    Go 项目代码布局
    【Python】如何使用PyInstaller打包自己写好的代码
  • 原文地址:https://blog.csdn.net/qq_46137324/article/details/126687529