Skip to content

rmdir Command Cheat Sheet

rmdir removes empty directories. It is safer than rm -r because it refuses to delete a directory if it contains any files.


Synopsis

rmdir [OPTION]... DIRECTORY...

Basic Usage

rmdir my_folder

If my_folder is not empty:

rmdir: failed to remove 'my_folder': Directory not empty


Remove Parent Hierarchy (-p)

Remove a directory, then try to remove its parent if it becomes empty.

rmdir -p a/b/c
Equivalent to:
rmdir a/b/c
rmdir a/b
rmdir a


Usage Scenarios

Cleanup Scripts

If you want to clean up a directory tree but only if it's empty (ensuring you don't accidentally check in files).

rmdir build/artifacts

Notes

  • Hidden Files: If a directory contains only hidden files (.config), rmdir will still consider it not empty and fail.
  • Alternatives: rm -d (in some versions) behaves like rmdir.