Skip to content
Latchkey

find -empty: Find Empty Files and Dirs

find -empty matches regular files with zero bytes and directories with no entries.

Empty files and directories often litter a workspace after a failed step. -empty finds them so you can prune the noise.

What it does

find -empty matches a regular file whose size is zero or a directory that contains no entries. Combined with -type it targets one or the other, for example -type d -empty to clean up empty output directories.

Common usage

Terminal
find . -type f -empty
find . -type d -empty -delete
find dist -empty

Options

ElementWhat it does
-emptyMatch zero-byte files or empty directories
-type f -emptyOnly empty regular files
-type d -emptyOnly empty directories
-deleteCombine to remove the empties

In CI

find . -type d -empty -delete is a tidy way to drop leftover empty output directories after a build. Run it repeatedly if removing children creates new empty parents, or rely on the -depth behavior of -delete to cascade in one pass.

Common errors in CI

-empty is a GNU extension that is also supported by BSD/macOS find, so it is portable on common runners. If it appears unsupported on a very minimal BSD, replace it with -size 0 for files. Mixing -empty with -delete on a deep tree may need a second pass if parents become empty.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →