rmdir Command in Linux (With Examples)

Master the Linux rmdir command to safely remove empty directories. Learn practical examples, options, and troubleshooting tips.

Last updatedAuthorJoshua JamesRead time6 minGuide typeLinux Commands

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...
  • OPTION changes how removal is handled, such as parent removal with -p or verbose output with -v.
  • DIRECTORY is one or more directory paths that must already be empty.
  • A successful plain rmdir command prints nothing; errors appear only when a path cannot be removed.

rmdir Quick Reference

TaskCommand PatternWhat It Does
Check availabilitycommand -v rmdirPrints the resolved binary path.
Remove one empty directoryrmdir old-cacheDeletes the directory only if it has no contents.
Remove several empty directoriesrmdir old-cache test-output scratch-spaceProcesses each path and reports failures per directory.
Remove empty parent pathsrmdir -p releases/2026/draftsRemoves the child, then each empty parent.
Show successful removalsrmdir -v old-cachePrints a diagnostic for each directory removed.
Skip non-empty failuresrmdir --ignore-fail-on-non-empty old-cacheSuppresses failures caused only by remaining contents.
Clean empty treesfind 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 manualman rmdirShows 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:

TaskUseWhy
Remove an empty directoryrmdir pathFails safely if anything remains inside.
Remove empty parent pathsrmdir -p path/to/childPrunes empty ancestors without deleting files.
Remove a directory and all contentsrm -r pathDeletes recursively, so inspect the path first.
Remove a symlink to a directoryunlink link_nameDeletes 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 find expression can touch many paths, and -mindepth 1 protects 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
  • -depth visits child directories before their parents, so parent directories can become empty during the same pass.
  • -mindepth 1 keeps project itself out of the result set.
  • -type d passes each directory to rmdir; this lets parents be removed after their empty children disappear.
  • --ignore-fail-on-non-empty keeps non-empty directories from adding noise while still leaving them untouched.
  • -exec rmdir ... {} + batches matched paths into fewer rmdir calls.

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 reports removes 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.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show more of our fresh Linux tutorials in Top Stories and From your sources when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

We read and reply to every comment - let us know how we can help or improve this guide.

Verify before posting: