bash - 连接 find -print0 和 xargs -0

标签 bash find xargs

find 参数 -print0xargs 参数 -0 应该一起工作。 findman 页面说:

-print0 .... This option corresponds to the -0 option of xargs.

好吧,只要 find 产生一些输出,它们就对我有用。当 find 不产生任何输出时,如何让它工作?

find /dev /sys /usr -maxdepth 0 -print0 | xargs -0 -n1 | wc -l    # 3 - OK
find /dev /sys -maxdepth 0 -print0 | xargs -0 -n1 | wc -l         # 2 - OK
find /dev -maxdepth 0 -print0 | xargs -0 -n1 | wc -l              # 1 - OK
find /dev -maxdepth 0 -name "x" -print0 | xargs -0 -n1 | wc -l    # 1 instead of 0 - Fail

可能会发生一些不好的事情,例如,

find -type d -name ... -print0 | xargs -0 du -sh

当没有找到目录时,xargs 调用不带参数的 du 并且 du 假定

附言我知道 find-exec 参数,我只想正确连接 findxargs

最佳答案

GNU xargs 具有以下选项:

  --no-run-if-empty
   -r     

If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input.

关于bash - 连接 find -print0 和 xargs -0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596476/

相关文章:

Linux 查找带有符号链接(symbolic link)递归和一些额外功能

json - 如何使用 Bash 在单引号内回显变量?

bash - Makefile 错误 - 寻找匹配 `"时意外的 EOF '

Ruby Selenium - find_elements if nil => Net::ReadTimeout

linux - "find"命令无法检测到执行期间添加的文件

linux - 列出包含字符串但 *NOT* 包含另一个字符串的文件的 Unix 命令

bash - docker exec 在 cron 中不工作

linux - 为什么 cat 会改变二进制文件的内容?

bash - 手动指定输入时,如何对 xargs 使用 -0 选项?

linux - CIS 文档中的 Bash 语法错误