how to recursively delete empty directories in home directory

December 11, 2015 - Reading time: ~1 minute

The find command is the primary tool for recursive file system operations. Use the -type d expression to tell find you're interested in finding directories only (and not plain files). The GNU version of find supports the -empty test, so

$ find . -type d -empty -print

will print all empty directories below your current directory.

Use find ~ -… or find "$HOME" -… to base the search on your home directory (if it isn't your current directory).

After you've verified that this is selecting the correct directories, use -delete to delete all matches:

$ find . -type d -empty -delete

I would add -mindepth 1 here, to prevent from deleting the starting directory itself, if it would be empty. It's not really probable case for $HOME but if you would use this on any other directory..