1

I'd like to find files whose name contain "Mar" or "Jun", how can I do this?

1
  • find . -type f -name "Mar" -name "Jun"
    – alwbtc
    Feb 1, 2013 at 13:25

4 Answers 4

6

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
4
  • 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?
    – alwbtc
    Feb 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?
    – alwbtc
    Feb 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 using find -exec and the grep program.
    – Daniel Beck
    Feb 1, 2013 at 21:06
3

Use find and its regex option:

find -regex '.*\(Mar\|Jun\).*'
1
  • 1
    If you use -regextype posix-extended, you won't have to escape the brackets and pipe with a \ .
    – evilsoup
    Feb 1, 2013 at 12:49
1
ls *{Jun,Mar}*

This lists all the filenames that contain Jun or Mar

0

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*"
1
  • 1
    This returns folders named Jun, and misses files named June or March.
    – Daniel Beck
    Feb 1, 2013 at 13:34

You must log in to answer this question.

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