Empty directories often survive builds, downloads, archives, and cleanup scripts. The rmdir command in Linux removes only empty directories, so it gives you a safer first pass before reaching for recursive deletion.
That strict behavior is the point: if a directory still contains files, subdirectories, or hidden dotfiles, rmdir refuses to remove it. Use the patterns here to remove one empty directory, prune empty parent paths, clean empty directory trees with find, and decide when rm -r is the correct tool instead.
Understand the rmdir Command in Linux
What rmdir Does
The rmdir command removes directory entries only when they are empty. Empty means no visible files, no hidden files such as .gitkeep, and no child directories. If any content remains, rmdir exits with an error and leaves the directory untouched.
This makes rmdir useful when you expected a path to be clear already. It can confirm that a cleanup step finished without turning a small mistake into a recursive delete.
rmdir Command Syntax
The basic syntax is short:
rmdir [OPTION]... DIRECTORY...
OPTIONchanges how removal is handled, such as parent removal with-por verbose output with-v.DIRECTORYis one or more directory paths that must already be empty.- A successful plain
rmdircommand prints nothing; errors appear only when a path cannot be removed.
rmdir Quick Reference
| Task | Command Pattern | What It Does |
|---|---|---|
| Check availability | command -v rmdir | Prints the resolved binary path. |
| Remove one empty directory | rmdir old-cache | Deletes the directory only if it has no contents. |
| Remove several empty directories | rmdir old-cache test-output scratch-space | Processes each path and reports failures per directory. |
| Remove empty parent paths | rmdir -p releases/2026/drafts | Removes the child, then each empty parent. |
| Show successful removals | rmdir -v old-cache | Prints a diagnostic for each directory removed. |
| Skip non-empty failures | rmdir --ignore-fail-on-non-empty old-cache | Suppresses failures caused only by remaining contents. |
| Clean empty trees | find project -depth -mindepth 1 -type d -exec rmdir --ignore-fail-on-non-empty {} + | Prunes empty children and parents in one bottom-up pass. |
| Open the local manual | man rmdir | Shows the manual page installed on your system. |
Verify rmdir Availability and Help
Most full Linux distributions provide rmdir through GNU Coreutils or a compatible implementation, while minimal systems often provide a BusyBox applet. Confirm the command path first:
command -v rmdir
Common output looks like this:
/usr/bin/rmdir
Use the built-in help output for supported options on the current system:
rmdir --help
Relevant GNU Coreutils help lines include:
Usage: rmdir [OPTION]... DIRECTORY... Remove the DIRECTORY(ies), if they are empty.
Help text formatting varies between GNU Coreutils, BusyBox, and other compatible implementations, but the core empty-directory behavior is the same. The GNU Coreutils rmdir manual documents the upstream GNU options and also points to rm for recursive non-empty directory removal.
Practical rmdir Command Examples
Remove One Empty Directory
Use a plain rmdir command when you already expect the directory to be empty:
rmdir old-cache
test ! -d old-cache && echo "old-cache removed"
Expected output after the verification check:
old-cache removed
If old-cache still contains anything, the first command fails and the verification line does not print.
Remove Several Empty Directories
Pass multiple paths when several directories should be empty already:
rmdir old-cache test-output scratch-space
rmdir processes each argument. Empty paths are removed, but any path with contents stays in place and produces an error such as:
rmdir: failed to remove 'test-output': Directory not empty
The command exits with a nonzero status if any argument fails, which matters in scripts and CI jobs.
Remove Empty Parent Directories with rmdir -p
The -p or --parents option removes the target directory first, then attempts each parent component in order. This is useful for abandoned nested paths:
rmdir -pv releases/2026/drafts
Expected output when every directory in the chain is empty:
rmdir: removing directory, 'releases/2026/drafts' rmdir: removing directory, 'releases/2026' rmdir: removing directory, 'releases'
The chain stops at the first parent that still contains something. When you need to rebuild paths later, the mkdir command in Linux covers parent creation with -p from the opposite direction.
Confirm Successful Removals with rmdir -v
Verbose mode prints a line for every successful removal:
rmdir -v build-empty
Expected output:
rmdir: removing directory, 'build-empty'
Use -v when a script or cleanup job should leave a readable record of what actually disappeared.
Skip Non-Empty Directories Without Failing
The --ignore-fail-on-non-empty option ignores failures caused only by remaining contents:
rmdir --ignore-fail-on-non-empty old-cache test-output scratch-space
Non-empty directories remain in place without producing a diagnostic. Failures for missing paths or permission problems still report errors and return a nonzero status.
This option is best for cleanup jobs where leftover content is acceptable and should not make the whole job fail. Use a reporting loop if you need a per-directory success list.
Remove Directory Names with Spaces
Quote names that contain spaces so the shell passes the whole path as one argument:
rmdir "old cache"
rmdir 'release candidate'
rmdir archived\ logs
All three forms can work. Quoting is usually easiest to read, while backslash escaping is what many shells insert through tab completion.
rmdir vs rm Command in Linux
rmdir and rm both remove filesystem entries, but they carry very different safety profiles:
| Task | Use | Why |
|---|---|---|
| Remove an empty directory | rmdir path | Fails safely if anything remains inside. |
| Remove empty parent paths | rmdir -p path/to/child | Prunes empty ancestors without deleting files. |
| Remove a directory and all contents | rm -r path | Deletes recursively, so inspect the path first. |
| Remove a symlink to a directory | unlink link_name | Deletes the link, not the target directory. |
Start with rmdir when a directory should already be empty. If it fails, inspect the contents before deciding whether a recursive rm -r delete is really the intended next step.
Clean Empty Directory Trees with rmdir and find
rmdir does not recursively remove a directory tree by itself. To remove every empty directory under a path, use find to discover candidates, then call rmdir only on those matches. The find -exec command option in Linux is useful background if you want more detail on batched -exec usage.
Preview matches before removing them. A broad
findexpression can touch many paths, and-mindepth 1protects the starting directory from being treated as a removal target.
Preview the empty directories first:
find project -depth -mindepth 1 -type d -empty -print | sort
Example output from a small project tree:
project/build project/cache/old-empty project/logs/old-empty
After checking the list, remove the same empty directories:
find project -depth -mindepth 1 -type d -exec rmdir --ignore-fail-on-non-empty {} +
Verify which directories remain afterward:
find project -mindepth 1 -type d -print | sort
Only non-empty paths should remain:
project/cache project/cache/full
-depthvisits child directories before their parents, so parent directories can become empty during the same pass.-mindepth 1keepsprojectitself out of the result set.-type dpasses each directory tormdir; this lets parents be removed after their empty children disappear.--ignore-fail-on-non-emptykeeps non-empty directories from adding noise while still leaving them untouched.-exec rmdir ... {} +batches matched paths into fewerrmdircalls.
GNU find also has -delete, but the -exec rmdir {} + pattern keeps the empty-directory safety of rmdir visible and works on common minimal environments that do not provide every GNU extension. On older Unix systems without --ignore-fail-on-non-empty, use the same bottom-up shape and expect diagnostics for non-empty parents.
Use rmdir in Shell Scripts
Scripts should treat rmdir as a conditional cleanup step. A failed removal does not always mean the script is broken; it often means a directory still has content that should be reviewed or preserved.
This loop reads directory names from a file, handles spaces safely, and reports which entries were removed or skipped:
while IFS= read -r dir; do
if [ -d "$dir" ] && rmdir "$dir" 2>/dev/null; then
printf 'Removed: %s\n' "$dir"
else
printf 'Skipped: %s\n' "$dir"
fi
done < cleanup-list.txt
Example output:
Removed: release-candidate Removed: old cache Skipped: missing
IFS= and read -r preserve spaces and backslashes in names. The if statement keeps a non-empty or missing path from stopping the rest of the loop, which is safer than hiding every error and assuming the cleanup succeeded.
Troubleshoot Common rmdir Errors
Fix “Directory not empty” with rmdir
This error means the target still contains a file, hidden file, or subdirectory:
rmdir: failed to remove 'reports': Directory not empty
List direct contents, including hidden entries:
find reports -mindepth 1 -maxdepth 1 -print
Relevant output might show a hidden placeholder:
reports/.gitkeep
Remove or move the remaining content only after you have identified it. Then retry the empty-directory removal:
rmdir reports
rm -r reportsremoves the directory and everything inside it. Use it only when you have inspected the contents and truly mean to delete the whole tree.
Fix “Permission denied” with rmdir
Permission failures usually come from the parent directory, because removing a directory changes the parent directory entry:
rmdir: failed to remove 'old-empty-dir': Permission denied
Check both the target and its parent:
ls -ld . old-empty-dir
For an empty directory in a system-owned path, use sudo only when you are sure the path is correct:
sudo rmdir /opt/old-empty-dir
For a directory under your home path that was created with the wrong owner, fix ownership first, then remove it normally:
sudo chown "$USER":"$USER" old-empty-dir
rmdir old-empty-dir
Fix “No such file or directory” with rmdir
This error means the path does not exist from the current working directory, or the name was typed differently than the real path:
rmdir: failed to remove 'old-cache': No such file or directory
Confirm your current location and check the exact path:
pwd
ls -ld old-cache
If the directory is somewhere else, use an absolute path such as /home/alex/projects/old-cache. Tab completion helps catch spaces, capitalization, and special characters before you run the command.
Fix “Not a directory” for Symlinks
rmdir does not remove symbolic links to directories because the link itself is not a directory:
rmdir link_to_archive
rmdir: failed to remove 'link_to_archive': Not a directory
Verify that the path is a symlink:
ls -l link_to_archive
Example output:
lrwxrwxrwx 1 user user 7 May 9 10:00 link_to_archive -> archive
Remove the symlink itself with unlink. The target directory remains untouched:
unlink link_to_archive
test ! -L link_to_archive && echo "link removed"
Expected output:
link removed
Conclusion
Empty-directory cleanup is safer when rmdir is the first tool you reach for and rm -r stays reserved for deliberate recursive deletion. Pair it with find for tree cleanup, then use the mkdir command in Linux when rebuilding paths or the mv command in Linux when reorganizing files before deletion.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>