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

silviud
  • 2,735
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,402