How to find and delete the empty directories in a directory tree

How to find and delete the empty directories in a directory tree

In this tutorial I will show you, how to find and delete the empty directories in a directory tree.

I will use the find command, combined with rmdir.

The rmdir command deletes only the empty directories and doesn’t do anything to the non empty directories.

Delete the empty directories from the current directory:

$ find . -mindepth 1 -maxdepth 1 -type d -exec rmdir {} +

Delete the empty directories recursively, by modifing the -maxdepth argument:

$ find . -mindepth 1 -maxdepth 2 -type d -exec rmdir {} +

$ find . -mindepth 1 -maxdepth 3 -type d -exec rmdir {} +

 
Enjoyed this video?
How to find and delete the empty directories in a directory tree
"No Thanks. Please Close This Box!"