I accidentally created 8 million files and every time I'm trying to delete them the server almost dies because of the rm process eating all disk IO (the server is remote without console).

Should something like this work i.e. invoking ionice?

sudo find /var/lib/php5/ -type f -exec ionice -c3 rm -f {} \;
  • What's the -c3 parameter?
    – badp
    Nov 1, 2010 at 11:18
  • @badp ionice -c3 basically set it to the lowest IO priority. Nov 1, 2010 at 11:57
  • except unlink is a system call which has essentially no IO activity. The file system implements the actual IO needed to complete an unlink so I don't know if ionice can help here.
    – msw
    Nov 1, 2010 at 14:15

3 Answers 3


I would do it like this...

sudo ionice -c3 find /var/lib/php5/ -type f -exec  rm -f '{}' +

the + is more xargs like (I think), and thus more efficient at very large numbers of files. putting ionice on the whole command should make every sub command also ioniced, as well as the search itself. Have you tried this?

you could also make it really nice nice -n 19 ionice -c 3 programname though it shouldn't be necessary.

or taking a cue from @alex you could

ionice -c3 find /var/lib/php5/ -type f -delete
  • 7
    Even better use find's -delete instead of spawning rms.
    – alex
    Nov 1, 2010 at 13:44
  • @alex right, I completely forgot about it. I was simply tuning his, instead of thinking of that. Nov 2, 2010 at 0:22

I'd do something like this:

import os
dir = "/var/lib/php5/"
bad_files = [os.join(dir, file) for file in os.listdir(dir)
                     if is_bad(os.join(dir, file))]
for junk in bad_files:
  • 2
    looks like python?
    – phunehehe
    Nov 1, 2010 at 13:15
  • @phunehehe Bingo!
    – badp
    Nov 3, 2010 at 10:09
  • yeah I have just started learning python, but don't you think you should include some instruction on how to run this script?
    – phunehehe
    Nov 3, 2010 at 10:41

Maybe too silly.

What about deleting the whole directory structure?

find /var/lib/php5 -type d -print > /tmp/directories
rm -r /var/lib/php5
cat /tmp/directories | xargs mkdir
  • That will not be any more efficient, it still requires calling unlink() on every file.
    – Chris Down
    Jan 17, 2014 at 3:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .