"Argument list too long" error for `rm -rf *` on a directory with 4000 files
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).
timpone
Updated on September 18, 2022Comments
-
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?
-
Wuffers about 12 yearsWhat are you asking about? Mac OS X or Ubuntu?
-
timpone about 12 yearsuhh... i think the title is pretty clear
-
soandos about 12 yearsAnd then you went and muddled it with the last question
-
HikeMike about 12 yearsPlease revise your question. It is not clear what system you're asking about, whether you're asking about the cause or for a workaround.
-
Wuffers about 12 years@timpone: No, it isn't. You ask about Mac OS X in the title but your question itself only mentions Ubuntu.
-
timpone about 12 yearsthey attempt to both be *nix's; should be obvious from context
-
phuclv almost 6 years
-
-
timpone about 12 yearsxargs tends to be SLOW - just more surprised this is still an issue than anything. such a common procedure. ugh...
-
afrazier about 12 yearsThis 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 about 12 yearshmm.... maybe just alias rm -rf to that?
-
timpone about 12 yearsthx, helpful to know
-
phuclv almost 6 yearsThe 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?