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.

Related Resources

More in This Category
Linux su Command: Switch User Accounts and Login Shells

Linux su Command: Switch User Accounts and Login Shells

Learn how to use the Linux su command to switch user accounts and start login or non-login shells, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Practice creating, copying, renaming, moving, and deleting files and directories in a dedicated folder while checking each result safely.

Linux man Command: Read Command Manual Pages

Linux man Command: Read Command Manual Pages

Learn how to use the Linux man command to find and read command manuals, sections, options, and related pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Compare Files with diff and cmp

Linux Tutorial / Compare Files with diff and cmp

Use diff and cmp to compare two files, read unified diff output, and distinguish a detected difference from an actual command error.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux htop Command: Explore Processes Interactively

Linux htop Command: Explore Processes Interactively

Learn how to explore Processes Interactively with the Linux htop command, including practical examples, key options, and important precautions.

Linux dpkg Command: Inspect and Install Local DEB Packages

Linux dpkg Command: Inspect and Install Local DEB Packages

Learn how to inspect and Install Local DEB Packages with the Linux dpkg command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux modinfo Command: Inspect Kernel Module Details

Linux modinfo Command: Inspect Kernel Module Details

Learn how to inspect Kernel Module Details with the Linux modinfo command, including practical examples, key options, and important precautions.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.