9

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?

David Oneill
  • 1,208

3 Answers3

15

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.

rush
  • 27,403
3

Workaround #1:

find /path/to/dir -delete

Workaround #2:

rm a*;

rm b*;

rm c*;

etc
anlar
  • 4,165
  • 4
    you 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 a find /path/to/dir -delete, other than just using the same glob as the OP? – jsbillings Apr 06 '12 at 15:01
  • 1
    Also, 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
-2

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 !.

Arcege
  • 22,536