2

The man page for gnu find states:

Please note that -a when specified implicitly (for example by two tests appearing without an explicit operator between them) or explicitly has higher precedence than -o. This means that
find . -name afile -o -name bfile -print
will never print afile.

I'm guessing the above expression equates to:

find . -name afile -o -name bfile -a -print

My question is, how does this expression work? and why will afile never be printed?

2 Answers2

3

The -and/-a operators are the logical AND operator. They do not have to be specified since they are implied by the juxtaposition of two expression.

And yes, it won't print afile while you're using find . -name afile -o -name bfile -a -print since AND operator has a higher precedence than OR operator -or/-o. If you want afile to be printed, you have two options:

First Option:

As @malo mentioned, by running find . \( -name afile -o -name bfile \) -a -print you can have the afile printed since parentheses have the highest precedence in Find commands in both GNU and BSD versions.

Second Option:

By not passing -print option. So it'll be find . -name afile -o -name bfile and the reason is, there is nothing with higher precedence than -o.

  • This answer left me confused because there is a major part missing: If there are no "-print" actions, find implicitly surrounds the entire expression with ( ... ) -print (see top of find man page). That is why "find . -name afile -o -name bfile" finds both. But once you add "-print" anywhere, the global default now becomes to not print. That is why afile does not show up even though "-name afile" (and the entire expression) evaluates to true. Another way to fix it is this: "find . -name afile -print -o -name bfile -print". – Curtis Yallop Feb 28 '18 at 19:38
1

You should use parentheses since they have higher precedence than -a:

find . \( -name afile -o -name bfile \) -a -print
0x0C4
  • 365