Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a directory with many subdirectories. I tried doing

rm -rf mydirectory

but it was still running after 1 hour. I have tried getting the number of subdirectories with

ls -l . | egrep -c '^-'

but it hasn't finished after 30 minutes.

Is there a faster way to recursively delete an entire directory?

share|improve this question
    
How many files are in that directory? If you have millions of files it will take a long time. What does df -i show on that filesystem? If you do have a large number of files there really isn't much you can do except wait. –  Zoredache Dec 13 '10 at 23:09
    
Not a duplicate question but similar ones you probably ought to read: serverfault.com/questions/127221/… and serverfault.com/questions/183821/… –  RobM Dec 13 '10 at 23:27
add comment

2 Answers

/usr/bin/find /mydir_with_many_subdirs -exec rm {} \;

you can also filter with

/usr/bin/find /mydir_with_many_subdirs -type f -exec rm {} \;  -- will delete all files
/usr/bin/find /mydir_with_many_subdirs -mtime +10 -exec rm {} \;  -- will delete dirs and files older then 10

man find will give away more filters that you can apply.

share|improve this answer
1  
This is going to take an ungodly amount of time. Either replace that \; with a + if you're running a recent version of GNU findutils, or -print0 and pipe it to xargs -0 if you're not. –  jgoldschrafe Dec 14 '10 at 0:13
    
in that case you can just use /usr/bin/find /my_dir -delete (of course you can still apply filters for time, access etc) –  silviud Dec 14 '10 at 1:21
add comment

This should work, but I wouldn't use the -f option. Nor would I want to run that command as root! If there are any symlinks that link back to say, /var/log or /usr/lib, then you will run into trouble faster than you can say "OMGWTF!"

Which is likely also the problem that you're running into here - there are symlinks that either go to very large directories, or symlinks that go into some kind of infinite loop. It shouldn't take that long to remove a small directory that doesn't have much to recurse to.

share|improve this answer
    
I am not root on this machine. Is there a solution to the symlink issue you describe, or a way to see if that's the case? –  chris Dec 13 '10 at 21:49
6  
I am confused, 'man 7 symlink' says that "The mv(1) and rm(1) commands do not follow symbolic links named as arguments, but respectively attempt to rename and delete them." –  Cory J Dec 13 '10 at 22:00
    
add a -v to your command to see what it's doing. See if it's progressing at all. –  drewrockshard Dec 13 '10 at 22:07
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.