双星号 (**) 通配符是什么意思? [英] What do double-asterisk (**) wildcards mean?

查看:142
本文介绍了双星号 (**) 通配符是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下命令,但无法解释结果:

I've tried the following command but I am having trouble interpreting the results:

ls **

我不确定它输出的是什么以及为什么.

I'm not sure exactly what it is outputting and why.

推荐答案

您很可能会看到某些 shell 的一个特殊功能,它允许通配符文件名模式跨越目录边界进行匹配,而不是单个 *,这是一个仅在目录内匹配的通配符.

You're most likely seeing a special feature of some shells that allow wildcard filename patterns to match across directory boundaries, as opposed to a single *, which is a wildcard that matches only within a directory.

如果您没有这样的外壳,** 可能等同于 *,因为匹配零个或多个字符后跟零个或多个字符"与仅匹配零个或多个字符"相同.

If you do not have such a shell, ** will likely be equivalent to *, because "matching zero or more characters followed by zero or more characters" is the same as just "matching zero or more characters".

但是如果你有这样的shell, ** 将匹配当前目录和子目录中的所有文件和目录,而 * 只匹配当前目录下的文件和目录.(在这两种情况下,点文件",名称以 . 开头的文件不匹配).

But if you do have such a shell, ** will match all files and directories in the current directory and subdirectories, whereas * only matches files and directories in the current directory. (In both cases "dot files", those with names starting with ., are not matched).

** 的真正力量在于您在更具体的模式中使用它时.例如,您可以使用 **/*.txt 指定所有 .txt 文件,而不管它们在哪个子目录中,而 *.txt只匹配当前目录中的那些.

**'s real power comes when you use it in more specific patterns. For example, you can specify all .txt files no matter what subdirectory they are in with **/*.txt, whereas *.txt only matches those in the current directory.

您应该查看 shell 的通配符匹配规则,以确定您的 shell 正在做什么.例如,bash 手册 说:

You should look at the wildcard matching rules for your shell to know for sure what your shell is doing. For example, the bash manual says:

*
匹配任何字符串,包括空字符串.当...的时候'globstar' shell 选项已启用,并且在文件名中使用了 '*'扩展上下文,用作单个模式的两个相邻的*"将匹配所有文件和零个或多个目录和子目录.如果后跟一个/",两个相邻的*"将只匹配目录和子目录.

*
Matches any string, including the null string. When the 'globstar' shell option is enabled, and '*' is used in a filename expansion context, two adjacent '*'s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a '/', two adjacent '*'s will match only directories and subdirectories.

在最新版本的 bash 中,'globstar' shell 选项默认是禁用的.通过以下方式启用:

In recent versions of bash the 'globstar' shell option is disabled by default. Enabled via:

shopt -s globstar

我相信 zsh 也支持这种语法.

I believe zsh also supports this syntax.

请务必记住,通配符是由 shell 扩展的,而不是由 ls 命令扩展的.如果您键入 ls **ls *.txtls 命令本身永远不会看到 * 字符;它只看到与模式匹配的文件扩展列表,就像您在命令行中输入了整个列表一样.

It's important to keep in mind that wildcards are expanded by the shell, not by the ls command. If you type ls **, or ls *.txt, the ls command itself never sees the * characters; it only sees an expanded list of files matching the pattern, just as if you had typed the entire list on the command line.

这篇关于双星号 (**) 通配符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆