I'd like to find files whose name contain "Mar" or "Jun", how can I do this?
4 Answers
Your first approach (find . -type f -name "Mar" -name "Jun"
) had two problems:
find
combines different rules using a logical "and" by default- You did an exact name match
So the only possible matches were files that were named exactly "Jun" and "Mar", i.e. none.
Instead, do the following:
- You can use wildcards in the name search (
*
means any possibly empty sequence of characters), wrapped in quotation marks to prevent interpretation by the shell - combine the two rules with an
-o
(or) — this requires you two wrap the alternative rules in parentheses to override usual operator precedence, or to repeat the common-type f
rule
Example:
$ ls -1
1st-of-June
January
March
$ find . -type f -name '*Mar*' -o -type f -name '*Jun*'
./1st-of-June
./March
-
Thanks, how about this: How can I find files that contains 2 strings together, the strings may or may not be on the same line. And what if I want to find files which satisfy this condition and also created after a specific date?– alwbtcFeb 1, 2013 at 14:41
-
@alwbtc Please clarify, your original question stated you were looking for files with a specific name? Was that not correct, or are you now asking something totally different?– Daniel Beck ♦Feb 1, 2013 at 14:59
-
Yes, that was correct, and you answered it. Now I would like to know how to do this other search? Could you please?– alwbtcFeb 1, 2013 at 20:43
-
@alwbtc How about you try it yourself first? The date restriction is really easy with what you learned from the answers to this question and a tiny bit of reading
man find
. The other part can be solved usingfind -exec
and thegrep
program.– Daniel Beck ♦Feb 1, 2013 at 21:06
Use find
and its regex
option:
find -regex '.*\(Mar\|Jun\).*'
-
1If you use
-regextype posix-extended
, you won't have to escape the brackets and pipe with a\
.– evilsoupFeb 1, 2013 at 12:49
This command will work
[max@localhost ~]$ find / -type f -name Mar -o -name Jun ./Mar ./Jun
UPDATE
Then this is the answer
find / -type f -name "*Mar*" -o -type f -name "*Jun*"
-
1This returns folders named
Jun
, and misses files namedJune
orMarch
.– Daniel Beck ♦Feb 1, 2013 at 13:34