Linux rmdir Command: Remove Empty Directories
This guide explains how to remove empty directories with Linux rmdir, delete empty parent paths, diagnose failures, and understand when rm -r is different. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.
The examples use GNU Coreutils behavior. Check rmdir --help, man rmdir, and your distribution documentation when portability or a version-specific option matters.
What Is the rmdir Command?
rmdir removes only empty directories. It refuses to remove a directory that still contains files, hidden entries, or child directories, which makes it a useful guardrail when recursive deletion is not intended.
Basic Syntax
rmdir [OPTION]... DIRECTORY...
Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.
Basic rmdir Usage
Remove one empty directory
The directory is removed only when it contains no entries.
rmdir old-cache
Remove several empty directories
Each operand is processed in order. Some may succeed while others fail, so check the messages and exit status.
rmdir empty-a empty-b empty-c
Remove empty parent directories
With -p, rmdir removes the named directory and then attempts its empty parents from the bottom up.
rmdir -p a/b/c
Checking the result of removing an empty directory
rmdir does not output any message when removing an empty directory. Apply the example below only to temporarily created targets.
mkdir empty-demo rmdir empty-demo test ! -e empty-demo echo $?
The sample output is 0. This means that the condition that the path does not exist in test was true. If there are even hidden files inside the directory, rmdir will fail, so check the contents first.
Key Options
| Option | Purpose |
|---|---|
-p, --parents |
Remove the directory and empty parent components. |
-v |
Report each directory being processed. |
--ignore-fail-on-non-empty |
Suppress failure diagnostics caused only by a nonempty directory. |
Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.
Practical Examples
Check whether the directory is truly empty
A normal ls can hide dotfiles. Use ls -la or find when removal fails unexpectedly.
ls -la old-cache find old-cache -mindepth 1 -maxdepth 1 -print
Clean up only when empty
A script can attempt rmdir and keep the directory when data remains. Treat other failures separately when they matter.
if rmdir -- cache-dir 2>/dev/null; then printf '%s\n' 'Removed the empty cache directory.' fi
Report parent cleanup
Combine -p and -v to see how far cleanup proceeded. Avoid -p when a specific parent boundary must remain.
rmdir -pv build/tmp/session
rmdir and Related Commands
| Command or method | Primary role | Best fit |
|---|---|---|
rmdir |
Remove empty directories only | Preserving any path that still has data |
rm -d |
Remove empty directories with GNU rm | Mixed file operations |
rm -r |
Delete a directory tree recursively | Intentional removal of all contents |
find -empty -delete |
Find and remove matching empty entries | Rule-based cleanup across many paths |
Important Cautions
Hidden files count as contents
Files such as .keep and .gitignore make a directory nonempty.
-p may reach higher than expected
If every parent is empty, multiple levels can be removed. Choose the cleanup boundary before running the command.
Failure can be useful information
A nonempty failure often means data remains. Do not suppress every error without checking why it occurred.
FAQ
Can rmdir remove a directory that contains files?
No. Use a carefully reviewed rm -r only when all contents should be deleted.
Why does an apparently empty directory fail to be removed?
It may contain hidden entries, be a mount point, or have a permission problem.
How do I remove empty parents too?
Use rmdir -p PATH. Each level must be empty.
Is rmdir safer than rm -r?
It is more restrictive because it refuses nonempty directories, but the target path must still be verified.









