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

In this article I will show you how to delete the empty directories from a directory tree with the find command.

Watch Free Movies

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 {} +

Scroll to Top