Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's what I checked:

find mydir -maxdepth 2 -name .project -or -name .classpath

gives output:



find mydir -maxdepth 2 -name .project -or -name .classpath -exec echo {} \;

gives output:


so only 1 found item is listed, why?

xargs works as expected:

find mydir -maxdepth 2 -name .project -or -name .classpath | xargs -I {} echo {};



Am I missing something about -exec?

share|improve this question
3 comments in one: -o is much more portable than -or, and means the same thing. I assume the -exec echo is for the sake of illustrating -exec only, since -print would be much better otherwise. For safety reasons, avoid piping find to xargs unless you have find -print0 | xargs -0. If you don't have that, use -exec \; or -exec + instead. – jw013 Jul 19 '12 at 15:12
up vote 8 down vote accepted

A find expression is basically a list of predicates (boolean conditions). In almost all boolean contexts, including find expressions, the AND operator takes precedence over OR. If I add some parentheses to illustrate the precedence rules explicitly, your second find expression is logically equivalent to

( -name .project ) OR ( -name .classpath AND -exec echo)

In order to get your desired behavior, you need

find mydir -maxdepth 2 \( -name .project -o -name .classpath \) -exec ... {} \;
share|improve this answer
If I understood you correctly, Does this mean that I can have different -exec's for different conditions? – user7477 Jul 19 '12 at 15:11
@gasan Yes you can – jw013 Jul 19 '12 at 15:12

Your Answer


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