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 w Command: Show Logged-In Users and Their Processes

Linux w Command: Show Logged-In Users and Their Processes

Learn how to show Logged-In Users and Their Processes with the Linux w command, including practical examples, key options, and important precautions.

Linux fdisk Command: Inspect and Edit Disk Partitions

Linux fdisk Command: Inspect and Edit Disk Partitions

Learn how to inspect and Edit Disk Partitions with the Linux fdisk command, including practical examples, key options, and important precautions.

Linux nc Command: Test TCP and UDP Connections

Linux nc Command: Test TCP and UDP Connections

Learn how to test TCP and UDP Connections with the Linux nc 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 diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.

Linux join Command: Combine Files by a Common Field

Linux join Command: Combine Files by a Common Field

Learn how to combine Files by a Common Field with the Linux join command, including practical examples, key options, and important precautions.

Linux comm Command: Compare Two Sorted Files

Linux comm Command: Compare Two Sorted Files

Learn how to compare Two Sorted Files with the Linux comm command, including practical examples, key options, and important precautions.

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Understand how the Linux kernel differs from a distribution, what user space and open source mean, and how to check your system's kernel and distribution versions.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

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.