I have a directory that was generated by mistake by an application and this directory has a structure that involves more than 2000 directories, one inside a another, something like:

Directory aaaa inside bob inside ccc inside ddd inside eee etc. These are 1000 levels deep and many of these directories have names where spaces are URL encoded (spaces are %20 and > are %3E etc).

If I try to remove that directory using rm -fr I get the message directory is not empty. I think the string necessary to remove a directory is too long for UNIX. Is there a way to delete this directory with rm?

I am on Mac OS X Mountain Lion.

  • What is the output of type -a rm?
    – jordanm
    Commented Jan 13, 2013 at 21:07
  • rm is /bin/rm ... why?
    – Duck
    Commented Jan 13, 2013 at 21:09
  • Can you remove it in the finder?
    – ott--
    Commented Jan 13, 2013 at 21:14
  • 1
    Try using: sudo rm -rfv /path/to/file Perhaps it is a permission issue with one of the nested files and sudo will let you get past it, although usually you get an error message if this is the case. Also the -v will verbosely list the files, so maybe it will show you if it is getting hung up on a certain file. Can you please post your output starting from the rm command you type all the way until the next prompt that the terminal returns? Commented Jan 13, 2013 at 21:29
  • 1
    Try using the find command instead. First confirm the syntax using ls instead of rm to make sure that you get the output you expect, which should be a list of the files you want to delete. Because of the size of the directory you are dealing with, terminate the command early with Ctrl+C and scan the output: find -d /path/to/file -exec ls {} \; If everything looks good, then swap out the ls for rm -rf and hopefully the directory will be removed: find -d test -exec rm -rf {} \; Commented Jan 13, 2013 at 22:01

2 Answers 2


If you use rm -rf stuff_to_delete with a very deep structure then it is possible that there are too many directories for rm to handle.

You can work around this with:

find /starting/path/to/delete/from -type d -delete

or with

find -type d /starting/path/to/delete/from -exec rm -f {} \;

The first should just work. The second command starts a new command (rm) for each directory, but that allows you to use rm's force flag. I assume it is not needed though and I expect the first command to be faster.

Regardless of command used, try first with -print to make sure your path is correct.

  • perfect! The only thing this works is something like your first command. I found this one just seconds before reading your solution and this worked for me ........ find . -name '.*' -delete THANKS!
    – Duck
    Commented Jan 13, 2013 at 22:46
  • Do you have a reference for "it is possible that there are too many directories for rm to handle"? A recursive rm does the same kind of depth first traversal that "find" does, so I don't see how it could fail unless it ran out of memory to store the directory tree as it traverses it (in which case it should give an out of memory error, not silently fail).
    – Johnny
    Commented May 3, 2013 at 0:09
  • Just to satisfy my own curiosity, I created a directory tree 10,000 levels deep, with each level having 50 small text files, and rm -r worked just fine. But this was on Linux, does the OSX version of rm have problems with deep directory trees?
    – Johnny
    Commented May 3, 2013 at 0:36

I have observed failures of "sudo rm -fr " on my MacOSX 10.8 laptop fairly regularly. In the latest case, the directory I was deleting contained ~28,000 file in a directory hierarchy that was maybe 8 or 10 levels deep, at most; the total size was about 4 GB on disk. Simply running "sudo rm -fr " a second time removed the leftover files, as it always has for me.

It doesn't look to me like the problem is the depth of the directory hierarchy, because the undeleted files always seem to be near the top of the directory tree. It's hard to believe it could have anything to do with the total number of files or directories, because I would expect 'rm' to delete these directories recursively.

You must log in to answer this question.

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