代码之家  ›  专栏  ›  技术社区  ›  Xiong Chiamiov

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

  •  264
  • Xiong Chiamiov  · 技术社区  · 15 年前

    我试图用命令获取目录中所有python和html文件的列表 find Documents -name "*.{py,html}" .

    接着,出现了个人主页:

    图案内的大括号(____)不被认为是特殊的(即,查找)。-名称“foo 1,2”与名为foo 1,2的文件匹配,而不是文件foo1和foo2。

    由于这是管道链的一部分,我希望能够指定它在运行时匹配的扩展(没有硬编码)。如果find不能做到这一点,那么PerlOne(或类似的)行程序就可以了。

    编辑:我最终得出的答案包括各种各样的废话,而且也有点长,所以我把它作为 an answer 原来我想抓的痒。如果你有更好的解决方案,请随意修改。

    11 回复  |  直到 6 年前
        1
  •  398
  •   charlie_pl    8 年前

    使用 -o ,意思是“或”:

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

    编辑 :抱歉,请重新阅读问题…您需要以编程方式构建命令行,这并不容易。

    您是否在使用bash(或在Windows上使用cygwin)?如果是,您应该能够做到:

    ls **/*.py **/*.html
    

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

    编辑 :对答案应用了@artbristol注释。

        2
  •  48
  •   simbabque    9 年前

    find的一些版本,主要在Linux系统上,也可能在其他版本上支持-regex和-regextype选项,这些选项可以查找名称与regex匹配的文件。

    例如

    find . -regextype posix-egrep -regex ".*\.(py|html)$" 
    

    应该在上面的例子中完成这个技巧。 但是,这不是标准的posix find函数,它依赖于实现。

        3
  •  27
  •   Brendan Long    9 年前

    您可以通过编程添加更多 -name 条款,由分隔 -or :

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

    或者,换一个简单的循环:

    for F in Documents/*.{py,html}; do ...something with each '$F'... ; done
    
        4
  •  10
  •   bkidd    15 年前

    我也有同样的需求。这对我很有用:

    find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print
    
        5
  •  8
  •   netskink    8 年前

    这将在Linux上找到所有.c或.cpp文件

    $ find . -name "*.c" -o -name "*.cpp"
    

    你不需要转义括号,除非你做了一些额外的修改。在这里的手册页上,他们说如果图案匹配,打印出来。也许他们正在努力控制印刷。在这种情况下-print充当条件并成为“an d'd”条件。它将阻止打印任何.c文件。

    $ find .  -name "*.c" -o -name "*.cpp"  -print
    

    但是如果你喜欢原始答案,你可以控制打印。这也可以找到所有.c文件。

    $ find . \( -name "*.c" -o -name "*.cpp" \) -print
    
        6
  •  4
  •   PaSe    9 年前

    我的默认值是:

    find -type f | egrep -i "*.java|*.css|*.cs|*.sql"

    像不那么刻意的过程 find Brendan Long和Stephan202等人执行:

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

        7
  •  2
  •   mnrl    11 年前
    #! /bin/bash
    filetypes="*.py *.xml"
    for type in $filetypes
    do
    find Documents -name "$type"
    done
    

    简单但有效:)

        8
  •  1
  •   Adobe    13 年前

    我需要删除子目录中的所有文件,除了一些文件。以下对我有效(指定了三种模式):

    find . -depth -type f -not -name *.itp -and -not -name *ane.gro -and -not -name *.top -exec rm '{}' +
    
        9
  •  1
  •   user7531934    8 年前
    find MyDir -iname "*.[j][p][g]"
    +
    find MyDir -iname "*.[b][m][p]"
    =
    find MyDir -iname "*.[jb][pm][gp]"
    
        10
  •  0
  •   Chris Mukherjee    10 年前

    这在AIXKornShell上工作。

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

    这是在找 *.cbl *.dms 只有一天的时间,在当前目录中,跳过子目录。

        11
  •  0
  •   Code42    6 年前

    怎么样

    ls {*.py,*.html}
    

    它在文件名中列出以.py或.html结尾的所有文件