1

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?

chris
  • 111
  • 2
  • 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: http://serverfault.com/questions/127221/deleting-large-no-of-files-on-linux-eats-up-cpu and http://serverfault.com/questions/183821/rm-on-a-directory-with-millions-of-files – Rob Moir Dec 13 '10 at 23:27

2 Answers2

2
/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.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
silviud
  • 2,677
  • 2
  • 16
  • 19
  • 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 00: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 01:21
0

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.

Ernie
  • 5,324
  • 6
  • 30
  • 37
  • 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