Finding and removing empty folders

Imagine that you decided to download a torrent that originally has a million folders. Then, imagine that you’ll only download actually one or two of them. Imagined that?

Now, imagine that you’re cleaning your files, you know, and then suddenly you see yourself in front of that million of empty folders. Of course you want to rip them off!

Knowing that find is a **VERY** powerful tool, reading its man I found out the following:

$ find . -type d -empty

And voila, only the empty dirs of the folder you are are returned.

Now, to delete them you have two options:

$ find . -type d -empty -exec rmdir "{}" ";"

(This should execute rmdir n times, where n is the number of empty folders the find command returns.)

or

$ find . -type d -empty | xargs rmdir -

(This should get all the output of the find command and use as input to rmdir command, wrapped by xargs.)

Both of them should work.

Leave a Reply

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