Orion42 ~IT made easy~

Giving feedback, alpha testing and solving common problems with out of the box and lateral thinking

Linux, Bash and Git

[Shell] How to remove all empty directories in a sub-tree?

Using shell, both in Linux or in Windows Sub-system for Linux [WSL], you can remove all the empty directories in a folder or hard drive and in all the sub-folders.

Short answer:
open your terminal and navigate into the top directory from where you want to remove all the empty sub-directories, and then type.

I found the answer in https://unix.stackexchange.com/questions/8430/how-to-remove-all-empty-directories-in-a-subtree :

Combining GNU find options and predicates, this command should do the job:

find . -type d -empty -delete

  • -type d restricts to directories
  • -empty restricts to empty ones
  • -delete removes each directory The tree is walked from the leaves without the need to specify -depth as it is implied by -delete.

Enjoy :)

LEAVE A RESPONSE

Skip to content