Recursively delete empty directories

I recently had to find a neat way to remove all empty directories recursively on a Unix machine. In the world of UNIX you can expect to find a way to do things like this pretty easy. When I started to search for a neat way to do it (rather than reading a bunch of MAN-pages) I came across a really funny story on The Old New Thing. Windows users are so used to having to use an application to do simple things like this, they forget about scripting possibilities. Guess that will change with Power shell.

However this was about how to do this on Unix. Well, this is my solution:

find $1 -type d | sort -r |
while read D
  ls -l "$D" | grep -q 'total 0' && rmdir "$D" 2>/dev/null

That script takes one argument; a directory you want to remove if it and all its sub-directories are empty. Any directories encountered where files exists are preserved.