使用查找找到与多种模式之一匹配的文件

使用查找找到与多种模式之一匹配的文件

问题描述:

我试图使用命令 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.

我最终想出的答案包括各种各样的废话,而且有点长,所以我将其发布为

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.