Milligran
Milligran

Reputation: 3171

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.

Upvotes: 180

Views: 95413

Answers (3)

asur_3228
asur_3228

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

enter image description here

Upvotes: 0

smac89
smac89

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 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!

Upvotes: 5

Kirby Todd
Kirby Todd

Reputation: 11566

Try the following:

find . -type d -empty

Upvotes: 329

Related Questions