I can get all jpg
images by using:
find . -name "*.jpg"
But how can I add png
files to the results as well?
I can get all jpg
images by using:
find . -name "*.jpg"
But how can I add png
files to the results as well?
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 insensitivetype -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
a * b + c
is different from a * (b + c)
– Shadur-don't-feed-the-AI
Mar 19 '17 at 12:13
a * (b + c)
as a * b + a * c
with:find ./ -type f -iname \*.jpg -o -type f -iname \*.png
– MikeD
Mar 19 '17 at 12:22
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)'
-iname
instead of -name
— then you will also capture image.JPG
and image.PnG
– ccpizza
Nov 11 '17 at 10:48
-iregex
matches jpg
as well as JPG Jpg jpG
and such. I think the $
isn't needed.
– ott--
May 19 '15 at 10:41
find -E . -iregex '.*\.(jpg|png|gif)'
as shown in @sorin's answer.
– ccpizza
Nov 11 '17 at 11:36
-o
operator and it's more correct since you have to use the right tool for the job.
– pouya
Apr 22 '22 at 14:57
-o
isn't stupid; it does exactly what the or
operator is supposed to do. And the "right" tool for the job is whatever gets the job done.
– Shadur-don't-feed-the-AI
Sep 23 '22 at 13:01
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.
-type f \( -iname \*.png -o -iname \*.jpg\)
does not work on OSX?
– Shadur-don't-feed-the-AI
Apr 16 '18 at 07:43
-type f \( -iname \*.png -o -iname \*.jpg\)
does work on OSX
– Steve Vermeulen
Nov 05 '19 at 16:22
If only files are needed:
find ./ -type f -regex '.*\.\(jpg\|png\)$'
For example, this will not match a directory named "blah.jpg".
find -name
pattern that matches multiple patterns at SO – kenorb Apr 13 '15 at 13:46find . | egrep '.jpg|.png'
a lot. – Ulf Gjerdingen Jan 13 '21 at 09:58