Optimal way of deleting huge numbers of files

find /path/to/dir -type f -print0 | xargs -0 rm
Using xargs is better than: find /path/to/dir -type f -exec rm \-f {} \; as the -exec switch uses a separate process for each remove. xargs splits the streamed files into more managable subsets so less processes are required.

By: root
2009-01-26 11:30:47

4 Alternatives + Submit Alt

What Others Think

cd /path/to/dir ; ls | xargs rm
speirs · 636 weeks and 4 days ago
I think you can get find to delete for you with the -delete option, which I imagine might be fastest: find /path/to/dir -type f -delete or with the -exec xargs-like behavoir: find /path/to/dir -type f -exec rm {} + without launching the extra xargs process.
bwoodacre · 628 weeks and 2 days ago
@speirs: Never parse ls: http://mywiki.wooledge.org/ParsingLs @bwoodacre: +1 for the "+"
dennisw · 620 weeks and 4 days ago
Benchmarked this using the drupal-6.14 files extracted from http://ftp.drupal.org/files/projects/drupal-6.14.tar.gz time find drupal-6.14 -type f -delete real 0m0.024s user 0m0.004s sys 0m0.020s time find drupal-6.14 -type f -print0 | xargs -0 rm real 0m0.030s user 0m0.008s sys 0m0.020s time find drupal-6.14 -type f -exec rm {} + real 0m0.032s user 0m0.004s sys 0m0.028s time find drupal-6.14 -type f -exec rm \-f {} \; real 0m1.700s user 0m0.592s sys 0m0.628s
SlimG · 596 weeks and 2 days ago
how about this? time rm -r drupal-6.14 rm -r drupal-6.14 0.00s user 0.04s system 76% cpu 0.049 total find drupal-6.14 -type f -delete find drupal-6.14 -type f -delete 0.00s user 0.04s system 108% cpu 0.041 total
bwoodacre · 596 weeks and 2 days ago
rm -r will also delete subdirectories, whereas the find -type -f leaves the directory tree intact. Just curious...why have people wanted to delete just the files and not the directories (and other stuff)? Also, what does the plus do?
unixmonkey7434 · 595 weeks and 6 days ago
i have had this fail for me once on a machine with low ram, find couldn't expand the path, got the too many arguments error. ls | grep string |xargs rm worked in that situation. Most other times find works fine :)
alf · 576 weeks and 1 day ago
home health care el paso I think I need more information
yeoyeo · 26 weeks ago
great site! glad i found this. any recent updates? santa barbara painters
looneyhans · 12 weeks and 2 days ago
Are you looking for an escort girl, erotic services, stag-parties, strip clubs, brothels or massage parlors - international escort is the only website you need to know to get an instant casual date.
Geekstation · 8 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: