双星号通配符是什么意思? [英] What do double-asterisk wildcards mean?
问题描述
我尝试了以下命令,但无法解释它:
I've tried the following command but I am having trouble interpreting it:
ls **
但是我不确定输出的是什么,为什么会这样.
but I'm not sure exactly what it is outputting and why that is the result.
推荐答案
它可能是某些shell的特殊功能,它允许**
进行递归匹配,而不是单个*
,后者仅在当前目录.
It's probably using a special feature of some shells that lets **
do a recursive match, as opposed to a single *
, which matches only in the current directory.
通配符*
匹配当前目录中的任何文件或目录(其名称不以.
开头).
The wildcard *
matches any file or directory (whose name doesn't start with .
) in the current directory.
**
可能与*
等效,具体取决于您所使用的shell和设置.它可以匹配零个或多个字符,然后匹配零个或多个字符,这与匹配零个或多个字符一次相同.
Depending on which shell you're using, and with which settings, **
may be equivalent to *
; it could match zero or more characters followed by zero or more characters, which is the same as matching zero or more characters just once.
但是对于某些外壳程序和某些设置,**
是*
的递归版本,匹配当前目录和子目录中的所有文件和目录.
But with some shells, with some settings, **
is a recursive version of *
, matching all files and directories in the current directory and subdirectories.
引用 bash
手册:
`*'
匹配任何字符串,包括空字符串.当...的时候 启用了"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.
仅当通过以下方式设置globstar
选项时,此方法才有效:
This works only if the globstar
option is set via:
shopt -s globstar
(默认情况下处于禁用状态),并且仅在相对较新的bash
版本中.
(it's disabled by default) and only in relatively recent versions of bash
.
我相信zsh也支持这种语法.
I believe zsh also supports this syntax.
请记住,通配符是由Shell而不是ls
命令扩展的,这一点很重要.如果键入ls **
或ls *.txt
,则ls
命令本身将永远不会看到*
字符.它只会看到扩展的文件列表,就像您在命令行上键入了整个列表一样.
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, just as if you had typed the entire list on the command line.
这篇关于双星号通配符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!