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.

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

Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *




Subscribe to get the latest Linux news and how to guides directly on your e-mail!