How to list empty folders in linux

In Linux how do I check all folders in a directory and output the name of all directories that are empty to a list.


Try the following:

find . -type d -empty

With Zsh, you can do the following:

printf '%q\n' ./*/**/(/DN^F)

Replace . with the actual path to the directory you want, or remove it if you want to search the entire file system.


From the section called Glob Qualifiers:

F

‘full’ (i.e. non-empty) directories. Note that the opposite sense (^F) expands to empty directories and all non-directories. Use (/^F) for empty directories.

  • / means show directories
  • D means to also search hidden files (directories in this case)
  • N Enables null pattern. i.e. the case where it finds no directories should not cause the glob to fail
  • F means to show non-empty directories
  • ^ is used to negate the meaning of the qualifier(s) following it

To put them all into an array:

empties=(./*/**/(/DN^F))

Bonus: To remove all the empty directories:

rmdir ./*/**/(/DN^F)

Looks like we finally found a useful case for rmdir!