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

Fri Oct 17 10:18:22 UTC 2008
Paul Bijnens <Paul.Bijnens at xplanation.com>

On 2008-10-17 11:30, Jussi Hirvi wrote:
> Since when is there a limit in how long directory listings CentOS can show
> (ls), or how large directories can be removed (rm). It is really annoying to
> say, for example
> 
>     rm -rf /var/amavis/tmp
> 
> and get only "argument list too long" as feedback.
> 
> Is there a way to go round this problem?
> 
> I have CentOS 5.2. 

I believe you gave a bad example!
In the command

    rm -rf /var/amavis/tmp

the argument list is not at all "very long".
However if you did:

    rm -rf /var/amavis/tmp/*

then the argument list could be very long depending on the number of
entries there are in the subdirectory.

You have to understand that globbing is done by the shell before
starting the command.  The result of the glob is what makes the
"argument list too long"; it needs to fit in a buffer (about
128K bytes on a default CentOS install I believe).

If you want the to remove the subfolders and files, and not the parent
folder itself, on CentOS 5, try this:

    cd /var/amavis/tmp
    rm -rf *

Doing a "cd" makes the resulting of the globbing much shorter, maybe
fixing your problem already.

If still too long, then try:

    find . -maxdepth 1 -exec rm -rf {} +

You could even try things like:

    cd /var/amavis/tmp
    rm a*
    rm *0
    ...etc...

Any glob pattern resulting in less arguments for the command to avoid
overflowing the 128K buffer is good.

On CentOS 4, the '+' variant of -exec does not exist, and you need to do:

    find . -maxdepth 1 -exec rm -rf {} \;	 # one rm command for each arg
  or
    find . -maxdepth 1 -print0 | xargs -0 rm -rf  # less resource intensive

-- 
Paul Bijnens, xplanation Technology Services        Tel  +32 16 397.511
Technologielaan 21 bus 2, B-3001 Leuven, BELGIUM    Fax  +32 16 397.512
http://www.xplanation.com/          email:  Paul.Bijnens at xplanation.com
***********************************************************************
* I think I've got the hang of it now:  exit, ^D, ^C, ^\, ^Z, ^Q, ^^, *
* F6, quit, ZZ, :q, :q!, M-Z, ^X^C, logoff, logout, close, bye, /bye, *
* stop, end, F3, ~., ^]c, +++ ATH, disconnect, halt,  abort,  hangup, *
* PF4, F20, ^X^X, :D::D, KJOB, F14-f-e, F8-e,  kill -1 $$,  shutdown, *
* init 0, kill -9 1, Alt-F4, Ctrl-Alt-Del, AltGr-NumLock, Stop-A, ... *
* ...  "Are you sure?"  ...   YES   ...   Phew ...   I'm out          *
***********************************************************************