- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Java7 中,sun.nio.fs.Globs
和 getPathMatcher()
似乎将习语 **
理解为跨越目录边界 (参见 the getPathMatcher javadoc)匹配零个或多个字符 的方式。
I could swear some flavor of shell (zsh, bash, tcsh) with some appropriate option settings was giving me the same behavior at some point.但是对于我的生活,我不记得如何启用它,我什至开始怀疑我的内存是否在某个时候工作过......(编辑:zsh 提供了这种行为,但仅适用于目录,即 "**.gz"
不匹配 foo/bar/fubar.gz
,但 "**/*.gz"
匹配)。
事实上,查看 glob 的各种实现的文档(例如 POSIX glob(3)、glob(7) 和 Perl 的 File::Glob)似乎没有任何地方提到这种行为。一个异常(exception)是 Ruby 的 Dir.glob()
,它明确处理 **
。
(最初的问题是:“有谁知道如何在 unix shell(例如 zsh)中启用这种行为?”,但现在请参阅下面的已编辑问题)。
作为奖励问题:有人知道如何在 Google 中搜索 '**'
吗?...
编辑问题
事实上,看起来我的 zsh
shell 确实接受了这种行为(感谢对这一事实的回应,并促使我进一步研究)。我认为它不是的原因来自以下微妙之处: "**.gz"
不会匹配 <path>/<prefix>.gz
,但 "**/*.gz"
会。这是一个例子。让我们从以下树开始:
$ find . -type f | sort
./foo/a.gz
./foo/bar/fubar/abc.gz
./foo/bar/x.gz
./foo/bar/y.gz
./xyz.gz
"**.gz"
在子目录内不匹配,只匹配 "*.gz
"将:$ ls -1 **.gz
xyz.gz
"**/*.gz"
则:$ ls -1 **/*.gz
foo/a.gz
foo/bar/fubar/abc.gz
foo/bar/x.gz
foo/bar/y.gz
xyz.gz
@Test
public void testStar() {
String pat = Globs.toUnixRegexPattern("*.gz");
assertEquals("^[^/]*\\.gz$", pat);
}
@Test
public void testStarStar() {
// '**' allows any number of directories on the path
// this apparently is not POSIX, although darn useful
String pat = Globs.toUnixRegexPattern("**.gz");
assertEquals("^.*\\.gz$", pat);
}
"**"
匹配路径上的任何字符(即它在正则表达式中变为 ".*"
),无论是否在子目录中,以及是否作为文件名的一部分。Globs
是 sun.nio.fs.Globs.toUnixRegexPattern(String glob)
的副本,因为我需要一些跨平台的东西)。
最佳答案
在 POSIX shell :
The slash character in a pathname shall be explicitly matched by using one or more slashes in the pattern; it shall neither be matched by the asterisk or question-mark special characters nor by a bracket expression
globstar
:
[An asterisk] 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.
$ shopt -s globstar
$ ls **/
$ shopt -u globstar
$ ls **/
关于shell - 文件 glob() 什么时候理解 ** ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11974196/
这个问题已经有答案了: 奥 git _a (40 个回答) 已关闭 4 年前。 我有一个包含不同类型文件的文件夹。我只需要处理图像文件(jpg、png、jpeg、JPG...)。为此,我使用 glob
我编写了以下 Python 代码: #!/usr/bin/python # -*- coding: utf-8 -*- import os, glob path = '/home/my/path' f
我正在尝试使用 glob 和 os 在目录中找到最新的 .zip 文件。有趣的是,我进行了以下设置并且以前可以正常工作: max(glob.glob('../directory/*.zip'), ke
这是一个非常愚蠢的问题,但我还没有真正找到令人满意的答案:How do I use gulp globbing to select all files in all某个目录下的子目录? 我已经尝试过:
我正在尝试为 CLI 脚本之一提供通配符支持,并且我正在使用 pythons glob 模块。为了进行测试,我尝试了以下方法: >>> import glob >>> for f in glob.gl
目前我的os.walk代码列表是指定目录下所有目录下的所有文件。 top = /home/bludiescript/tv-shows for dirpath, dirnames, fi
有一点奇怪的问题,因为这段代码似乎在我 friend 的 mac 上运行良好,但在我的 ubuntu 16.04 上根本不起作用。 通过我的 python,我正在运行以下命令 filenames =
import glob list = glob.glob(r'*abc*.txt') + glob.glob(r'*123*.txt') + glob.glob(r'*a1b*.txt') for i
我编写了以下 Python 代码: #!/usr/bin/python # -*- coding: utf-8 -*- import os, glob path = '/home/my/path' f
我想知道是否可以使用 glob.glob("**/*.jpg") 来获取多个文件夹中的所有图像,但作为迭代器以避免填充内存 目前,我将以下代码与 glob 一起使用: for file in glob
当我尝试运行此代码时: import pandas as pd import glob files = [pd.read_excel(p, skipfooter=1) for p in glob.gl
我的桌面上有一个文件夹,其中包含 200 多个 txt 文件。我想在每个文件中打印行号 1050.539。这是我所拥有的: import glob f=glob.glob('/Users/me/Des
我注意到 glob.glob('**/*', recursive=True) 以呼吸优先搜索的方式遍历目录。有没有办法让它成为深度优先? 最佳答案 要遍历目录树,请切换到 os.walk它可以让你控制
我正在尝试使用 python 3 从包含文本文档的文件夹中读取数据。具体来说,这是对 LingSpam 垃圾邮件数据集的修改。我希望我编写的代码返回所有 1893 个文本文档名称,但是,代码反而返回前
现在,我使用 subprocess 来调用 find ,它可以很好地完成工作,但我追求的是 pythonic 的做事方式。 这是当前代码: cmd = "find /sys/devices/pci*
这个问题在这里已经有了答案: How to use glob() to find files recursively? (28 个回答) 关闭去年。 我想打开一个文件夹中的一系列子文件夹并找到一些文本
我可以使用 boto3 的过滤器工具在存储桶中查找类似于使用 glob 目录中的文件的键(技术上是子键)吗? 我想得到一个具有这样模式的键列表 "key/**//**.gz" . 最佳答案 不幸的是没
我正在寻找一些用 BFN 规则描述的广泛扩展的方言(比如这个 https://github.com/vmeurisse/wildmatch + globstar **)。 任何格式或语言。 OMeta
我有以下nodejs代码。客户端首先调用/api/demosounds,然后调用/api/testsounds。 var glob = require('glob-fs')({ gitignore:
我使用以下命令在python中读取目录中所有文件的名称: import glob list_of_files = glob.glob(".../*.txt") 但是列表“list_of_files”的
我是一名优秀的程序员,十分优秀!