Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
3  
What have you tried so far? –  Daniel Beck Feb 1 '13 at 12:24
    
find . -type f -name "Mar" -name "Jun" –  alwbtc Feb 1 '13 at 13:25

3 Answers 3

up vote 6 down vote accepted

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
share|improve this answer
    
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 '13 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 '13 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 '13 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 '13 at 21:06

Use find and its regex option:

find -regex '.*\(Mar\|Jun\).*'
share|improve this answer
    
If you use -regextype posix-extended, you won't have to escape the brackets and pipe with a \ . –  evilsoup Feb 1 '13 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*"
share|improve this answer
1  
This returns folders named Jun, and misses files named June or March. –  Daniel Beck Feb 1 '13 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.