Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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
add comment

1 Answer 1

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
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
add comment

Your Answer

 
discard

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