"Argument list too long" error for `rm -rf *` on a directory with 4000 files

9,597

Solution 1

This is not an issue but a limit. You can use something like this:

find ./ -exec rm -rf {} \;

Or what is the matter of not using an alternative that does the job?

Solution 2

This is still a problem on all Unixes I know of, as well as Windows. It's really a limit on the number of bytes being passed on the command line, not the number of files or whatever.

Try getconf ARG_MAX to see the limit (in bytes) for your Unix. You can use the xargs command to work around such problems.

Solution 3

While I can't speak for other *nixs, AFAIK, this "issue" has always existed in Mac OS X.

ARG_MAX is defined as the following in /usr/include/sys/syslimits.h:

#define ARG_MAX   (256 * 1024)  /* max bytes for an exec function */

sysctl kern.argmax returns:

kern.argmax: 262144

(This is in Mac OS X 10.7.3; many of these types of limits have been increased gradually over the course of the lifetime of OS X).

Share:
9,597
timpone
Author by

timpone

Updated on September 18, 2022

Comments

  • timpone
    timpone over 1 year

    I thought this issue was fixed in Linux like 10 years ago. 4000 files really doesn't seem too excessive and should be able to be removed no problem. So this issue clearly exists in OS X - maybe not in Ubuntu. I'm developing on OS X and deploying to Ubuntu

    Is there a system level workaround in either environment? I really don't want to have to think about this issue for such a small number of files? Does this issue still exist in Linux, specifically Ubuntu?

  • timpone
    timpone about 12 years
    xargs tends to be SLOW - just more surprised this is still an issue than anything. such a common procedure. ugh...
  • afrazier
    afrazier about 12 years
    This particular problem doesn't pop up on Windows all that often, because the shell doesn't expand wildcards, it's up to each app to do that.
  • timpone
    timpone about 12 years
    hmm.... maybe just alias rm -rf to that?
  • timpone
    timpone about 12 years
    thx, helpful to know
  • phuclv
    phuclv almost 6 years
    The same limit exists on Linux because it's defined in POSIX standard. grep ARG_MAX /usr/include/linux/limits.h on my Ubuntu returns 131072. What defines the maximum size for a command single argument?, What is the maximum length of command line arguments in gnome-terminal?