Server Fault is a question and answer site for system and network administrators. 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 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:… and… – RobM Dec 13 '10 at 23:27
/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
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

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

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.