How to list empty folders in linux
LinuxLinux 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 directoriesD
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 failF
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
!