126

I can get all jpg images by using:

find . -name "*.jpg"  

But how can I add png files to the results as well?

153

Use the -o flag between different parameters.

find ./ -type f \( -iname \*.jpg -o -iname \*.png \) works like a charm.

NOTE There must be a space between the bracket and its contents or it won't work.

Explanation:

  • type -f - only search for files (not directories)
  • \( - needed for the type -f to apply to all arguments
  • -o - logical OR operator
  • -iname - like -name, but the match is case insensitive
  • Do you need the parentheses. The command works for me without them. Are they needed for some shells? – MikeD Mar 19 '17 at 2:25
  • 2
    @miked The command will "work" without them, yes, but you'd wind up getting hits on directories that end in .png as well as files that end with .jpg, which is not exactly what was intended. – Shadur Mar 19 '17 at 8:25
  • 1
    Thanks for clarifying! The type -f does not extend and apply to both expressions without the parentheses, So, find ./ -type f -iname \*.jpg -o -type f -iname \*.png also works ... although it's two characters longer :-) – MikeD Mar 19 '17 at 11:58
  • 2
    It's a matter of operator precedence. Just like a * b + c is different from a * (b + c) – Shadur Mar 19 '17 at 12:13
  • 2
    @jdhao Good catch, amended. – Shadur Oct 24 '18 at 8:34
86

You can combine criteria with -o as suggested by Shadur. Note that -o has lower precedence than juxtaposition, so you may need parentheses.

find . -name '*.jpg' -o -name '*.png'
find . -mtime -7 \( '*.jpg' -o -name '*.png' \)  # all .jpg or .png images modified in the past week

On Linux, you can use -regex to combine extensions in a terser way. The default regexp syntax is Emacs (basic regexps plus a few extensions such as \| for alternation); there's an option to switch to extended regexps.

find -regex '.*\.\(jpg\|png\)'
find -regextype posix-extended -regex '.*\.(jpg|png)'

On FreeBSD, NetBSD and OSX, you can use -regex combined with -E for extended regexps.

find -E . -regex '.*\.(jpg|png)'
  • It's always better to use -iname instead of -name — then you will also capture image.JPG and image.PnG – ccpizza Nov 11 '17 at 10:48
40

This is more correct:

find . -iregex '.*\.\(jpg\|gif\|png\|jpeg\)$'
  • 10
    Why do you say it is "more" correct? – Kevin Nov 17 '12 at 3:51
  • 2
    @Kevin I guess because -iregex matches jpg as well as JPG Jpg jpG and such. I think the $ isn't needed. – ott-- May 19 '15 at 10:41
  • This works fine on MinGW. – Peter Mortensen Sep 18 '15 at 11:30
  • 2
    First, you omitted the search folder as first argument which will throw an error. Secondly, on OSX escaping the parenthesis will not work, and this should be used instead: find -E . -iregex '.*\.(jpg|png|gif)' as shown in @sorin's answer. – ccpizza Nov 11 '17 at 11:36
8

To make it clear, the only option that works on Linux, Unix and macOS flavour is:

find -E . -regex '.*\.(jpg|png)'

That's because the OS X version is a little bit different, but that's important to write things that go well on most platforms.

  • 2
    -E flag is not valid for find on Ubuntu 14.04 – gogaman Jul 25 '14 at 17:33
  • The -E flag is not valid on MinGW either (at least the version/configuration I tried it on (default configuration for a particular version)). – Peter Mortensen Sep 18 '15 at 11:29
  • Which part of -type f \( -iname \*.png -o -iname \*.jpg\) does not work on OSX? – Shadur Apr 16 '18 at 7:43
-3
/\.(jpe?g|png|gif|bmp)$/i;

Use it.

  • 5
    You may need to explain this a bit. – chaos Jan 15 '16 at 7:33

protected by don_crissti Nov 16 '17 at 12:21

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.