`find -name`模式匹配多个模式

本文翻译自:`find -name` pattern that matches multiple patterns

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 Documents -name "*.{py,html}"获取目录中所有python和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. 模式('{}')中的大括号不被认为是特殊的(即,find。-name'foo {1,2}'匹配名为foo {1,2}的文件,而不是文件foo1和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. 如果找不到它,perl单行(或类似)就可以了。

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. 如果你有更好的解决方案,请随意破解。


#1楼

参考:https://stackoom.com/question/4kvS/find-name-模式匹配多个模式


#2楼

Use -o , which means "or": 使用-o ,表示“或”:

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)? 您使用的是bash(或Windows上的Cygwin)吗? 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. 编辑 :应用@artbristol评论答案。


#3楼

You could programmatically add more -name clauses, separated by -or : 您可以通过编程方式添加更多-name子句,以-or分隔:

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

Or, go for a simple loop instead: 或者,转而使用简单的循环:

for F in Documents/*.{py,html}; do ...something with each '$F'... ; done

#4楼

#! /bin/bash
filetypes="*.py *.xml"
for type in $filetypes
do
find Documents -name "$type"
done

simple but works :) 简单但有效:)


#5楼

I had a similar need. 我有类似的需求。 This worked for me: 这对我有用:

find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print

#6楼

This works on AIX korn shell. 这适用于AIX korn shell。

find *.cbl *.dms -prune -type f -mtime -1

This is looking for *.cbl or *.dms which are 1 day old, in current directory only, skipping the sub-directories. 这是寻找1天前的*.cbl*.dms ,仅在当前目录中,跳过子目录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值