Take the 2-minute tour ×
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 13 down vote accepted

The GNU version of find supports the -empty test:

~$ find . -type d -empty -delete

Use -type d to tell find you're interested in directories only (and not files). Perhaps you should run the command without -delete first, so you can check what will be deleted.

share|improve this answer
2  
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=("$dir"/*)
   [[ ${files[@]} ]] || rmdir -v "$dir"
done
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

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