搜索路径时 ** 是如何工作的

发布于 2025-01-20 07:12:45 字数 47 浏览 0 评论 0原文

我没有找到很多关于此的信息,据我所知它递归地匹配文件名和目录,但它是如何工作的?

I didn't find a lot of info about this, as far as I know it matches filenames and directories recursively, but how does it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2025-01-27 07:12:45

Glob-expression **用于匹配所有文件零或更多目录和子目录。如果模式后面是/,则仅目录和子目录匹配。

这意味着在命令行上的路径名扩展模式中,它用于递归文件搜索。

根据您使用的外壳,需要启用它。在bash中,这是通过:

$ shopt -s globstar

以下示例:

# list all files recursively
$ echo **
# list all files recursively that end with .txt
$ echo **/*.txt
# list all files recursively that are in a subdirectory foo
$ echo **/foo/**

当心以下模式不递归地**。txt。这只是将两个单一星号球体的组合视为与*。TXT相同的组合。

注意: bash和zsh之间存在细微的差异,但总体而言,它的效果相同。

The glob-expression ** is used to match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.

This means that it is used in a recursive file-search during path-name expansion patterns on the command line.

Depending on the shell you use, it needs to be enabled. In bash this is done with:

$ shopt -s globstar

Here are examples:

# list all files recursively
$ echo **
# list all files recursively that end with .txt
$ echo **/*.txt
# list all files recursively that are in a subdirectory foo
$ echo **/foo/**

Beware that the following pattern does not work recursively **.txt. This is just seen as a combination of two single asterisk globs and is identical to *.txt.

Note: there are subtle differences between bash and zsh, but in general it works the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文