为什么"xargs -I {}" "wc or ls: {}: open: No such file or directory"出现错误,但"xargs without -I {}"有效?



工作原理:没有-I {}xargs

我发现了一切;。txt";我的文档文件夹中的文件(找到了大约5000个),并在其中使用wc -l

find . -type f -name "*.txt" -print0 | xargs -0 wc -l >| ~/Downloads/xargs_wc.txt

问题:xargs -I {}

我现在使用xargs -I {}来查找相同的文件:

find . -type f -name "*.txt" -print0 | xargs -0 -I {} wc -l {} >| ~/Downloads/xargs_I_wc.txt

我一直得到8行错误输出,对应于~/Downloads/xargs_I_wc.txt:中丢失的8个文件

wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory

调试

我在~/Downloads/xargs_wc.txt~/Downloads/xargs_I_wc.txt之间做了一个比较,以获得实际的文件。

8个文件名干净,文件内容干净,所有文件都可以手动wc -l',我可以从终端open它们,并在它们上运行file,按顺序:

... ASCII text   (wc gives 202 lines for this file)
... ASCII text   (16 lines for this one)
... ASCII text   (16 lines for this)
... ASCII text, with no line terminators   (132 lines each for the rest)
... ASCII text, with no line terminators
... ASCII text, with no line terminators
... ASCII text, with no line terminators
... ASCII text, with no line terminators

前3个文件类似于LICENSE文件,其中第一个文件在自己的目录中,接下来的两个文件彼此相对接近。最后5个是以TSV格式编写的数据文件,它们都相对接近。

我发现这些文件唯一相似的地方是,在它们上执行ls -l,它们都有"@"符号作为最后一个符号,但对于找到的许多文件来说都是如此:

-rw-r--r--@ ...
etc ...

调试-改为执行ls

我使用ls而不是wc:

find . -type f -name "*.txt" -print0 | xargs -0 -I {} ls -l {} >| ~/Downloads/xargs_I_ls.txt

8条类似的错误线得到输出:

ls: {}: No such file or directory
ls: {}: No such file or directory
ls: {}: No such file or directory
ls: {}: No such file or directory
ls: {}: No such file or directory
ls: {}: No such file or directory
ls: {}: No such file or directory
ls: {}: No such file or directory

我不知道xargs -I {}为什么不起作用。

进一步调试-操作有问题的文件

我将其中一个失败文件的目录复制到我的文档文件夹的根目录中。我用xargs -I {} wc -l:做了同样的测试

find . -type f -name "*.txt" -print0 | xargs -0 -I {} wc -l {} >| ~/Downloads/xargs_I_wc_after_copy.txt

输出相同的8条错误线:

wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory

我不知道发生了什么,我应该得到9条错误线。

进一步调试

我在它自己的目录中复制了另一个有问题的文件。我做了同样的测试。

find . -type f -name "*.txt" -print0 | xargs -0 -I {} wc -l {} >| ~/Downloads/xargs_I_wc_after_duplicate.txt

这次我得到9条错误线:

wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory
wc: {}: open: No such file or directory

我对重复的文件进行了许多操作:我运行chmod,清空文件,更改文件内容,重命名文件,重复保存文件,但总是会出现9行错误。

我创建了一个新的空白文件,并将原始违规文件的内容复制到其中,但我没有得到10行错误,我仍然得到9行错误。

如果我把一个有问题的文件移到当前目录之外的一个目录,我会得到8行错误。如果我把文件移回,我会再次得到9行错误。

还有其他的";。txt";文件,但它们不会导致错误。

违规文件的目录似乎没有任何特殊性。

除了删除所有有问题的文件,或者将它们移走,或者在它们的位置创建新的相同内容副本之外,我不知道如何消除它们的错误,也不知道它们出错的原因是什么。

问题

我看似正常而平凡的文件是怎么回事,导致它们在xargs -I {}中失败的?

如何获得更多导致他们失败的信息并找到根本原因?

我相信macOSxargs是基于BSD版本的。

FreeBSD xargs手册页状态(强调矿):

-I replstr

为每个输入行执行实用程序,用整行输入替换实用程序的一个或多个replstr参数(如果未指定-R标志,则为5个)。替换完成后,生成的参数将不会允许超过replsize(如果未指定-S标志,则为255)字节;这是通过将尽可能多的包含replstr的参数连接到实用程序的构造参数来实现的,最多可达replsize字节大小限制不适用于不包含replstr的实用工具的参数,此外,不会对实用工具本身进行替换。

-S replsize

指定-I可以用于替换的空间量(以字节为单位)replsize的默认值为255。

当从find馈送的路径超过此限制时,不会发生替换。

您的某些路径超出了限制。

解决方法是使用-S标志来设置更高的限制。


最大可能路径长度由getconf PATH_MAX /给出。因此,在这种情况下,至少同样大的东西应该是安全的:

find . -type f -name "*.txt" -print0 |
xargs -0 -S 1024 -I {} ls -l {} >| ~/Downloads/xargs_I_ls.txt