zoukankan      html  css  js  c++  java
  • 🍖文件查找之 find

    一.功能

    在指定目录查找符合条件的文件

    二.语法

    find [路径] [选项] [表达式]

    三.常用选项

    选项 功能
    -name 根据文件名查找 (' * ', )
    -type 根据文件类型查找 (详细类型在后)
    -perm 根据文件权限查找,比如 777
    -user 根据属主查找
    -group 根据属组查找
    -size 根据文件大小
    -maxdepth n 最大搜索层数 (n:数字)
    -o 或者
    -a 并且(默认就是)
    -not 表达式: 非
    • -type 类型分类
    f	#普通文件
    d	#目录
    l	#链接
    b	#块设备
    C	#字符设备
    s	#套接字
    p	#管道
    
    • -size 单位
    b   #block块 (不添加单位默认就是block块: 512字节)
    w   #2字节
    c   #字节
    K   #K = 1024c
    M   #兆 = 1024K
    G   #G = 1024M
    

    三.查找文件

    1.按文件名查找

    find /etc -name 'ifcfg-ens32'
    find /etc -iname 'ifcfg-ens32'  #不区分大小写查找
    find /etc -iname 'ifcf*'        #匹配 'ifcf所有字符'
    

    2.按文件大小

    find /etc -size +3M        #大于3M
    find /etc -size 3M         #等于
    find /etc -size -3M        #小于
    find /etc -size +3M -ls    #找到之后 ls 操作
    

    3.指定查找目录的深度

    用法: -maxdepth [指定层数]
    find /etc -maxdepth 5 -name "ifcfg.*"  #最大遍历5目录查找
    

    4.按时间查找 (atime, mtime, ctime)

    find /etc -mtime +3        #修改时间超过3天
    find /etc -mtime 3         #修改时间等于3天
    find /etc -mtime -3        #修改时间3天以内
    
    • 查看一个文件的元数据
    # stat [文件名]
    

    5.按属组查找

    find /root -user shawn       #属主是Shawn
    find /root -group song	     #属组是song
    find /root -user shawn -group song       #属主和属组
    find /root -user shawn -a -group -song   #属主和属组 (不加 -a 默认 -a )
    find /root -user shawn -o -group -song   #属主或者属组满足一个就可以
    
    • 更改一个文件属主和属组
    语法 : chown [user].[grep] a.txt
    用法 : 将"a.txt"的属主和属组都变成"root"
    # chown root.root a.txt
    删除一个文件的属主:进入"/etc/passwd"下删除这个用户记录
    删除一个文件的属组:进入"/etc/group"下删除这个用户所在的组记录
    find /root -nouser      #查找没有属主的文件
    find /root -nogroup     #查找没有属组的文件
    find /root -nouser -o nogroup  #查看没有属主或没有属组的文件
    

    6.按文件类型查找

    find /root -type f          #普通文件
    find /root -type d          #目录
    find /root -type l          #链接
    find /root -type c          #字符设备
    find /root -type b          #块设备
    find /root -type s          #套接字
    find /root -type p          #管道文件
    

    7.按文件权限查找 (后续用户权限详细讲)

    find /root -perm 644 -print     #不加 "-print" 默认就是 "-print"
    find /root -perm -644 -ls 
    find /root -perm -600 -ls
    

    四.找到文件的后续处理

    举例命令

    命令 作用
    -print 默认命令,打印找到的文件
    -ls 显示详细信息
    -delete 删除
    -exec 每次操作不提醒直接执行
    -ok 每次操作进行提醒 "y/n"
    find /root -name "song*" -print  #默认就是 "-print"
    find /root -name "song*" -ls
    find /root -name "song*" -delete #找到删除
    find /root -name "song*" -exec rm -rf {} \;  #找到删除
    
    • 配合 "-exec""-ok" 进行交互与非交互
    find /root -name "song*" -ok cp -rvf {} /tmp \;  #交互式,每次都会提醒:"y/n"
    find /root -name "song*" -exec cp -rvf {} /tmp \;  #非交互式,不会提醒
    

    五.find 与 xargs 配合使用

    1.xargs 作用

    • 让不支持管道的命令也可以使用管道内的内容
    find /root -name "song*" | xargs rm -rvf   #删除管道里的内容
    find /root -name "song*" | xargs -I {} cp -rf {} /tmp
    find /root -name "song*" | xargs -I {} mv {} /tmp
    find /root -name "song*" | xargs -I {} chmod 777 {} #修改找到的文件的权限等级
    
  • 相关阅读:
    学习FPGA的几个阶段
    Quartus 编译问题Info (176311): Pin ~ALTERA_nCEO~ is assigned to pin location Pin_P28 (IOPAD_X115_Y43_N7)
    No Title
    最近比较忙
    如何在Qsys 中定制Nand_Flash软核(学习)
    Nand_Flash工作原理(学习)2
    (转)FPGA时序约束的几种方法
    ‘Downloading ELF Process failed’问题如何解决
    celery使用的时候的坑
    Git命令一览
  • 原文地址:https://www.cnblogs.com/songhaixing/p/13881593.html
Copyright © 2011-2022 走看看