5

The find man page says:

-execdir command {} + always returns true, while -execdir command {} ; returns true only if command returns 0.

and

EXIT STATUS find exits with status 0 if all files are processed successfully, greater than 0 if errors occur.

But if I test this, I get the opposite behaviour:

 $ find . -execdir false '{}' \; ; echo $?      
 0

 $ find . -execdir false '{}' \+ ; echo $?      
 1

Can someone explain this?

With -exec instead of -execdir I get the same results. I tried find (GNU findutils) 4.6.0 and 4.4.2.

6

This "return" isn't the exit code of find, but the return value of the -execdir action for the purposes of chaining multiple find actions together. If you try:

find . -execdir false '{}' \; -print

then -print never executes (that is, there is no output), while both of:

find . -execdir true '{}' \; -print
find . -execdir false '{}' + -print

print every path.


However, when using a + the exit code of find is set: it is non-zero if any invocation of the command exits non-zero, while for a ; the exit code is unaffected (directly) by the command results. This behaviour is required by POSIX for -exec, I suppose to indicate failures that would otherwise be undetectable:

If the primary expression is punctuated by a <plus-sign>, the primary shall always evaluate as true, and the pathnames for which the primary is evaluated shall be aggregated into sets. [...] If any invocation returns a non-zero value as exit status, the find utility shall return a non-zero exit status.

-exec ... \; is not specified as affecting the exit code, so find's default behaviour of exiting zero unless there was an error applies. An -exec ... \; command exiting non-zero is not regarded as an error, so in the absence of some other error the exit code will always be zero.

  • Thanks! The exit status 0 if "All path operands were traversed successfully." of the POSIX is also more precise than the GNU man page ("0 if all files processed successfully"). – jofel Apr 26 '16 at 10:09

Your Answer

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

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