How to use find command to find all files with extensions from list?

后端 未结 9 1532
旧时难觅i
旧时难觅i 2020-12-04 05:45

I need to find all image files from directory (gif, png, jpg, jpeg).

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

How to modify this string to f

9条回答
  •  悲哀的现实
    2020-12-04 06:18

    in case files have no extension we can look for file mime type

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

    where (audio|video|matroska|mpeg) are mime types regex

    &if you want to delete them:

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

    or delete everything else but those extensions:

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

    notice the !~ instead of ~

提交回复
热议问题