Deleting lots of files

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 {} ;
Asked By: Jonas Lejon


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:
Answered By: badp

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
Answered By: xenoterracide

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
Answered By: ddeimeke
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.