使用 find 查找与多个模式之一匹配的文件 [英] Using find to locate files that match one of multiple patterns
问题描述
我试图使用命令 find Documents -name "*.{py,html}"
获取目录中所有 python 和 html 文件的列表.
I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}"
.
然后是手册页:
模式 ('{}') 中的大括号不被认为是特殊的(即 find .-name 'foo{1,2}' 匹配名为 foo{1,2} 的文件,而不是文件 foo1和 foo2.
Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{1,2}' matches a file named foo{1,2}, not the files foo1 and foo2.
由于这是管道链的一部分,我希望能够指定它在运行时匹配哪些扩展(无硬编码).如果 find 做不到,那么 perl 单行(或类似的)就可以了.
As this is part of a pipe-chain, I'd like to be able to specify which extensions it matches at runtime (no hardcoding). If find just can't do it, a perl one-liner (or similar) would be fine.
我最终想出的答案包括各种废话,而且有点长,所以我将其发布为 an answer 对我试图抓挠的原始痒.如果您有更好的解决方案,请随意破解.
The answer I eventually came up with include all sorts of crap, and is a bit long as well, so I posted it as an answer to the original itch I was trying to scratch. Feel free to hack that up if you have better solutions.
推荐答案
使用-o
,意思是或":
find Documents ( -name "*.py" -o -name "*.html" )
<小时>
您需要以编程方式构建该命令行,这并不容易.
You'd need to build that command line programmatically, which isn't that easy.
您使用的是 bash(或 Windows 上的 Cygwin)吗?如果你是,你应该能够做到这一点:
Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:
ls **/*.py **/*.html
这可能更容易以编程方式构建.
which might be easier to build programmatically.
这篇关于使用 find 查找与多个模式之一匹配的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!