Howto delete empty directories in GNU /Linux with find linux command

Wednesday, 7th April 2010

Ever wondered how you can delete all the empty directories in Linux?
I bet you did, there are many ways to achieve that in GNU/Linux, however here is one way you might go:
First it is probably a good idea to list the empty directories and examinethe empty directories before you take the next step and execute a command to delete them:

find . -depth -type d -empty

Now after you take a close look in the directories, next step to partake is delete the directories.

find . -depth -type d empty -exec rmdir {} ;

Be aware that in the above examples, the first one would list all directories in your current directory in which you
execute the command, the second example will delete all the empty directories starting from your current directory unto thedeepest located empty directory in the directory tree.

Share this on:

Download PDFDownload PDF

Tags: , , , ,

2 Responses to “Howto delete empty directories in GNU /Linux with find linux command”

  1. Adam Vandenberg says:
    Google Chrome 8.0.552.18 Google Chrome 8.0.552.18 Mac OS X 10.6.4 Mac OS X 10.6.4
    Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.18 Safari/534.10

    You’ll want to quote the “{}” if the directly names can contain spaces.

    View CommentView Comment

Leave a Reply

CommentLuv badge