291

I can get all jpg images by using:

find . -name "*.jpg"  

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

2

5 Answers 5

351

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)
  • \( & \) - are needed for the -type f to apply to all arguments
  • -o - logical OR operator
  • -iname - like -name, but the match is case insensitive
5
  • 2
    Do you need the parentheses. The command works for me without them. Are they needed for some shells?
    – MikeD
    Commented Mar 19, 2017 at 2:25
  • 4
    @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. Commented Mar 19, 2017 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
    Commented Mar 19, 2017 at 11:58
  • 7
    It's a matter of operator precedence. Just like a * b + c is different from a * (b + c) Commented Mar 19, 2017 at 12:13
  • 1
    Thanks, and I have rewritten a * (b + c) as a * b + a * c with: find ./ -type f -iname \*.jpg -o -type f -iname \*.png
    – MikeD
    Commented Mar 19, 2017 at 12:22
153

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 \( -name '*.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)'
2
  • 9
    It's always better to use -iname instead of -name — then you will also capture image.JPG and image.PnG
    – ccpizza
    Commented Nov 11, 2017 at 10:48
  • 1
    You can use -iregex instead of -regex to ignore case
    – Jotschi
    Commented Aug 4, 2023 at 3:49
64

This is more correct:

find . -iregex '.*\.\(jpg\|gif\|png\|jpeg\)$'
7
  • 17
    Why do you say it is "more" correct?
    – Kevin
    Commented Nov 17, 2012 at 3:51
  • 5
    @Kevin I guess because -iregex matches jpg as well as JPG Jpg jpG and such. I think the $ isn't needed.
    – ott--
    Commented May 19, 2015 at 10:41
  • 1
    This works fine on MinGW. Commented Sep 18, 2015 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
    Commented Nov 11, 2017 at 11:36
  • Much better than the stupid -o operator and it's more correct since you have to use the right tool for the job.
    – pouya
    Commented Apr 22, 2022 at 14:57
22

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.

4
  • 7
    -E flag is not valid for find on Ubuntu 14.04
    – gogaman
    Commented Jul 25, 2014 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)). Commented Sep 18, 2015 at 11:29
  • 1
    Which part of -type f \( -iname \*.png -o -iname \*.jpg\) does not work on OSX? Commented Apr 16, 2018 at 7:43
  • 1
    Can confirm -type f \( -iname \*.png -o -iname \*.jpg\) does work on OSX Commented Nov 5, 2019 at 16:22
4

If only files are needed:

find ./ -type f -regex '.*\.\(jpg\|png\)$'

For example, this will not match a directory named "blah.jpg".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .