【问题标题】:find all files except e.g. *.xml files in shell查找除 e.g. 之外的所有文件外壳中的 *.xml 文件
【发布时间】:2012-09-26 13:31:37
【问题描述】:

使用bash,如何在目录结构中查找除*.xml 文件之外的文件? 我只是想使用

find . -regex ....

正则表达式:

'.*^((?!xml).)*$'

但没有预期的结果...

或者是否有其他方法可以实现这一点,即没有正则表达式匹配?

【问题讨论】:

  • 三倍的好答案!我应该授予哪个已接受的答案?两难境地……

标签: regex bash shell find


【解决方案1】:

find . ! -name "*.xml" -type f

【讨论】:

  • 你能解释一下它是如何与 -name 参数周围的父母一起找到的吗?
【解决方案2】:
find . -not -name '*.xml'

应该做的伎俩。

【讨论】:

    【解决方案3】:

    比上面的 find 解决方案更草率,而且它做的工作比它需要的要多,但你可以做

    find . | grep -v '\.xml$'
    

    另外,这是源代码树吗?也许您在树中拥有所有源代码和一些 XML,但您只想获取源代码?如果你使用ack,你可以这样做:

    ack -f --noxml
    

    【讨论】:

    • 感谢有关 ack 的提示。以前不知道ack。
    【解决方案4】:

    用 bash:

    shopt -s extglob globstar nullglob
    for f in **/*!(.xml); do 
        [[ -d $f ]] && continue
        # do stuff with $f
    done
    

    【讨论】:

      【解决方案5】:

      你也可以用 or-ring 来做,如下:

      find . -type f -name "*.xml" -o -type f -print

      【讨论】:

        【解决方案6】:

        尝试这样的正则表达式解决方案:

        find . -regextype posix-extended -not -regex '^.*\.xml$'
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-16
          • 1970-01-01
          • 1970-01-01
          • 2010-09-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多