首页 文章

如何使用find命令查找列表中包含扩展名的所有文件?

提问于
浏览
148

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

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

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

9 回答

  • -1
    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/
    
  • 6
    find -regex ".*\.\(jpg\|gif\|png\|jpeg\)"
    
  • 22
    find /path -type f \( -iname "*.jpg" -o -name "*.jpeg" -o -iname "*gif" \)
    
  • 10
    find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log
    
  • 6
    find /path/to/ -iname '*.gif' -o -iname '*.jpg' -o -iname '*.png' -o -iname '*.jpeg'
    

    将工作 . 可能会有更优雅的方式 .

  • 144

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

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

    在Mac OS上使用

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

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

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

    where(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
    

    注意!〜而不是〜

  • 1

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

    -E 使您免于逃离正则表达式中的parens和pipe .

相关问题