You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在sh中启用globstar以支持**递归匹配?

在sh中实现递归通配(替代bash的globstar)

POSIX标准的sh(比如dash、ash这类基础shell)本身不支持**这种递归通配符(这是bash的扩展特性),要实现匹配指定目录下所有层级的特定文件,最通用且兼容的方法是使用find命令。

基础用法

要匹配/path目录下所有递归层级的.proto文件,直接执行:

find /path -name "*.proto"

如果需要确保只匹配文件(排除同名目录),可以加上-type f参数:

find /path -type f -name "*.proto"

将结果传给其他命令使用

如果要把匹配到的文件作为参数传给其他命令,比如查看所有文件内容,推荐用-exec选项(避免文件名含空格/特殊字符时出问题):

find /path -type f -name "*.proto" -exec cat {} +

{} +表示把所有匹配到的文件一次性传给命令,效率比{} \;更高。

如果需要用管道处理,且文件名可能包含换行符这类特殊字符,用-print0配合xargs -0更安全:

find /path -type f -name "*.proto" -print0 | xargs -0 grep "some_pattern"

为什么sh不支持**?

**递归通配是bash、zsh等扩展shell的特性,不属于POSIX shell标准的通配符规则,所以原生sh没有这个功能,必须依赖find这类工具实现递归查找。

内容的提问来源于stack exchange,提问作者Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 07:17:06