Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Out of curiosity, I wondered what the maximum number of arguments that I could use was, and I found this post saying 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 xargs method of moving and copying these files, because I tried using a normal mv * ... or cp * ... and got a 'Argument list too long' error.

So, do the mv and cp commands 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?

like image 872
Lee Netherton Avatar asked Nov 15 '10 13:11

Lee Netherton


People also ask

How many arguments can be passed to bash script?

NUM} ) in the 2^32 range, so there is a hard limit somewhere (might vary on your machine), but Bash is so slow once you get above 2^20 arguments, that you will hit a performance limit well before you hit a hard limit.

What is the maximum number of arguments that you can pass to a script?

But tried on posix and bash shell , and the maxmium number of arguments you can pass to a script is 9. If you want to pass more parametrs , you need to use the shift function. The intresting thing is , more than 9 parameters works fine if numbers are given , but gives unexpected output when tried with letters.

What is the maximum number of lines allowed in a shell script?

The shell/OS imposed limit is usually one or two hundred thousand characters. getconf ARG_MAX will give you the maximum input limit for a command. On the Debian system I currently have a terminal open on this returns 131072 which is 128*1024 .

What is $# in bash?

$# is a special variable in bash , that expands to the number of arguments (positional parameters) i.e. $1, $2 ... passed to the script in question or the shell in case of argument directly passed to the shell e.g. in bash -c '...' .... .


1 Answers

As Ignacio said, ARG_MAX is the maximum length of the buffer of arguments passed to exec(), not the maximum number of files (this page has a very in-depth explanation). Specifically, it lists fs/exec.c as checking the following condition:

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_STRINGS in <linux/binfmts.h> (2^32-1 = 4294967296-1). And as additional limit, one argument must not be longer than MAX_ARG_STRLEN (131072).

like image 144
Wyatt Anderson Avatar answered Oct 07 '22 06:10

Wyatt Anderson