4

Here's what I checked:

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

gives output:

mydir/.project
mydir/.classpath

Meanwhile

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

gives output:

mydir/.classpath

so only 1 found item is listed, why?

xargs works as expected:

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

prints:

mydir/.project
mydir/.classpath

Am I missing something about -exec?

  • 3
    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
8

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 ... {} \;
  • 2
    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
  • 4
    @gasan Yes you can – jw013 Jul 19 '12 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy