本文翻译自:`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 ,仅在当前目录中,跳过子目录。

`find -name`模式匹配多个模式相关推荐

  1. 字符串的模式匹配 (朴素模式匹配算法 ,KMP算法)

    字符串的模式匹配 寻找字符串p在字符串t中首次出现的起始位置 字符串的顺序存储 typedef struct {char str[MAXSIZE];int length; }seqstring; 朴素 ...

  2. 朴素模式匹配与KMP模式匹配算法

    一.朴素模式匹配 朴素模式匹配算法 就是遍历主串,然后把待匹配字符串与子串进行比对,先把待匹配子串的第一个字母与主串进行匹配,若匹配成功,则两串的坐标依次 ++,匹配不成功时,主串坐标返回到开始匹配时 ...

  3. python中正则表达式的默认匹配方式为_Python模式匹配与正则表达式

    1.1 不用正则表达式来匹配文本 假设我希望在一个字符串中找到电话号码,电话号码的格式为三个数字,一个短横线,四个数字,一个短横线,四个数字 比如:131-3310-5293和132-2670-986 ...

  4. SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易

    目录 CLR 用户定义函数 模式匹配 数据提取 模式存储 匹配 在匹配项中进行数据提取 总结 尽管 T-SQL 对多数数据处理而言极其强大,但它对文本分析或操作所提供的支持却很少.尝试使用内置的字符串 ...

  5. grep 或 egrep 或awk 过滤两个或多个关键词|使用grep匹配“与”或者“或”模式

    原文地址为: grep 或 egrep 或awk 过滤两个或多个关键词|使用grep匹配"与"或者"或"模式 转自:http://hi.baidu.com/ch ...

  6. 14 模式匹配和样例类

    文章目录 模式匹配 守卫 变量模式 类型模式 匹配数组.列表和元组 提取器 变量声明中的模式 for表达式的模式 样例类 copy方法和带名参数 case语句的中置表示法 匹配嵌套结构 密封类 偏函数 ...

  7. 数据结构之字符串模式匹配

    程序源代码:点击打开链接 1.引入 字符串模式匹配.首先我们引入目标串,模式串的概念,而字符串模式匹配就是查找模式串在目标串中的位置. 2.brute-Force算法 brute-Force算法,我的 ...

  8. Scala学习(八)模式匹配

    文章目录 模式匹配 基本语法 模式守卫 模式匹配类型 匹配常量 匹配类型 匹配数组 匹配列表 匹配元组 匹配对象及样例类 模式匹配 基本语法 模式匹配语法中,采用 match 关键字声明,每个分支采用 ...

  9. 7-主题模式(Topics)

    参考官方文档 rabbitmq版本:3.8.3 amqp-client版本:5.7.1 发布订阅模式是广播式的发送消息,所有消费者都会接收相同的消息.路由模式通过指定路由键可选择地发送消息,只有匹配的 ...

最新文章

  1. 码云新建仓库,与本地仓库关联
  2. Windows Embedded Compact 7网络编程概述(上)
  3. 用JavaScript实现动态省市县三级联动
  4. 原作者出局,Faker.js已被社区控制
  5. Python获取同目录下json文件内容
  6. 【三维深度学习】多视角立体视觉模型R-MVSNet
  7. 三角形内部线性插值方法
  8. java与javac版本不一致问题
  9. 服务器远程桌面日志,Windows记录远程桌面3389登录日志
  10. postgresal去重_postgresql数据库去重方法
  11. A2 雷达多点触控
  12. Oracle 11g 创建数据库
  13. 国内第一本Julia语言书籍《Julia语言程序设计》出版了!
  14. 一款勒索病毒的详细分析
  15. Uva 1616 Caravan Robbers (商队抢劫者)
  16. 什么是单元测试?该怎么做单元测试?
  17. CALL入门篇一:CALL的本质
  18. 给CheckBox换样式
  19. HTML/JSP/CSS网页编写实例,附源码
  20. outlook使用笔记

热门文章

  1. Android binder学习一:主要概念
  2. Manual Create Database OMF
  3. Android 高级控件ListView用法
  4. InstanceBeginEditable dw中特有标识
  5. wav格式的音频文件 16位转化成8位的
  6. 聊聊restful和restframework
  7. LeetCode——Find Minimum in Rotated Sorted Array II
  8. 2016HUAS暑假集训训练题 F - 简单计算器
  9. java keytool详解
  10. UIRefreshControl