Use rmdir -- DIR when the directory should be empty; it refuses to remove a non-empty directory. Use recursive rm only when you have confirmed that the exact directory and all its contents should be deleted. GNU rm -I adds a prompt before recursive removal, but no prompt can tell whether you chose the right path.
Choose the command that matches what you intend to remove
| Command | Must the directory be empty? | What it does | Prompt or boundary safeguard |
|---|---|---|---|
rmdir -- DIR |
Yes | Removes the named directory only if it is empty. | Does not recursively remove contents. GNU rmdir documents -- for marking subsequent operands as paths rather than options. |
rm -r -- DIR |
No | Recursively removes the named directory and its contents. | No confirmation prompt is implied by -r. |
rm -I -- DIR |
No | Recursively removes the named directory and its contents. | GNU rm prompts once before a recursive removal. |
rm --one-file-system -r -- DIR |
No | Recursively removes the hierarchy while skipping directories on a different filesystem. | GNU-specific option; it does not protect other directories on the same filesystem. |
GNU-specific options and behavior in this table are documented for GNU Coreutils 9.11. Check man rm and man rmdir on your system; other implementations may differ.
As an Amazon Associate I earn from qualifying purchases.
Confirm the target before deleting anything
Replace DIR in the examples only after identifying the exact path. Inspect the target and its parent with non-destructive commands such as ls -la -- DIR and ls -la -- PARENT, substituting the real paths. Check that the listing shows the directory you mean and that its contents can be discarded. A typo or unexpected expansion can make a destructive command act on a different set of paths than you intended.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Remove an empty directory
When the directory is expected to be empty, run:
rmdir -- DIR
If it contains files or subdirectories, rmdir reports an error instead of removing those contents. That refusal makes it the appropriate choice when you want to remove only an empty directory.
#1 Best Overall
Remove a non-empty directory and everything inside it
After confirming that the whole tree is disposable, a human-operated GNU command can be:
rm -I -- DIR
GNU rm -I asks once before recursive removal. If you have independently confirmed the path and intentionally want no prompt, recursive removal is:
rm -r -- DIR
The -- marks the end of options, so a path that begins with a hyphen is not mistaken for an option. For example, GNU documentation supports rm -- -f to remove a file literally named -f. You can also refer to it as ./-f.
Understand what the safeguards do—and do not do
A prompt is a checkpoint, not path validation
GNU rm -I can give you a final opportunity to stop, but it cannot detect whether the path is a typo or whether its contents are important. Read the prompt and verify the target before answering.
The root protection does not make recursive removal safe
GNU rm refuses to recursively remove / by default. Its manual says --no-preserve-root is not recommended unless you really intend to remove all files on the computer. Do not disable the guard as a workaround for an uncertain target.
Filesystem boundaries matter only when they are on a different filesystem
GNU rm --one-file-system -r -- DIR can skip directories on a different filesystem during recursive removal. This can matter when a hierarchy contains a mount point—for example, GNU’s manual discusses a chroot with /home bind-mounted inside it. The option does not protect a directory on the same filesystem as the tree being removed.
Why wildcards can broaden a deletion
The shell expands wildcards before it runs rm. With rm -r *, for example, the shell first expands * to matching entries in the current directory; recursive removal then applies to directory operands in that list. It does not make rm search arbitrary descendants for names matching a pattern. Because the expansion determines the operands, avoid broad wildcard cleanup when you have not checked exactly what the shell will pass to the command. See the GNU Core Utilities FAQ on wildcard expansion and recursive operands.
Recommended Free Tools
Remove only empty directories beneath a tree
If your goal is to remove empty directories nested under a chosen tree, preview the exact scope first. These commands can act on multiple directories, so do not run a deletion command until the preview matches the tree and directories you intend to target.
GNU Findutils option
Preview empty directories under the selected tree:
find DIR -type d -empty -print
After checking that output, GNU Findutils documents this deletion form:
Rank #4
find DIR -type d -empty -delete
The -delete action is a GNU Findutils feature; check man find for your implementation’s behavior.
POSIX-compatible alternative
Preview the directories that the depth-first command will consider:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsfind DIR -depth -type d -empty -print
When that list matches the intended scope, GNU Coreutils documents this POSIX-compatible removal form:
Best Value
find DIR -depth -type d -exec rmdir {} +
rmdir removes only empty directories, so this approach does not recursively delete files. The preview remains important: the command can remove multiple empty directories under the chosen tree.
What happens if you use the wrong command?
On GNU systems, unlink is not a directory-removal shortcut: it removes one file name and cannot delete the name of a directory. For directory removal, use rmdir or rm according to whether contents should remain.
Deletion recovery is uncertain. GNU’s Coreutils 9.11 manual for rm warns that it is “usually possible to recover the contents” of a file removed with rm. That is not a guarantee, nor a directory-recovery procedure. The outcome depends on factors such as the filesystem, storage, activity after deletion, and whether backups are available. Do not rely on recovery instead of checking the target before deletion.
Check your system’s command documentation
The GNU manuals cited here describe GNU Coreutils 9.11 and GNU Findutils 4.10.0.77-733bb. Options such as rm -I and the find -delete action are implementation-specific. On the system where you plan to run a command, consult man rm, man rmdir, and man find. The GNU references are rm invocation, rmdir invocation, unlink invocation, and Findutils: Deleting Files.
Quick Recap
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.




