Reputation: 3171
In Linux how do I check all folders in a directory and output the name of all directories that are empty to a list.
Upvotes: 180
Views: 95413
Reputation: 24
To list empty folders in Linux, you can use the find command. Open your terminal and run the following command:
find /dev -type d -empty
Upvotes: 0
Reputation: 43234
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 itTo 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
!
Upvotes: 5