I must remove 200 000 files (all of them) from a folder, and I don't want to delete the folder itself.

using rm, I get an "Argument list too long" error. I've tried to do something with xargs, but I'm not a Shell Guy, so it doesn't work:

find -name * | xargs rm -f
link|improve this question

5  
Why don't delete the folder and recreate it after deletion? – garconcn Sep 27 '11 at 2:28
feedback

4 Answers

up vote 20 down vote accepted
$ find /path/to/folder -type f -delete
link|improve this answer
14  
It's probably worth mentioning that GNU find (as used by most Linux distributions) can delete files on its own using -delete. This also avoids problems with files containing quotes or newline characters (though you could use GNU find's -print0 and GNU xarg's -0 options to fix that). – DerfK Sep 26 '11 at 16:22
3  
@DerfK, nice remark! Also, lots of ppl tend to play with xargs meanwhile find has -exec command {} + syntax. – poige Sep 26 '11 at 17:16
@DerfK: thanks. I've updated my answer. – quanta Sep 27 '11 at 2:14
feedback

You are doing everything right. It is the '*' that gives you a problem (the shell is expanding it into list of files instead of the find). The right syntax could be:

cd <your_directory>; find . -type f | xargs rm -f
find <your_directory> -type f | xargs rm -f

(The latter is a bit less efficient since it will pass longer names to xargs, but you will hardly notice :-) )

Alternatively, you could escape your '*' like this (however in that case it will also try also remove "." and ".."; it is not a biggie - you will just get a little warning :-) ):

find . -name '*' | xargs rm -f
find . -name "*" | xargs rm -f
find . -name \* | xargs rm -f

If your file names contain spaces then use this:

find . -type f -print0 | xargs -0 rm -f
link|improve this answer
This doesn't work if you have a filename containing a space. – Iain Sep 26 '11 at 16:25
@lain: Yes, I was just in a process of editing my answer to include the trick for white spaces :-) – dtoubelis Sep 26 '11 at 16:29
feedback

you can try

find /path/to/folder -type f -print0  | xargs -0 rm -f

or

find /path/to/folder -type f -exec rm -f "{}" +
link|improve this answer
feedback

The following command will delete all files from the current directory:

find . -type f -exec rm {} \;
link|improve this answer
2  
-name '*' doesn't mean "all files". Files are said with: -type f – poige Sep 26 '11 at 17:09
feedback

Your Answer

 
or
required, but never shown

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