简体   繁体   English

将查找输出作为参数传递时出现“参数列表太长”错误

[英]"Argument list too long" error when passing a find output as arguments

I have some 5 million text files under a directory - all of the same format (nothing special, just plain text files with some integers in each line).我在一个目录下有大约 500 万个文本文件 - 都是相同的格式(没什么特别的,只是每行一些整数的纯文本文件)。 I would like to compute the maximum and minimum line count amongst all these files, along with its two filenames (the one for max and another for min).我想计算所有这些文件中的最大和最小行数,以及它的两个文件名(一个是最大值,另一个是最小值)。

I started out by trying to write out all the line count like so (and then workout how to find the min and max from this list):我开始尝试像这样写出所有的行数(然后练习如何从这个列表中找到最小值和最大值):

wc -l `find /some/data/dir/with/text/files/ -type f` > report.txt

but this throws me an error:但这给我带来了一个错误:

bash: /usr/bin/wc: Argument list too long bash: /usr/bin/wc: 参数列表太长

Perhaps there is a better way to go about this?也许有更好的方法来解决这个问题?

There is a limit to the argument list length.参数列表长度有限制 Since you have several millions files passed to wc, the command certainly crossed this line.由于您有数百万个文件传递给 wc,因此该命令肯定跨越了这条线。

Better invoke find -exec COMMAND instead:更好地调用find -exec COMMAND

find /some/data/dir/with/text/files/ -type f -exec wc -l {} + > report.txt

Here, each found file find will be appended to the argument list of the command following -exec in place of {} .在这里,每个找到的文件find都将附加到-exec之后的命令的参数列表中,而不是{} Before the argument length is reached, the command is run and the remaining found files will be processed in a new run of the command the same way, until the whole list is done.在达到参数长度之前,将运行该命令,并将以相同的方式在新的命令运行中处理剩余的找到的文件,直到完成整个列表。

See man page of find for more details.有关更多详细信息,请参阅find手册页


Thanks to Charles Duffy for the improvements of this answer.感谢Charles Duffy对这个答案的改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM