How to list empty folders in linux

Linux

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

Try the following:

find . -type d -empty

Solution 2 - Linux

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMilligranView Question on Stackoverflow
Solution 1 - LinuxKirby ToddView Answer on Stackoverflow
Solution 2 - Linuxsmac89View Answer on Stackoverflow