167

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

Milligran
  • 3,031
  • 9
  • 44
  • 56

2 Answers2

306

Try the following:

find . -type d -empty
kenorb
  • 155,785
  • 88
  • 678
  • 743
Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
  • 5
    You'll need to change '/' to the directory you want to use as your search root. – Kirby Todd Feb 23 '12 at 18:29
  • Are you sure your directories are really empty? Do ls -a to see hidden (dot) files. – Kirby Todd Feb 23 '12 at 18:33
  • 5
    Thanks! It does work well on my system. To get a sorted list use: `find / -type d -empty | sort -u` – acme Mar 22 '12 at 15:49
  • 4
    Incidentally, to remove: find . -type d -empty -exec rmdir {} \; – Scott Mar 01 '15 at 04:01
  • 4
    Empty dirs in current dir: `find . -type d -empty`. In addition, empty files: `find . -type f -empty` in current dir and deeper. – starikovs Apr 28 '15 at 09:09
  • 34
    @Scott - why not `find . -type d -empty -delete`? – Ken Sharp Apr 16 '17 at 23:28
  • 1
    Ken, that would work--I didn't know about that option (I usually just get to a point where I can run a command on the right collection of files and start using -exec; I'll have to remember -delete for that next time). – Scott Apr 18 '17 at 14:43
  • 1
    @KenSharp @scott `-exec rmdir {} \;` scales better than `-delete` .. for a small number of items, it doesn't really matter. For large numbers, it does. Thus, I always use `-exec` because it works well regardless of the number. – bubba Sep 13 '18 at 02:51
  • 1
    @bubba why would `-exec rmdir {} \;` scale better than `-delete`? In my view, `-delete` is way better, as `find` just deletes the file, instead of spawning another process for each directory to delete – Peter H Sep 15 '18 at 08:46
  • https://unix.stackexchange.com/questions/167823/find-exec-rm-vs-delete/167824 – `-exec rmdir {} +` *might* scale better as it *could* reduce I/O, but there's no reason I can think of why `-exec rmdir {}` should see any improvement. And only then if `rmdir` isn't updating the filesystem data one by one (which I guess would depend on the filesystem and mount options). There would have to be a LOT of empty directories to notice any difference at all, even on my first 486DX. – Ken Sharp Sep 20 '18 at 05:26
  • This does not find the folder that has an empty folder inside – alper Jul 30 '22 at 13:00
  • @KenSharp can `find . -type d -empty -delete` also print the deleted dirs in the process – alper Jul 30 '22 at 13:08
4

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!

smac89
  • 39,374
  • 15
  • 132
  • 179