rmdir
Remove empty directories
By CMD Script Team · 4 min read · Last updated
rmdir [OPTIONS] DIRECTORY...Options
| Flag | Description |
|---|---|
-p | Remove a directory and then attempt to remove each empty parent in the given path |
-v | Print a message for each directory removed |
--ignore-fail-on-non-empty | Ignore 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 emptyrmdir -v build— remove a directory and print a confirmation messagels -a old_folder— check for hidden files that might be blocking removal before runningrmdirrmdir 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/emptyremovesempty, thenutils, thensrc, thenproject, 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
findto prune empty directories throughout a tree:find . -type d -empty -exec rmdir {} \;. - Fall back to
rm -rexplicitly (and deliberately) only whenrmdirconfirms a directory truly isn't empty:rmdir folder || rm -ri folder.
find . -type d -empty -exec rmdir {} \;
Common mistakes
- Expecting
rmdirto remove a directory with files in it — it never does; that always requiresrm -r. - Not noticing that hidden dotfiles (like a stray
.DS_Storeor.gitkeep) inside a directory will make it "non-empty" and causermdirto fail even thoughls(without-a) shows nothing. - Assuming
rmdir -premoves non-empty parent directories too — it stops silently at the first non-empty parent, only removing the empty ones. - Using
rmdirwhen the actual intent wasrm -r, and then reaching forrm -rfreflexively out of frustration instead of checking what's actually inside first.
Tips
- When
rmdirfails, runls -a directoryto see what's inside (including hidden files) before deciding whether to manually clean it up or userm -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 blanketrm -r. - Treat an
rmdirfailure as useful information — "not empty" is a signal to look before reaching forrm -r, not just a hurdle to route around.
Best practices
- Prefer
rmdiroverrm -rwhenever 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
rmdirfor expected-empty directories (e.g. leftover build scaffolding) and reserverm -r/rm -rffor cases where you deliberately intend to delete contents too. - Avoid scripting a habit of "try
rmdir, then always fall back torm -rfon failure" — that defeats the safetyrmdirprovides; 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
rmdirandrm -r?rmdironly removes empty directories and errors on anything non-empty;rm -rrecursively deletes a directory and all of its contents regardless of whether it's empty. - Why would
rmdirfail on a directory that looks empty inls? Because it contains hidden files (dotfiles) that don't show up in a plainlslisting but still count as content;ls -awould reveal them. - What does
rmdir -pdo? 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.