从 find 到 xargs 或使用 find -exec + 管道传输的文件长度是否有限制

sim*_*ser 4 linux shell find

在这个站点上的问题[查找并删除linux中名为“test”的所有目录]中,最佳答案是关于使用这两个命令的:

find . -name test -type d -print0|xargs -0 rm -r --
find . -name test -type d -exec rm -r {} +
Run Code Online (Sandbox Code Playgroud)

因为他们将使用目录列表调用 rm而不是单独多次调用它。

由于声誉低,我无法在那里发表评论,因此我在这里提出一个新问题:

使用这些技术可以传递给 rm 的文件数量是否有任何限制(除了实际的系统资源限制)?

在 shell 中,像 'rm *' 这样的命令可以超过 shell 的最大命令行长度,但是这样的限制是否适用于 find + 的这种用法或通过管道到 xargs?

mta*_*tak 5

简而言之,没有。

长答案: - Find 将为每个匹配运行 exec 指定的命令,因此如果您的 find 出现 20 个文件,它将运行 20 个单独的 rm 实例。- xargs 将确定您的 shell 的最大命令长度并在这些限制内添加参数,正如您在输出中看到的那样xargs --show-limits mtak@frisbee:~$ xargs --show-limits Your environment variables take up 4050 bytes POSIX upper limit on argument length (this system): 2091054 POSIX smallest allowable upper limit on argument length (all systems): 4096 Maximum length of command we could actually use: 2087004 Size of command buffer we are actually using: 131072