find
参数 -print0
和 xargs
参数 -0
应该一起工作。 find
的 man
页面说:
-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
参数,我只想正确连接 find
和 xargs
。
最佳答案
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/