Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively delete all empty folders in Bash

Tags:

bash

shell

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.

like image 902
Rafid Avatar asked Dec 21 '25 11:12

Rafid


1 Answers

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.

like image 141
Jeff Schaller Avatar answered Dec 24 '25 01:12

Jeff Schaller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!