I have a folder with 137795 files in it. I need to delete all of them. When I run rm *
I get -bash: /bin/rm: Argument list too long
. How do I get past this error?

- 829,060

- 1,208
3 Answers
As I can see you don't need to remove your dir , only files inside. So you can recreate it
rm -r /path/to/dir && mkdir /path/to/dir
or even delete only files inside
find /path/to/dir -type f -delete
afair first one works faster.
UPD. Note that way with find
might be not optimal from space consumption point of view, as directory size will be reduced only after fsck
. Details.

- 27,403
-
Thanks - I knew there was going to be some elegant solution. – David Oneill Apr 06 '12 at 15:17
Workaround #1:
find /path/to/dir -delete
Workaround #2:
rm a*;
rm b*;
rm c*;
etc

- 4,165
-
4you don't need to add
-name "*"
to find all files. It finds so by default. – rush Apr 06 '12 at 15:00 -
using
-name "*"
seems a bit redundant, could you explain why you chose that instead of just afind /path/to/dir -delete
, other than just using the same glob as the OP? – jsbillings Apr 06 '12 at 15:01 -
1Also, depending on the implementation or age of your
find
, "*" might not include files that start with a "." (current versions of findutils does include that dotted files). – jsbillings Apr 06 '12 at 15:04
Using find
is probably the best bet. Some of the problems with the other answers are either not deleting everything inside the directory or deleting the directory itself. You can either use ls
with xargs
, if there are no special characters, or find
with certain options.
ls -1 | xargs rm -r
or
find . -depth -path ./.* -prune -o -not -name . -delete
This will ignore anything in the current directory starting with .
(-prune
) and will remove any other file or directory, as long as it is not the top directory (.
). The -depth
will look inside directories first, which will avoid a 'directory not empty' error. If the -not
argument is not available on your system, then use !
.

- 22,536
-
5Do not parse
ls
, or expect things to break. Badly. http://mywiki.wooledge.org/ParsingLs – Chris Down Apr 06 '12 at 16:18 -
1