Linux Bash 参数的最大数量!= max num cp 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4185017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:58:38  来源:igfitidea点击:

Maximum number of Bash arguments != max num cp arguments?

linuxbashcommand-line-arguments

提问by Lee Netherton

I have recently been copying and moving a large number of files (~400,000). I know that there are limitations on the number of arguments that can be expanded on the Bash command line, so I have been using xargs to limit the numbers produced.

我最近一直在复制和移动大量文件(~400,000)。我知道可以在 Bash 命令行上扩展的参数数量有限制,所以我一直使用 xargs 来限制产生的数量。

Out of curiosity, I wondered what the maximum number of arguments that I could use was, and I found this postsaying that it was system-dependant, and that I could run this command to find out:

出于好奇,我想知道我可以使用的最大参数数量是多少,我发现这篇文章说它依赖于系统,我可以运行这个命令来找出:

$ getconf ARG_MAX

To my surprise, the anwser I got back was:

令我惊讶的是,我得到的答案是:

2621440

Just over 2.6 million. As I said, the number of files that I am manipulating is much less than this -- around 400k. I definitely need to use the xargsmethod of moving and copying these files, because I tried using a normal mv * ...or cp * ...and got a 'Argument list too long' error.

刚刚超过260万。正如我所说,我正在处理的文件数量远少于此 - 大约 400k。我肯定需要使用xargs移动和复制这些文件的方法,因为我尝试使用普通mv * ...cp * ...并得到“参数列表太长”错误。

So, do the mvand cpcommands have their own fixed limit on the number of arguments that I can use (I couldn't find anything in their man pages), or am I missing something?

那么,mvandcp命令对我可以使用的参数数量是否有自己的固定限制(我在他们的手册页中找不到任何内容),还是我遗漏了什么?

采纳答案by Wyatt Anderson

As Ignacio said, ARG_MAXis the maximum length of the bufferof arguments passed to exec(), not the maximum number of files (this pagehas a very in-depth explanation). Specifically, it lists fs/exec.cas checking the following condition:

正如 Ignacio 所说,ARG_MAX是传递给 的参数缓冲区的最大长度exec(),而不是最大文件数(此页面有非常深入的解释)。具体来说,它列出fs/exec.c了检查以下条件:

PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *) / sizeof(void *)

And, it seems, you have some additional limitations:

而且,您似乎还有一些额外的限制:

On a 32-bit Linux, this is ARGMAX/4-1 (32767). This becomes relevant if the average length of arguments is smaller than 4. Since Linux 2.6.23, this function tests if the number exceeds MAX_ARG_STRINGSin <linux/binfmts.h>(2^32-1 = 4294967296-1). And as additional limit, one argument must not be longer than MAX_ARG_STRLEN(131072).

在 32 位 Linux 上,这是ARGMAX/4-1 (32767)。这将成为相关如果参数的平均长度小于4.由于Linux 2.6.23,该功能检查,如果数量超过MAX_ARG_STRINGS<linux/binfmts.h>(2 ^ 32-1 = 4294967296-1)。作为附加限制,一个参数不得长于MAX_ARG_STRLEN(131072)。

回答by Ignacio Vazquez-Abrams

ARG_MAXis the maximum length of the arguments to the exec(3)functions. A shell is not required to support passing this length of arguments from its command line.

ARG_MAXexec(3)函数参数的最大长度。shell 不需要支持从它的命令行传递这个长度的参数。