How to use find command to search for multiple extensions
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I can get all jpg
images by using:
find . -name "*.jpg"
But how can I add png
files to the results as well?
find regular-expression
add a comment |
I can get all jpg
images by using:
find . -name "*.jpg"
But how can I add png
files to the results as well?
find regular-expression
Related:find -name
pattern that matches multiple patterns at SO
– kenorb
Apr 13 '15 at 13:46
add a comment |
I can get all jpg
images by using:
find . -name "*.jpg"
But how can I add png
files to the results as well?
find regular-expression
I can get all jpg
images by using:
find . -name "*.jpg"
But how can I add png
files to the results as well?
find regular-expression
find regular-expression
edited Jun 20 '11 at 18:51
Michael Mrozek♦
62.3k29194214
62.3k29194214
asked Jun 20 '11 at 15:06
wong2wong2
848398
848398
Related:find -name
pattern that matches multiple patterns at SO
– kenorb
Apr 13 '15 at 13:46
add a comment |
Related:find -name
pattern that matches multiple patterns at SO
– kenorb
Apr 13 '15 at 13:46
Related:
find -name
pattern that matches multiple patterns at SO– kenorb
Apr 13 '15 at 13:46
Related:
find -name
pattern that matches multiple patterns at SO– kenorb
Apr 13 '15 at 13:46
add a comment |
5 Answers
5
active
oldest
votes
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 thetype -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! Thetype -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
1
It's a matter of operator precedence. Just likea * b + c
is different froma * (b + c)
– Shadur
Mar 19 '17 at 12:13
2
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
|
show 2 more comments
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 captureimage.JPG
andimage.PnG
– ccpizza
Nov 11 '17 at 10:48
add a comment |
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
1
@Kevin I guess because-iregex
matchesjpg
as well asJPG 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
add a comment |
To make it clear, the only optio that works on Linux, Unix and OS X 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
add a comment |
/.(jpe?g|png|gif|bmp)$/i;
Use it.
5
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
add a comment |
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?
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 thetype -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! Thetype -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
1
It's a matter of operator precedence. Just likea * b + c
is different froma * (b + c)
– Shadur
Mar 19 '17 at 12:13
2
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
|
show 2 more comments
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 thetype -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! Thetype -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
1
It's a matter of operator precedence. Just likea * b + c
is different froma * (b + c)
– Shadur
Mar 19 '17 at 12:13
2
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
|
show 2 more comments
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 thetype -f
to apply to all arguments
-o
- logical OR operator
-iname
- like-name
, but the match is case insensitive
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 thetype -f
to apply to all arguments
-o
- logical OR operator
-iname
- like-name
, but the match is case insensitive
edited Oct 24 '18 at 8:34
answered Jun 20 '11 at 15:11
ShadurShadur
20.1k84658
20.1k84658
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! Thetype -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
1
It's a matter of operator precedence. Just likea * b + c
is different froma * (b + c)
– Shadur
Mar 19 '17 at 12:13
2
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
|
show 2 more comments
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! Thetype -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
1
It's a matter of operator precedence. Just likea * b + c
is different froma * (b + c)
– Shadur
Mar 19 '17 at 12:13
2
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
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
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
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
@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
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
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
1
1
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
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
2
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
@jdhao Good catch, amended.
– Shadur
Oct 24 '18 at 8:34
|
show 2 more comments
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 captureimage.JPG
andimage.PnG
– ccpizza
Nov 11 '17 at 10:48
add a comment |
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 captureimage.JPG
andimage.PnG
– ccpizza
Nov 11 '17 at 10:48
add a comment |
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)'
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)'
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Jun 20 '11 at 21:29
GillesGilles
546k12911091623
546k12911091623
It's always better to use-iname
instead of-name
— then you will also captureimage.JPG
andimage.PnG
– ccpizza
Nov 11 '17 at 10:48
add a comment |
It's always better to use-iname
instead of-name
— then you will also captureimage.JPG
andimage.PnG
– ccpizza
Nov 11 '17 at 10:48
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
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
add a comment |
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
1
@Kevin I guess because-iregex
matchesjpg
as well asJPG 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
add a comment |
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
1
@Kevin I guess because-iregex
matchesjpg
as well asJPG 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
add a comment |
This is more correct:
find . -iregex '.*.(jpg|gif|png|jpeg)$'
This is more correct:
find . -iregex '.*.(jpg|gif|png|jpeg)$'
edited 18 mins ago
Community♦
1
1
answered Nov 17 '12 at 3:20
DimitryDimitry
37932
37932
10
Why do you say it is "more" correct?
– Kevin
Nov 17 '12 at 3:51
1
@Kevin I guess because-iregex
matchesjpg
as well asJPG 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
add a comment |
10
Why do you say it is "more" correct?
– Kevin
Nov 17 '12 at 3:51
1
@Kevin I guess because-iregex
matchesjpg
as well asJPG 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
10
10
Why do you say it is "more" correct?
– Kevin
Nov 17 '12 at 3:51
Why do you say it is "more" correct?
– Kevin
Nov 17 '12 at 3:51
1
1
@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
@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
This works fine on MinGW.
– Peter Mortensen
Sep 18 '15 at 11:30
2
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
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
add a comment |
To make it clear, the only optio that works on Linux, Unix and OS X 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
add a comment |
To make it clear, the only optio that works on Linux, Unix and OS X 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
add a comment |
To make it clear, the only optio that works on Linux, Unix and OS X 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.
To make it clear, the only optio that works on Linux, Unix and OS X 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.
answered Jul 21 '13 at 14:08
sorinsorin
5182714
5182714
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
add a comment |
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
2
2
-E flag is not valid for find on Ubuntu 14.04
– gogaman
Jul 25 '14 at 17:33
-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
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
Which part of
-type f ( -iname *.png -o -iname *.jpg)
does not work on OSX?– Shadur
Apr 16 '18 at 7:43
add a comment |
/.(jpe?g|png|gif|bmp)$/i;
Use it.
5
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
add a comment |
/.(jpe?g|png|gif|bmp)$/i;
Use it.
5
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
add a comment |
/.(jpe?g|png|gif|bmp)$/i;
Use it.
/.(jpe?g|png|gif|bmp)$/i;
Use it.
edited Jan 15 '16 at 7:32
chaos
36k977120
36k977120
answered Jan 15 '16 at 7:00
rohit gourrohit gour
1
1
5
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
add a comment |
5
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
5
5
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
You may need to explain this a bit.
– chaos
Jan 15 '16 at 7:33
add a comment |
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?
Related:
find -name
pattern that matches multiple patterns at SO– kenorb
Apr 13 '15 at 13:46