【问题标题】:How to use find command to find all files with extensions from list?如何使用 find 命令从列表中查找所有带有扩展名的文件?
【发布时间】:2011-02-06 23:15:24
【问题描述】:

我需要从目录中找到所有图像文件(gif、png、jpg、jpeg)。

find /path/to/ -name "*.jpg" > log

如何修改此字符串以不仅查找 .jpg 文件?

【问题讨论】:

    标签: unix shell find


    【解决方案1】:
    find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
    

    【讨论】:

    • 在 mac 上不适用于我,但可以使用 -E(扩展)选项(也许管道是扩展功能?):find -E /path/to -iregex ".*\ .(jpg|gif|png|jpeg)" > 日志
    【解决方案2】:
    find /path/to/  \( -iname '*.gif' -o -iname '*.jpg' \) -print0
    

    会起作用。可能有更优雅的方式。

    【讨论】:

    • -iname 不区分大小写
    • @Gerald:您可能需要将 OR 表达式分组到转义括号中:find /path/to/ \( -iname '*.gif' -o -iname '*.jpg' \) -exec ls -l {} \; 否则 exec 仅适用于最后一部分(在这种情况下为 -iname '*.jpg' )。
    • 这是一个重要的评论。 find /path/to/ -iname '*.gif' -o -iname '*.jpg' -print0 只会打印 jpg 文件!这里需要括号:find /path/to/ \( -iname '*.gif' -o -iname '*.jpg' \) -print0
    【解决方案3】:

    find -E /path/to -regex ".*\.(jpg|gif|png|jpeg)" > log

    -E 使您不必逃避正则表达式中的括号和管道。

    【讨论】:

    • 我的发现没有这个-E
    • 嗯,-E 选项告诉find 使用“扩展正则表达式”。其他几个工具也有类似的选项,但我不确定这个选项是否适用于所有 UNIX 发行版。
    • 也适用于 Mac OS。
    • @tboyce12 在 ubuntu 上工作,我可以指定 -regextype 来简化正则表达式:find . -regextype posix-extended -regex ".*\.(jpg|gif|png|jpeg)"
    • @cjm 也许find -E /path/to -iregex ".*\.(jpg|gif|png|jpeg)" > log。使用-iregex 标志告诉find 不区分大小写。
    【解决方案4】:
    find /path/to/ -type f -print0 | xargs -0 file | grep -i image
    

    这使用file 命令来尝试识别文件的类型,而不考虑文件名(或扩展名)。

    如果/path/to 或文件名包含字符串image,则上述内容可能会返回虚假命中。在这种情况下,我建议

    cd /path/to
    find . -type f -print0 | xargs -0 file --mime-type | grep -i image/
    

    【讨论】:

    • 抱歉,点击错误,我显然无法撤消反对票... :(
    【解决方案5】:
    find /path -type f \( -iname "*.jpg" -o -name "*.jpeg" -o -iname "*gif" \)
    

    【讨论】:

    • 您能解释一下为什么在 name/iname 参数周围添加(转义)括号吗?
    • 不一致有什么原因吗? -iname *.jpg-o -name *.jpeg-o -iname *gif 的格式都略有不同。
    • 如果您至少可以解释与其他答案的不同之处。
    【解决方案6】:

    在 Mac OS 上使用

    find -E packages  -regex ".*\.(jpg|gif|png|jpeg)"
    

    【讨论】:

      【解决方案7】:

      作为对@Dennis Williamson 上述回复的补充,如果您希望相同的正则表达式对文件扩展名不区分大小写,请使用 -iregex :

      find /path/to -iregex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
      

      【讨论】:

        【解决方案8】:
        find -regex ".*\.\(jpg\|gif\|png\|jpeg\)"
        

        【讨论】:

          【解决方案9】:

          如果文件没有扩展名,我们可以查找文件 mime 类型

          find . -type f -exec file -i {} + | awk -F': +' '{ if ($2 ~ /audio|video|matroska|mpeg/) print $1 }'
          

          其中 (audio|video|matroska|mpeg) 是 mime 类型的正则表达式

          &如果你想删除它们:

          find . -type f -exec file -i {} + | awk -F': +' '{ if ($2 ~ /audio|video|matroska|mpeg/) print $1 }' | while read f ; do
            rm "$f"
          done
          

          或删除除这些扩展之外的所有其他内容:

          find . -type f -exec file -i {} + | awk -F': +' '{ if ($2 !~ /audio|video|matroska|mpeg/) print $1 }' | while read f ; do
            rm "$f"
          done
          

          注意 !~ 而不是 ~

          【讨论】:

            【解决方案10】:

            添加-regextype posix-extended 选项在我的情况下有效:

            sudo find . -regextype posix-extended -regex ".*\.(css|js|jpg|jpeg|png|ico|ttf|woff|svg)" -exec chmod 0640 {} \;
            

            【讨论】:

              猜你喜欢
              • 2012-02-11
              • 1970-01-01
              • 1970-01-01
              • 2011-06-27
              • 2016-10-18
              • 1970-01-01
              • 2015-07-27
              • 2014-08-19
              • 1970-01-01
              相关资源
              最近更新 更多