我尝试了以下命令,但我不明白结果:
ls **
**
是什么意思?我该如何使用它?
最佳答案
您很可能会看到某些 shell 的特殊功能,允许通配 rune 件名模式跨目录边界匹配,而不是单个 *
,后者是仅在目录内匹配的通配符.
如果您没有这样的 shell,**
可能相当于 *
,因为“匹配零个或多个字符后跟零个或多个字符”是与“匹配零个或多个字符”相同。
但是如果您确实有这样的 shell, **
将匹配当前目录和子目录中的所有文件和目录,而*
仅匹配当前目录中的文件和目录。 (在这两种情况下,“点文件”,即名称以 .
开头的文件,都不匹配)。
**
时,它的真正威力就会显现出来。例如,您可以使用 **/*.txt
指定所有 .txt
文件,无论它们位于哪个子目录中,而 *.txt
只匹配当前目录中的那些。
您应该查看 shell 的通配符匹配规则,以确定您的 shell 正在做什么。例如,bash
manual说:
*
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 选项。启用方式:
shopt -s globstar
我相信 zsh 也支持这种语法。
请务必记住,通配符是由 shell 扩展的,而不是由 ls
命令扩展的。如果您输入 ls **
或 ls *.txt
,ls
命令本身永远看不到 *
字符;它只会看到与模式匹配的文件的扩展列表,就像您在命令行上键入了整个列表一样。
关于bash - 双星号 (**) 通配符是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28176590/