Argument list too long

Steven W. Orr steveo at syslang.net
Fri Aug 3 14:58:25 UTC 2007


On Wednesday, Aug 1st 2007 at 14:03 -0600, quoth Phil Meyer:

=>Ashley M. Kirchner wrote:
=>> 
=>>    Question: at what point would commands such as 'ls', 'cp' and others
=>> consider the argument list too long?  The reason I'm asking is because I'm
=>> getting random results here.  One time 'ls' would tell it the argument list
=>> is too long when the file count in the folder reached around 2,000 files.
=>> Another, like this morning, it let me go all the way to 2,600+ before
=>> complaining.  And just now it reached 1,900 files and already 'ls' is
=>> complaining.  That seems random to me.  Is there a set number that causes
=>> all of these commands to fail with 'Argument list too long' or what
=>> convoluted algorithm is used to figure it out?
=>> 
=>
=>It is based upon a max string length, not number of files.  That is the
=>purpose for the command xargs.
=>
=>Whenever you are likely to exceed the maximum command line, put xargs in the
=>middle, ie:
=>
=>$ find /usr/include -name \*.h -print | xargs grep SIGKILL
=>
=>Does that make sense?
=>
=>xargs knows what the max string length is for the system its on, and cuts the
=>command line into pieces accordingly and feeds it in pieces to the arguments
=>given.

Use xargs as described above. Learn it. Use it. Love it.

One more detail for the completely anal: The value of ARG_MAX in limits.h 
is the total bytecount of a commandline *minus* the size of your 
environment. i.e., ARG_MAX - $(wc -c $(env))

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net




More information about the fedora-list mailing list