`find -name`模式匹配多个模式 [英] `find -name` pattern that matches multiple patterns

查看:198
本文介绍了`find -name`模式匹配多个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过命令查找文件名*。{py,html}。来获取目录中所有python和html文件的列表。 / b>

接下来是手册页:
$ b


}')不被认为是特殊的(即find。-name'foo {1,2}'匹配一个名为foo {1,2}的文件,而不是文件foo1和foo2。


由于这是管道链的一部分,所以我希望能够指定在运行时匹配哪些扩展(无硬编码)。这样做,perl单行(或类似)将罚款。

编辑:我最终提出的答案包括各种废话,是有点长,所以我把它发布为如果你有更好的解决方案,可以随意破解。

-o ,这意味着或:

  find Documents \(-name* .py-o -name* .html\)

编辑:对不起,只需重新阅读问题...您需要以编程方式构建该命令行,而不是那很简单。

你使用bash(或Windows上的Cygwin)?如果你是,你应该可以这样做:

  ls ** / *。py ** / *。html 

这可能更容易以编程方式进行构建。



编辑:应用@artbristol对答案的评论。

I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}".

Then along came the man page:

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.

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.

Edit: 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.

解决方案

Use -o, which means "or":

find Documents \( -name "*.py" -o -name "*.html" \)

Edit: Sorry, just re-read the question... you'd need to build that command line programmatically, which isn't that easy.

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.

Edit: Applied @artbristol comment to the answer.

这篇关于`find -name`模式匹配多个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆