Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers

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
4  
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
add comment

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 at 3:33
add comment

I'd do something like this:

import os
os.nice(19)
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:
  os.unlink(junk)
  time.sleep(0.1)
share|improve this answer
2  
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
add comment

Your Answer

 
discard

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.