How to exit from find -exec if it fails on one of the files How to exit from find -exec if it fails on one of the files bash bash

How to exit from find -exec if it fails on one of the files


I think it is not possible to achieve what you want, only with find -exec.

The closest alternative would be to do pipe find to xargs, like this:

find some/path -print0 | xargs -0 program

or

find some/path -print0 | xargs -0L1 program

This will quit if program terminates with a non-zero exit status

  • the print0 is used so that files with newlines in their names can be handled
  • -0 is necessary when -print0 is used
  • the L1 tells xargs program to execute program with one argument at a time (default is to add all arguments in a single execution of program)

If you only have sane file names, you can simplify like this:

find some/path | xargs program

or

find some/path | xargs -L1 program

Finally, If program takes more than one argument, you can use -i combined with {}. E.g.

find some/path | xargs -i program param1 param2 {} param4


In addition to the other fine answers, GNU find (at least) has a -quit predicate:

find path -other -predicates \( -exec cmd {} \; -o -quit \)

The -quit predicate is certainly non-standard and does not exist in BSD find.


You could pipe the output from find to another subprocess and use while/break:

find some/path | while read fdo    program $f    if [ $? -ne 0 ]    then        break    fidone