[CentOS] Re: ls and rm: "argument list too long"

Sat Oct 18 02:56:12 UTC 2008
Robert Nichols <rnicholsNOSPAM at comcast.net>

Les Mikesell wrote:
> thad wrote:
>
>> it should be:
>>
>> for i in `ls  /var/amavis/tmp`
>> do
>> rm $i
>> done
> 
> These shouldn't make any difference.  The limit is on the size of the 
> expanded shell command line.

Really?

$ M=0; N=0; for W in `find /usr -xdev 2>/dev/null`; do M=$(($M+1)); N=$(($N+${#W}+1)); done; echo $M $N
156304 7677373

vs.

$ /bin/echo `find /usr -xdev 2>/dev/null`
bash: /bin/echo: Argument list too long

For the first case, the shell never tries to pass the list as command arguments.
It builds the list internally, limited only by memory size, and processes the
words one by one.  As a final test case, by using the shell's builtin 'echo'
the whole 7-plus megabytes gets echoed to the terminal:

$ echo `find /usr -xdev 2>/dev/null`
(no errors -- just lots of output)

Anyway, the "for i in `ls ...`" solution breaks for paths that include
embedded white space.

-- 
Bob Nichols     "NOSPAM" is really part of my email address.
                 Do NOT delete it.