Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use rmdir DIR when the directory should be empty; it will fail if anything remains. Use recursive rm only when you have confirmed that the named directory and all of its contents should be deleted. Before running a destructive command, inspect the exact path and its contents: prompts and filesystem safeguards cannot tell whether you picked the right target.

Choose the command based on what should be removed

Command Must the directory be empty? What it removes Prompt or traversal safeguard Scope
rmdir DIR Yes The directory itself, only if empty Does not recursively remove contents The named directory
rm -r DIR No The named directory and its contents, recursively No prompt by default; GNU rm -I can prompt once The named directory and its descendant contents
find DIR -type d -empty -delete Finds empty directories Every matching empty directory within the search scope GNU find extension; preview the scope first Potentially multiple nested directories

The examples use GNU utilities where noted. Options and behavior can differ on other systems; check man rm, man rmdir, or man find on your system. GNU Coreutils documents rm in its version 9.11 manual, and rmdir in its version 9.11 manual.

Check the target before removing it

  1. Identify the literal path. Replace DIR in the examples only after you have determined exactly which directory you mean.
  2. Inspect the parent and contents. Use a listing or other non-destructive inspection to verify the directory’s location and what is inside it.
  3. Choose the narrowest command that matches your intent. Use rmdir for an empty directory. Use recursive removal only if the contents should also go.
  4. Read the target again before pressing Enter. A typo or shell expansion can change what the command receives.

Remove one empty directory with rmdir

For an exact directory that should be empty, use:

rmdir -- DIR

If the directory contains files or subdirectories, rmdir reports an error and leaves it in place. That refusal makes it the safer choice when you expect an empty directory and do not want to remove its contents. GNU rmdir accepts -- to mark the end of options; check local documentation if portability matters.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Remove a non-empty directory only when its contents should go too

Recursive removal deletes the named directory and its contents. After confirming the target, the GNU command for a human-operated removal is:

rm -I -- DIR

GNU rm -I (also written --interactive=once) prompts once before a recursive operation. If you have independently verified the target and want no prompt, the recursive form is rm -r -- DIR. The prompt is a checkpoint, not a test of whether the path is correct.

Avoid making rm -rf the default. The -f option suppresses prompts for certain conditions; it does not verify the target or make a mistaken path safe.

When a name begins with a hyphen

A path such as -f might otherwise be interpreted as an option. GNU documentation shows two ways to make the operand unambiguous:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
rm -- -f

Or refer to it through the current directory:

rm ./-f

For a directory, use the same operand-handling principle with the appropriate command and removal mode.

Understand wildcards before using recursive removal

The shell expands wildcards before it starts rm. In rm -r *, for example, the shell passes matching names from the current directory as operands; recursive removal then applies to directory operands in that list. It does not make rm search arbitrary descendant paths for names matching the pattern. Because the expanded list may not match what you had in mind, do not use this as a casual cleanup shortcut. GNU’s Core Utilities FAQ explains this expansion behavior.

Delete empty directories nested under a tree

For bulk operations, preview the exact scope before deleting anything. These commands can affect multiple directories, so ensure that the preview matches the tree you intend to clean.

GNU find with -delete

Preview matching empty directories first:

find DIR -type d -empty -print

If that output contains only the intended targets, run:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
find DIR -type d -empty -delete

The -delete action is documented by GNU Findutils and is an extension; consult the GNU Findutils 4.10.0.77-733bb documentation and your local manual for implementation details.

POSIX-compatible alternative using rmdir

GNU Coreutils also documents a method that runs rmdir on directories found under a tree:

find DIR -depth -type d -exec rmdir {} +

To preview the directory paths first, use:

find DIR -depth -type d -print

Because rmdir removes only empty directories, non-empty ones remain. Review the preview carefully: the search begins at DIR and can select directories throughout that tree.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Know what GNU safeguards do—and do not—protect

Root directory protection

GNU rm protects / by default during recursive removal. Do not disable that guard: GNU’s manual says --no-preserve-root is not recommended unless you really intend to remove all files on the computer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Filesystem boundaries

GNU rm --one-file-system skips directories on a different filesystem while recursively removing a hierarchy. GNU gives the example of a chroot with /home bind-mounted inside it. This can avoid traversing into a separately mounted tree, but it does not protect a directory on the same filesystem as the removal target.

Prompts do not validate the path

A prompt gives you a chance to stop, but it cannot distinguish the intended directory from a typo or an unexpectedly expanded operand list. Confirm the path and scope yourself.

Do not use unlink as a directory-removal shortcut

On GNU systems, unlink removes one file name and cannot delete the name of a directory. It is not a substitute for rmdir or recursive rm. See the GNU Coreutils version 9.11 unlink documentation.

Can a deleted directory be recovered?

Recovery is not guaranteed. The GNU rm manual warns that file contents are often possible to recover, but this is not a promise that a deleted directory can be restored or a directory-recovery procedure. The outcome depends on the filesystem, storage, writes after deletion, and available backups. For important data, keep a backup before running a destructive command rather than relying on recovery.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.