且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果 find -exec 在其中一个文件上失败,如何退出

更新时间:2023-11-29 23:51:58

我觉得不可能实现你想要的,只有find -exec.

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

最接近的替代方法是将 find 传递到 xargs,如下所示:

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

find some/path -print0 | xargs -0 program

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

如果程序以非零退出状态终止,这将退出

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

  • print0 用于处理名称中带有换行符的文件
  • -0在使用-print0时是必须的
  • L1 告诉 xargs 程序一次使用一个参数执行程序(默认是在一次程序执行中添加所有参数)
  • 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

find some/path | xargs -L1 program

最后,如果程序需要多个参数,您可以使用 -i 结合 {}.例如

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