When I lập cập a command lượt thích ls */*/*/*/*.jpg, I get the error

-bash: /bin/ls: Argument list too long

I know why this happens: it is because there is a kernel limit on the amount of space for arguments lớn a command. The standard advice is lớn change the command I use, lớn avoid requiring so sánh much space for arguments (e.g., use find and xargs).

What if I don't want lớn change the command? What if I want lớn keep using the same command? How can I make things "just work", without getting this error? What solutions are available?

asked Aug 15, 2012 at 23:08

3

On Linux, the maximum amount of space for command arguments is 1/4th of the amount of available stack space. So, a solution is lớn increase the amount of space available for the stack.

Short version: lập cập something like

ulimit -s 65536

Longer version: The mặc định amount of space available for the stack is something lượt thích 8192 KB. You can see the amount of space available, as follows:

$ ulimit -s
8192

Choose a larger number, and mix the amount of space available for the stack. For instance, if you want lớn try allowing up lớn 65536 KB for the stack, lập cập this:

$ ulimit -s 65536

You may need lớn play around with how large this needs lớn be, using trial-and-error. In many cases, this is a quick-and-dirty solution that will eliminate the need lớn modify the command and work out the syntax of find, xargs, etc. (though I realize there are other benefits lớn doing so).

I believe that this is Linux-specific. I suspect it probably won't help on any other Unix operating system (not tested).

answered Aug 15, 2012 at 23:11

6

Instead of ls */*/*/*/*.jpg, try:

echo */*/*/*/*.jpg | xargs ls

xargs(1) knows, what the maximum number of arguments is on the system, and will break up its standard input lớn đường dây nóng the specified command-line multiple times with no more arguments phàn nàn that limit, whatever it is (you can also mix it lower phàn nàn the OS' maximum using the -n option).

For example, suppose, the limit is 3 arguments and you have five files. In that case xargs will execute ls twice:

  1. ls 1.jpg 2.jpg 3.jpg
  2. ls 4.jpg 5.jpg

Often this is perfectly suitable, but not always -- for example, you can not rely on ls(1) sorting all of the entries for you properly, because each separate ls-invocation will sort only the subset of entries given lớn it by xargs.

Though you can bump the limit as suggested by others, there will still be a limit -- and some day your JPG-collection will outgrow it again. You should prepare your script(s) lớn giảm giá khuyến mãi with an infinite number...

answered Jul 5, 2017 at 14:25

4

This Linux Journal article gives 4 solutions. Only the fourth solution does not involve changing the command:

Method #4 involves manually increasing the number of pages that are allocated within the kernel for command-line arguments. If you look at the include/linux/binfmts.h tệp tin, you will find the following near the top:

/*
 * MAX_ARG_PAGES defines the number of pages allocated for   arguments
 * and envelope for the new program. 32 should suffice, this gives
 * a maximum env+arg of 128kB w/4KB pages!
 */
#define MAX_ARG_PAGES 32

In order lớn increase the amount of memory dedicated lớn the command-line arguments, you simply need lớn provide the MAX_ARG_PAGES value with a higher number. Once this edit is saved, simply recompile, install and reboot into the new kernel as you would bởi normally.

On my own test system I managed lớn solve all my problems by raising this value lớn 64. After extensive testing, I have not experienced a single problem since the switch. This is entirely expected since even with MAX_ARG_PAGES mix lớn 64, the longest possible command line I could produce would only occupy 256KB of system memory--not very much by today's system hardware standards.

The advantages of Method #4 are clear. You are now able lớn simply run the command as you would normally, and it completes successfully. The disadvantages are equally clear. If you raise the amount of memory available lớn the command line beyond the amount of available system memory, you can create a D.O.S. attack on your own system and cause it lớn crash. On multiuser systems in particular, even a small increase can have a significant impact because every user is then allocated the additional memory. Therefore always test extensively in your own environment, as this is the safest way lớn determine if Method #4 is a viable option for you.

I agree that the limitation is seriously annoying.

answered Nov 26, năm ngoái at 22:53

Bash system calls and its direct variants (arguments and environment variables), will yield "Argument list too long", if pool space limit was reached.

I found how lớn avoid this limit, while dealing with special characters in the tệp tin names (e.g. spaces), by using printf with xargs:

printf '%s\0' */*/*/*/*.jpg | xargs -0 ls

answered Jun 22, 2021 at 13:25

4

You must log in lớn answer this question.

Not the answer you're looking for? Browse other questions tagged

.