Orion42 Blog

~IT made easy~

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

Using bash, 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 Reply