how can I recursively delete empty directories in my home directory?

217,232

Solution 1

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

Solution 2

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:
217,232

Related videos on Youtube

Santosh Kumar
Author by

Santosh Kumar

Updated on September 18, 2022

Comments

  • Santosh Kumar
    Santosh Kumar over 1 year

    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.

    • Admin
      Admin almost 12 years
      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.
  • Santosh Kumar
    Santosh Kumar almost 12 years
    And what if I only want to find/locate and not delete?
  • jordanm
    jordanm almost 12 years
    @Santosh - In either of the last two examples, just change rmdir -v to echo.
  • jordanm
    jordanm almost 12 years
    Good solution, but it should be noted that not all version of find have -empty.
  • Santosh Kumar
    Santosh Kumar almost 12 years
    @Baldrick Doesn't looks from home directory if I run it form ~/Desktop.
  • Baldrick
    Baldrick almost 12 years
    @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
    Baldrick almost 12 years
    @jordanm: You're quit right, I edited my answer accordingly.
  • Santosh Kumar
    Santosh Kumar almost 12 years
    @Baldrick Just a last task. I want to escape my ~/project directory.
  • Baldrick
    Baldrick almost 12 years
    @Santosh: You can use something like this: find "$HOME" -type d \! -path "$HOME/project/*" -empty -delete. The ! operator (or the escaped \!) returns false if the following expression is true. In effect, it excludes the ~/project directory. Note that if ~/project is empty, it will be deleted.
  • Vishnu Dasu
    Vishnu Dasu about 9 years
    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 {} \;
  • jlh
    jlh over 8 years
    These commands have terrible performance since they create a new process for every directory found. When using that approach, you really should at least use something like find ... | xargs -n 100 rmdir to delete them in batches. Even then, it won't work properly because it will fail to remove early directories that will later become empty because of later empty directories' removal. Thus you'd need to execute it several times.
  • jordanm
    jordanm over 8 years
    @jlh No, the first command will only execute rmdir once. That's the difference between using + and \; for terminating a command in find.
  • Greg Dubicki
    Greg Dubicki almost 8 years
    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..
  • KingsInnerSoul
    KingsInnerSoul over 7 years
    I had to use $ find . -type d -empty -delete
  • Franklin Yu
    Franklin Yu over 6 years
    @KingsInnerSoul Wasn't that already mentioned in answer?
  • KingsInnerSoul
    KingsInnerSoul over 6 years
    Maybe you are new to SE, but you can see that the my comment was added on October, and the answer was edited in November to include it! There is no need to spam everyone over a year after it was answered!
  • Charaf
    Charaf over 5 years
    It seems that it doesn't see hidden files. How to do it ?
  • Hotschke
    Hotschke over 5 years
    BSD version under macOS 10.12 has -empty.