Home > Mobile >  Recursively delete all empty folders in bash
Recursively delete all empty folders in bash

Time:05-13

Is there an command to execute in Bash to delete 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 that to know whether there is more empty folders, I would have to execute the same command, i.e. two calls to find . -type d -empty in each iteration.

Thanks!

CodePudding user response:

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.

  • Related