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

后端 未结 5 633
花落未央
花落未央 2021-01-17 08:29

I want to run the command

find some/path -exec program \\{} \\; 

but I want the find command to quit as soon as the command



        
5条回答
  •  时光取名叫无心
    2021-01-17 09:05

    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
    

提交回复
热议问题