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

UnixShellFind

Unix Problem Overview


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 find not only .jpg files?

Unix Solutions


Solution 1 - Unix

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

Solution 2 - Unix

find /path/to/  \( -iname '*.gif' -o -iname '*.jpg' \) -print0

will work. There might be a more elegant way.

Solution 3 - Unix

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

The -E saves you from having to escape the parens and pipes in your regex.

Solution 4 - Unix

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

This uses the file command to try to recognize the type of file, regardless of filename (or extension).

If /path/to or a filename contains the string image, then the above may return bogus hits. In that case, I'd suggest

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

Solution 5 - Unix

find /path -type f \( -iname "*.jpg" -o -name "*.jpeg" -o -iname "*gif" \)

Solution 6 - Unix

On Mac OS use

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

Solution 7 - Unix

In supplement to @Dennis Williamson 's response above, if you want the same regex to be case-insensitive to the file extensions, use -iregex :

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

Solution 8 - Unix

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

Solution 9 - Unix

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 ~

Solution 10 - Unix

Adding -regextype posix-extended option only worked in my case:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKirzillaView Question on Stackoverflow
Solution 1 - UnixDennis WilliamsonView Answer on Stackoverflow
Solution 2 - UnixJoseKView Answer on Stackoverflow
Solution 3 - Unixtboyce12View Answer on Stackoverflow
Solution 4 - UnixunutbuView Answer on Stackoverflow
Solution 5 - Unixghostdog74View Answer on Stackoverflow
Solution 6 - Unix7ynk3rView Answer on Stackoverflow
Solution 7 - UnixTCWView Answer on Stackoverflow
Solution 8 - UnixShrikantView Answer on Stackoverflow
Solution 9 - Unixk. DoeView Answer on Stackoverflow
Solution 10 - UnixAamirRView Answer on Stackoverflow