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?

2

2 Answers 2

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.

2
  • 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. Dec 14, 2010 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, 2010 at 1: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.

3
  • 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, 2010 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, 2010 at 22:00
  • add a -v to your command to see what it's doing. See if it's progressing at all. Dec 13, 2010 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .