Linux find 常用命令

本文详细介绍了Linux下find命令的各种高级用法,包括查找特定类型的文件、按文件大小或修改时间筛选、查找隐藏文件等,并提供了实用的例子,如查找大于10KB的日志文件、24小时内改动过的文件等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://blog.sina.com.cn/s/blog_6c963df701018q6u.html


说明:
以下内容,若标注在 [ ] 中括号内的均可省略,除非做特别说明。
但严格来说,若缺少 [ ] 中括号内的内容,在命令呈现的语义和返回结果上,偶尔会有些许差异。


正文:
0、使用说明
man find
找到所有关于 find 命令的说明

1、查找目录下的内容
1.1 所有文件
find /full/path -type f

1.2 所有文件夹
find /full/path -type d

1.3 所有 .php 文件
find /full/path [-type f] -name "*.php"

1.4 所有 .log 和 .txt 后续文件(多重选择,注意:括号内两边均带空格)
find /full/path [-type f] \( -name "*.log" -o -name "*.txt" \)
或者 find /full/path [-type f] -regex ".*\.\(txt\|sh\)"

1.5 所有不是 .log 和 .txt 后续的文件(否定参数)
find /full/path [-type f] -not \( -name "*.log" -o -name "*.txt" \)
或者 find /full/path [-type f] ! \( -name "*.log" -o -name "*.txt" \)

1.6 查找大于 10KB 的日志文件
find /full/path [-type f] -size +10k

除k之外,还可以用其他文件大小单元
b—块(512字节)
c—字节
w—字(2字节)
k—千字节
M—兆字节
G—吉字节

1.7 查找 24 小时内改动过的文件
find /full/path [-type f] -ctime -1

1.8 修改时间为7天以前的文件
find /full/path [-type f] -mtime +7

1.9 查找当前目录下所有的隐藏(普遍)文件(可以用 -iregex 来去除大小写敏感)
find /full/path [-type f] -regex ".*\/\.[^/]*"

2.0 查找空文件(夹)
find /full/path -empty

2.1 查找当前目录中以及一级子目录中的 php 文件
find /full/path [-type f] -name "*.php" -maxdepth 2

2.2 查找目录下除report子目录以外的所有文件
find /full/path -path "*report" -prune -o -print

2.3 查找权限是 777 的文件
find /full/path [-type f] -perm 777

2.4 输出当前目录下所有 txt 文件的内容
find /full/path [-type f] -name "*.txt" -exec cat {} \;
或者 find /full/path [-type f] -name "*.txt" | xargs -i cat {}

在这个命令中,{}是一个特殊的字符串,与 -exec 选项结合使用。
对于每一个匹配的文件,{}会被替换成相应的文件名。

2.5 只搜索最小深度为3,最大尝试为5的所有文件夹 ( -maxdepth参数需要放在最前面 )
find /full/path -mindepth 3 -maxdepth 5 -type d

2.6 删除匹配的文件
find /full/path -type f -name "*.bak" -delete

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值