如果 find -exec 在其中一个文件上失败,如何退出 [英] How to exit from find -exec if it fails on one of the files

查看:18
本文介绍了如果 find -exec 在其中一个文件上失败,如何退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行命令

find some/path -exec program {} ; 

但我希望 find 命令在命令发出后立即退出

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

 program {}

在找到的任何文件上失败.

fails on any of the files found.

有简单的方法吗?

推荐答案

我觉得不可能实现你想要的,只有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

这篇关于如果 find -exec 在其中一个文件上失败,如何退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆