bashshell

Recursively delete all empty folders in Bash


Is there a command to execute in Bash that deletes all empty folders recursively until there is no empty folder in the tree? I could execute this:

find . -type d -empty | xargs -I '{}' rmdir {}

repeatedly until there is no more empty folders, but I am looking for something a bit more efficient. Especially since that to know whether there are empty folders left, I would have to execute the same command, i.e. two calls to find . -type d -empty in each iteration.


Solution

  • This is simple, given the GNU find utility:

    find . -type d -empty -delete
    

    This will delete empty directories; since the -delete option implies the -depth option, it will delete directories that only had empty directories underneath them, so there's no need to run it multiple times.