Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 9 down vote accepted

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.

share|improve this answer
    
Thanks - I knew there was going to be some elegant solution. –  David Oneill Apr 6 '12 at 15:17

Workaround #1:

find /path/to/dir -delete

Workaround #2:

rm a*;

rm b*;

rm c*;

etc
share|improve this answer
4  
you don't need to add -name "*" to find all files. It finds so by default. –  rush Apr 6 '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 6 '12 at 15:01
    
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 6 '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 !.

share|improve this answer
5  
Do not parse ls, or expect things to break. Badly. mywiki.wooledge.org/ParsingLs –  Chris Down Apr 6 '12 at 16:18
1  
I had mentioned that above: "if there are no special characters". –  Arcege Apr 6 '12 at 17:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.