Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Possible Duplicate:
How to remove all empty directories in a subtree?

I create directories very often, scattered over my home directory, and I find it very hard to locate and delete them.

I want any alias/function/script to find/locate and delete all empty directories in my home directory.

share|improve this question

marked as duplicate by Gilles, jasonwryan, Renan, Ulrich Dangel, jw013 Aug 29 '12 at 19:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This does not answer your question, but could solve the underlying problem. I often use the construct: WORK=$(mktemp -d) or cd $(mktemp -d). Of course don't put important files that you need to preserve in those directories. But most likely your system is already setup to automagically make those files disappear after a while. –  emory Aug 25 '12 at 23:35
I have my machine mount a tmpfs ram drive to the /z/ directory on start-up and do all my temporary work there. –  Richard Oct 21 '13 at 20:23

2 Answers 2

up vote 42 down vote accepted

The find command is the primary tool for recursive, filesystem 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
share|improve this answer
Good solution, but it should be noted that not all version of find have -empty. –  jordanm Aug 25 '12 at 21:49
@Baldrick Doesn't looks from home directory if I run it form ~/Desktop. –  Santosh Kumar Aug 25 '12 at 21:58
@Santosh: The command as it is, is meant to be run from your home directory (that's why I added ~$ in the beginning). If you want to run it regardless of your working directory, use "$HOME" instead of . as jordanm suggested in his answer. –  Baldrick Aug 25 '12 at 22:09
@jordanm: You're quit right, I edited my answer accordingly. –  Baldrick Aug 25 '12 at 22:31
@Baldrick Just a last task. I want to escape my ~/project directory. –  Santosh Kumar Aug 27 '12 at 6:12

You can call rmdir on every directory, since rmdir will only delete a directory if it is empty:

find "$HOME" -type d -exec rmdir {} + 2>/dev/null

If you also want to print the directories being removed, you will need to check if they are empty:

find "$HOME" -type d -exec bash -c 'shopt -s nullglob; shopt -s dotglob; files=("$1"/*); [[ ${files[@]} ]] || rmdir -v "$1"' -- {} \; 

Here is a pure bash example (version 4 or higher):

shopt -s globstar
for dir in **/; do
   [[ ${files[@]} ]] || rmdir -v "$dir"
share|improve this answer
And what if I only want to find/locate and not delete? –  Santosh Kumar Aug 25 '12 at 21:44
@Santosh - In either of the last two examples, just change rmdir -v to echo. –  jordanm Aug 25 '12 at 21:47
BusyBox find doesn't have -empty, so the -exec trick is helpful. But rather than sending errors off to /dev/null, it's better to tell rmdir to suppress them, e.g. find . -type d -depth -exec rmdir -p --ignore-fail-on-non-empty {} \; –  Robert Calhoun Apr 1 at 19:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.