Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

Im using a script to delete old file in a directory, but the script kept showing

/usr/bin : Argument list too long

It turned out the number of files were 40000 and more, So I want to know what is the maximum limit of files that can be listed. Is there anyway to find it? Is it system specific?

share|improve this question

marked as duplicate by Stéphane Chazelas, vonbrand, jasonwryan, Anthon, slm Mar 5 at 20:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Read up on find(1) and perhaps xargs(1) –  vonbrand Mar 5 at 20:12
add comment

4 Answers 4

up vote 9 down vote accepted

This message is not from ls, but from exec. you can see the maximum size of the argument list by running getconf ARG_MAX.

It's not how many files that can be handled by the application, but the size of the arguments that can be passed using exec to the operating system, which returns E2BIG if the size of the arguments is beyond the acceptable range.

This limit was traditionally (until Linux 2.6.23) conferred using the kernel define ARG_MAX, found in sys/linux/limits.h. However, nowadays this is specific to the environment you are running on. Typically, the maximum length of the arguments can now be as big as a quarter of the userspace stack size.

share|improve this answer
    
What? ARG_MAX is no longer relevant? Really? Could you expand on that a little? Which environment do you mean? The shell? Where is the limit set now? –  terdon Mar 5 at 14:34
    
@terdon Where did I say it was no longer relevant? The limit is still enforced by the kernel, it's just not hardcoded (it's dynamic based upon the size of the stack) -- see man 2 execve and exec.c, especially the RLIMIT_STACK checks. –  Chris Down Mar 5 at 14:40
    
Oh, I read your answer to mean that ARG_MAX is no longer used. What do you mean by "is specific to the environment you are running on" then? Note that I'm not saying you're mistaken, I'm sure you're not, I'm just wondering about the details. –  terdon Mar 5 at 14:43
    
@terdon The size of the userspace stack varies depending on the environment (and user settings). Since the size of ARG_MAX is typically one quarter of the userspace stack size, it varies depending on the size of the userspace stack allocated (which may differ from computer to computer, or it may also be changed with ulimit -s in bash). –  Chris Down Mar 6 at 1:37
    
Ah, OK, so that's why I have 131072 in my /usr/include/linux/limits.h (Debian) and getconf ARG_MAX returns 2097152 which is 16*131072. Thanks! –  terdon Mar 6 at 1:42
add comment

The limit varies between operating systems and versions. The limit is not number of files, but bytes. You can (usually) get your local limit like this:

$ getconf ARG_MAX
2097152

See also: BASH FAQ 095

share|improve this answer
add comment

The situation is not the same if you do

ls /usr/bin

or

ls /usr/bin/*

Because in the second case, bash glob returns a list that gets passed to ls and this is very limited (which is among reasons why xargs exists). I even think shell's limit is lower than exec limit.

share|improve this answer
add comment

I ran into this situation when working with 4.2 BSD long ago. Under the covers, the shell has a memory space in the environment to use to pass command line arguments to invoked programs. As has been mentioned, it varies from Unix to Unix, but is often a tunable parameter in the OS.

Expanding this space on the system bumps up the memory requirement for the whole system, so that may not be optimal for you. What would be useful to explore for your purpose is the use of the -exec option in a 'find' syntax, as in this syntax to recursively find and delete files (type f) in the current directory (and below) which are older than 30 days (-mtime +30):

find . -type f -mtime +30 -exec rm {} \;

Its very powerful, and find itself is doing the removal, so there is no limit on the number of files removed. If your removal needs to match files of certain name patterns, you can generate that with find as well.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.