前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell入门系列(7)find

shell入门系列(7)find

作者头像
suveng
发布2019-09-18 14:47:20
4340
发布2019-09-18 14:47:20
举报

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qq_37933685/article/details/86360983

文章目录

  • shell入门系列(7)find
    • 简介
    • 入门小案列
      • 指定目录找文件(文件名)
      • 指定目录找文件夹名
      • 使用正则表达式搜索
      • 排除搜索
      • 查找文件类型
      • 基于目录深度搜索
      • 根据文件时间搜索
      • 基于文件大小搜索
      • 结合find执行命令或动作
      • 让find跳过特定目录
      • 基于文件权限和所有权的匹配搜索
    • 我的主页

shell入门系列(7)find

简介

find命令主要用于文件搜索,它的功能非常强大,可以根据不同的标准搜索任何文件,可以在任何位置进行检索

入门小案列

指定目录找文件(文件名)

代码语言:javascript
复制
find /usr -name '*.txt' -print

# -i 选项不分大小写
find /usr -iname '*.txt' -print 

# 使用通配符寻找多个 类型文件名
find /usr/include \(-iname  '*.c'  -o -name "*.x" \) -print

指定目录找文件夹名

代码语言:javascript
复制
# 找文件夹 用 -path 选项 会把路径 符合规则的全部取出
find /usr/include -path "X*" -print # 以X 开头的文件名字

使用正则表达式搜索

代码语言:javascript
复制
# 启用正则表达式 -regextype "posix-egrep" -regex
find /usr/include -regextype "posix-egrep" -regex '.*(\.c|\.x)$'   -print # 搜索以.c 或者 .x 结尾的文件

排除搜索

代码语言:javascript
复制
# 使用 !过滤掉符合规则的文件
find /usr/find ! -iname "*.h" -print

查找文件类型

代码语言:javascript
复制
# f 是所有文件  d 是所有目录
find /usr/include -type  f -print 

基于目录深度搜索

代码语言:javascript
复制
# -maxdepth 1 指定递归深度为当前一层
find /usr/include -maxdepth 1 -type f -print

# -mindepth 2 指定最低深度为 第二层
find /usr/include -mindepth 2 -type f -print 

根据文件时间搜索

代码语言:javascript
复制
# -atime 访问时间 7与系统时间相比大于等于7天 -7 与系统时间比小于7天  +7与系统时间币大于7天
find /usr/include  -type f  -atime -7 -print
# -mtime 修改时间 
find /usr/include -type f -mtime -7 -print
# -ctime 元数据修改时间,比如权限,拥有者等被修改
find /usr/include -type f -ctime -7 -print
# 以分钟为单位
find /urs/include -type f -amin -7 -print
# 比某一文件 时间更 新 -newer
find /usr/include -newer out.txt -type f -print

基于文件大小搜索

代码语言:javascript
复制
# -size 指定大小 + 表示大于 - 表示小于 不填默认大于等于
find /usr/include -type f -size +2k -print
find /usr/include -type f -size +2M -print 
find /usr/include -type f -size +2G -print 

结合find执行命令或动作

上面的 -print 操作都是打印匹配的文件路径,删除就是 -delete 当然还有其他操作,比如将匹配的文件复制到指定文件路径下,使用 -exec cp {} ./temp; 参数

代码语言:javascript
复制
find . -type f -size -2k -delete 
# -exec XXXX \;是执行其他操作 以分号结束 {}代表匹配到每一条记录
find . -type f -size -2k -exec cp {} ./temp/

让find跳过特定目录

代码语言:javascript
复制
# 使用 -prune 跳过 指定路径
find / -path "/root" -prune -o -type d -print 

基于文件权限和所有权的匹配搜索

代码语言:javascript
复制
# 使用-perm 指定文件权限 匹配
find . -type f -name "*.c" -perm 644 -print
# 使用否定参数 联合使用
find . -type f -name "*.c" ! -perm 644 -print
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • shell入门系列(7)find
    • 简介
      • 入门小案列
        • 指定目录找文件(文件名)
        • 指定目录找文件夹名
        • 使用正则表达式搜索
        • 排除搜索
        • 查找文件类型
        • 基于目录深度搜索
        • 根据文件时间搜索
        • 基于文件大小搜索
        • 结合find执行命令或动作
        • 让find跳过特定目录
        • 基于文件权限和所有权的匹配搜索
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档