使用“**”模式匹配部分文件名
Matching partial filenames with the "**" pattern
globstar
bash 选项的文档如下:
globstar
If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If
the pattern is followed by a ‘/’, only directories and subdirectories
match.
这让我想到给定这样的层次结构:
└── dir1
└── dir2
└── dir3
└── file.txt
我可以使用 **file*
这样的模式在这个树结构中匹配 file.txt
。但它不起作用:
ls **file*
ls: cannot access '**file*': No such file or directory
这虽然有效:
ls **/file*
dir1/dir2/dir3/file.txt
我想知道 **
是否应该匹配 文件目录或完整文件名。链接到更精确的文档将不胜感激。
文档是正确的,只是有点电报。如果设置了 globstar 并且 **
被用作 glob 的一部分,它出现的模式的组件(组件是由 / 字符分隔的模式部分)将匹配对象:
具有零个或多个组件的目录路径
当前上下文中的文件名,如果 globstar 组件后面没有跟 /。
请注意 **/
将匹配当前目录(具有零个组件的路径),即使 / 似乎不匹配任何内容。
如果您想匹配任何子目录中的 *file*
(递归),您可以使用 **/*file*
。然后 **/
将匹配所有目录和子目录(递归),并且在每个目录和子目录中都会尝试将 *file*
匹配为文件名(包括目录名称)。
node-glob 的文档以与 Bash 相同的方式处理 **
的扩展,似乎更精确:
If a "globstar" (**
) is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories.
[.. The double-star character] is supported in the manner of bsdglob and bash 4.3, where **
only has special significance if it is the only thing in a path part. That is, a/**/b
will match a/x/y/b
, but a/**b
will not.
所以 **
必须是路径部分中唯一的东西。
globstar
bash 选项的文档如下:
globstar
If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.
这让我想到给定这样的层次结构:
└── dir1
└── dir2
└── dir3
└── file.txt
我可以使用 **file*
这样的模式在这个树结构中匹配 file.txt
。但它不起作用:
ls **file*
ls: cannot access '**file*': No such file or directory
这虽然有效:
ls **/file*
dir1/dir2/dir3/file.txt
我想知道 **
是否应该匹配 文件目录或完整文件名。链接到更精确的文档将不胜感激。
文档是正确的,只是有点电报。如果设置了 globstar 并且 **
被用作 glob 的一部分,它出现的模式的组件(组件是由 / 字符分隔的模式部分)将匹配对象:
具有零个或多个组件的目录路径
当前上下文中的文件名,如果 globstar 组件后面没有跟 /。
请注意 **/
将匹配当前目录(具有零个组件的路径),即使 / 似乎不匹配任何内容。
如果您想匹配任何子目录中的 *file*
(递归),您可以使用 **/*file*
。然后 **/
将匹配所有目录和子目录(递归),并且在每个目录和子目录中都会尝试将 *file*
匹配为文件名(包括目录名称)。
node-glob 的文档以与 Bash 相同的方式处理 **
的扩展,似乎更精确:
If a "globstar" (
**
) is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories.[.. The double-star character] is supported in the manner of bsdglob and bash 4.3, where
**
only has special significance if it is the only thing in a path part. That is,a/**/b
will matcha/x/y/b
, buta/**b
will not.
所以 **
必须是路径部分中唯一的东西。