10.文件查找之の find

1|0一.功能

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

2|0二.语法

2|1find [路径] [选项] [表达式]

3|0三.常用选项

选项功能
-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

4|0三.查找文件

4|11.按文件名查找

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

4|22.按文件大小

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

4|33.指定查找目录的深度

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

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

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

4|55.按属组查找

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       #查看没有属主或没有属组的文件

4|66.按文件类型查找

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         #管道文件

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

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

5|0四.找到文件的后续处理

5|1举例命令

命令作用
-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 \;            #非交互式,不会提醒

6|0五.find 与 xargs 配合使用

6|11.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 {} #修改找到的文件的权限等级

 


__EOF__

本文作者ଲ小何才露煎煎饺
本文链接https://www.cnblogs.com/zeny/articles/14409656.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   ଲ小何才露煎煎饺  阅读(35)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示