>_cmd.script

rmdir

Remove empty directories

Directories

By CMD Script Team · 4 min read · Last updated

SYNTAX
rmdir [OPTIONS] DIRECTORY...

Options

Command options and flags
FlagDescription
-pRemove a directory and then attempt to remove each empty parent in the given path
-vPrint a message for each directory removed
--ignore-fail-on-non-emptyIgnore failures that occur solely because a directory is not empty

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

rmdir removes directories, but only if they're completely empty. If a directory contains any files or subdirectories, rmdir refuses and reports an error rather than deleting anything — this makes it a much safer, more conservative tool than rm -r for directory cleanup.

Beginner examples

  • rmdir old_folder — remove a directory, but only if it's empty
  • rmdir -v build — remove a directory and print a confirmation message
  • ls -a old_folder — check for hidden files that might be blocking removal before running rmdir
  • rmdir folder1 folder2 — remove multiple empty directories in one command
rmdir old_folder

Advanced examples

  • Clean up a whole chain of now-empty parent directories after removing the deepest one: rmdir -p project/src/utils/empty removes empty, then utils, then src, then project, stopping as soon as one isn't empty.
  • Use in scripts as a safe pre-check before a more destructive operation: `rmdir dir 2

    /dev/null || echo "dir not empty, skipping"`.

  • Combine with find to prune empty directories throughout a tree: find . -type d -empty -exec rmdir {} \;.
  • Fall back to rm -r explicitly (and deliberately) only when rmdir confirms a directory truly isn't empty: rmdir folder || rm -ri folder.
find . -type d -empty -exec rmdir {} \;

Common mistakes

  • Expecting rmdir to remove a directory with files in it — it never does; that always requires rm -r.
  • Not noticing that hidden dotfiles (like a stray .DS_Store or .gitkeep) inside a directory will make it "non-empty" and cause rmdir to fail even though ls (without -a) shows nothing.
  • Assuming rmdir -p removes non-empty parent directories too — it stops silently at the first non-empty parent, only removing the empty ones.
  • Using rmdir when the actual intent was rm -r, and then reaching for rm -rf reflexively out of frustration instead of checking what's actually inside first.

Tips

  • When rmdir fails, run ls -a directory to see what's inside (including hidden files) before deciding whether to manually clean it up or use rm -r.
  • Use find . -type d -empty -exec rmdir {} \; to prune all empty directories in a tree in one pass, which is much safer than a blanket rm -r.
  • Treat an rmdir failure as useful information — "not empty" is a signal to look before reaching for rm -r, not just a hurdle to route around.

Best practices

  • Prefer rmdir over rm -r whenever you specifically intend to remove only empty directories — its refusal to touch non-empty ones is a safety feature, not a limitation.
  • In cleanup scripts, use rmdir for expected-empty directories (e.g. leftover build scaffolding) and reserve rm -r/rm -rf for cases where you deliberately intend to delete contents too.
  • Avoid scripting a habit of "try rmdir, then always fall back to rm -rf on failure" — that defeats the safety rmdir provides; inspect first.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Cleaning up empty leftover build or cache directories after a build process without risking deletion of directories that unexpectedly still contain files.
  • Pruning empty directories left behind after moving or archiving files out of a project tree.
  • Safely checking whether a directory is truly empty as part of a script's cleanup logic, using rmdir's failure as a built-in "not empty" check.

Common interview questions

  • What's the difference between rmdir and rm -r? rmdir only removes empty directories and errors on anything non-empty; rm -r recursively deletes a directory and all of its contents regardless of whether it's empty.
  • Why would rmdir fail on a directory that looks empty in ls? Because it contains hidden files (dotfiles) that don't show up in a plain ls listing but still count as content; ls -a would reveal them.
  • What does rmdir -p do? It removes the target directory, then walks up the path removing each parent directory too, but only as long as each one is also empty — stopping at the first non-empty parent.

Frequently Asked Questions

Why does rmdir fail with 'Directory not empty'?

rmdir only removes directories that contain no files or subdirectories, including hidden ones. If anything is inside, it refuses to delete it. Use rm -r (or rm -rf) if you actually want to remove a directory and its contents.

What's the difference between rmdir and rm -r?

rmdir only removes empty directories and will error on anything non-empty, making it a safer, more deliberate tool. rm -r recursively deletes a directory and everything inside it, files and subdirectories alike, without requiring it to be empty first.

What does rmdir -p do?

It removes the specified directory, then walks back up the path removing each parent directory in turn, but only as long as each one is also empty. It stops (without erroring) as soon as it hits a non-empty parent.

Cheat sheet

Download a quick-reference cheat sheet for rmdir.