6 Answers
6

That will first drill down the directory tree until it finds the first empty directory, then delete it. Thus making the parent directory empty which will then be deleted, etc. This will produce the desired effect (I do this probably 10 times a week, so I'm pretty sure it's right). :-)

Consider if you have a tree (directories only) foo/bar/baz. Unless you use -depth, it will try to delete foo first, fail, and you'll end up with foo/bar after running.
–
l0b0Nov 8 '11 at 14:43

Possibly alternative is to use + instead of ; so you batch remove directories. Since you're doing it depth-first the children will still be removed before the parents (possibly dependent on your version of rmdir/bash and reliant on rmdir not deleting nonempty directories). This works for me in bash on cygwin: mkdir -p a/b/c/d ; find a -depth -type d -exec rmdir {} +
–
idbriiDec 19 '13 at 23:05

People, go for go2null's much more succinct answer below! Can't understand why SE gives priority to accepted answers rather than answers with most upvotes in displaying the answers below the question. The OP accepts the best answer available at his time of choosing, but later on much better answers can come which the community upvotes, no? (Of course, this is something for meta...)
–
jamadagniSep 19 '14 at 2:30

This won't work if any of the directory names contains whitespace or globbing characters. It's generally a bad idea to use command substitution on a list of file names. It's especially a bad idea with find because find has a way to do the processing cleanly: find … -exec.
–
GillesNov 7 '11 at 23:01

Thanks to Gilles for pointing that out. @lanzz, usually posting just a command without explaining what it does (and in this case, the pitfalls) is not enough. Please add to your answer.
–
n0peNov 8 '11 at 3:06