Skip to content
shellmap

List empty directories

Find directories with no files — useful for cleanup, build-output verification, and detecting failed extractions.

How to list empty directories in each shell

Bashunix
find . -type d -empty
Zshunix
find . -type d -empty
Fishunix
find . -type d -empty
PowerShellwindows
Get-ChildItem -Directory -Recurse | Where-Object { @(Get-ChildItem $_.FullName -Force).Count -eq 0 }

`-Force` includes hidden + system files in the count — without it, a dir containing only `.git` or `.DS_Store` appears falsely empty. `@(...)` array-wraps so `.Count` is defined on a 0-element result (without it, an empty pipeline returns `$null` and `.Count` throws).

cmd.exewindows
for /d /r %D in (*) do @dir /b /a "%D" | findstr "^" >nul || echo %D

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • `find -empty` is supported on BOTH GNU and BSD `find` since the early 2000s — portable. Combine with `-type d` to filter to directories (without it, also reports zero-byte files). Add `-delete` to recursively remove them: `find . -type d -empty -delete` (USE WITH CARE — re-runs the check as it deletes, which may newly-empty a parent dir and delete that too; behavior is intentional for full-tree cleanup but unexpected if you weren't ready for it).
  • macOS `.DS_Store` trap: Finder writes a `.DS_Store` file in every directory it browses. On any macOS-touched filesystem, "empty" directories aren't — they hold `.DS_Store`. `find -empty` correctly reports them as NON-empty (file exists). To list dirs containing only `.DS_Store`: `find . -type d ! -name .git -exec sh -c 'ls -A "$1" | grep -v "^\.DS_Store$" | head -1 | grep -q . || echo "$1"' _ {} \;`.
  • pwsh edge cases: `Get-ChildItem -Recurse` follows REPARSE POINTS (junctions, symlinks to dirs) by default on pwsh 5.1, which can create infinite recursion on circular junctions. Pass `-Attributes !ReparsePoint` to skip them; on pwsh 7+ add `-FollowSymlink:$false`. Hidden NTFS files (`-Attributes Hidden,System`) require `-Force` to be seen.
  • For a count-only "is this dir empty?" check: bash `[ -z "$(ls -A dir)" ] && echo empty`. pwsh `(Get-ChildItem dir -Force | Measure-Object).Count -eq 0`. Both POSIX `ls -A` and pwsh `-Force` show dotfiles + hidden — without those flags, a `.git/` repo would falsely look empty.

Related commands

Related tasks