Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 {} \;
share|improve this question
What's the -c3 parameter? – badp Nov 1 '10 at 11:18
@badp ionice -c3 basically set it to the lowest IO priority. – xenoterracide Nov 1 '10 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 '10 at 14:15
up vote 6 down vote accepted

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
share|improve this answer
Even better use find's -delete instead of spawning rms. – alex Nov 1 '10 at 13:44
@alex right, I completely forgot about it. I was simply tuning his, instead of thinking of that. – xenoterracide Nov 2 '10 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:
share|improve this answer
looks like python? – phunehehe Nov 1 '10 at 13:15
@phunehehe Bingo! – badp Nov 3 '10 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 '10 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
share|improve this answer
That will not be any more efficient, it still requires calling unlink() on every file. – Chris Down Jan 17 '14 at 3:33

Your Answer


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.