logo
search
list

Table of Content

Empty Directories: rmdir
Non-Empty Directories: rm -r
Decision Tree
Common Failures
Order of Operations
Special Cases
Verify Success
Safer Alternatives to Immediate Delete
After Removing Directories: Leave a Clear Trail
FAQ
Free Office Download

Remove Directory Linux: rmdir and rm -r

Posted by Algirdas Jasaitis

calendar

2026-07-27

views

869

likes

4

To remove a directory on Linux, use rmdir dirname when it is empty, or rm -r dirname when it still contains files—after you list and back up what matters. Prefer rm -ri while learning. Success means the directory path is gone and you did not delete the wrong tree. Related: delete files, check directory size first.

Shortest safe path for empty dirs: ls -la dirname && rmdir dirname. For non-empty trees: inspect with du -sh dirname and find dirname | head, then rm -ri dirname.

Remove a directory in Linux with rmdir or rm -r
rmdir removes empty directories; rm -r removes directory trees after confirmation habits.

To locate files before you create, copy, move, or delete them, see how to find files in Linux.

Empty Directories: rmdir

mkdir -p demo/empty
rmdir demo/empty
rmdir -p demo/a/b  # removes empty parents when possible

rmdir refuses non-empty directories—that refusal is a feature. If you see “Directory not empty,” something remains (often hidden files). Ask Ubuntu’s classic empty-dir mystery is frequently a leftover dotfile:

ls -la dirname
rm -i dirname/.*  # careful: review names; avoid deleting . and ..
# safer: ls -A dirname

Non-Empty Directories: rm -r

Ask Ubuntu’s highly voted guidance is clear: remove non-empty directories with recursive rm. Use interactive recursion while learning:

rm -ri project-old/

Verbose help for understanding what will go:

rm -rvi project-old/

Warning: rm -rf skips prompts. Only use it after pwd, full path review, and backups. Never copy-paste rm -rf / variants; spacing typos have destroyed systems.

Decision Tree

  • Empty → rmdir
  • Mostly junk but unsure → mv to ~/Quarantine/ for seven days
  • Confirmed disposable tree → rm -ri
  • Huge tree → check size first; delete in stages

Common Failures

  • Directory not empty: hidden files remain—list with ls -la
  • Permission denied: lack write/execute on parent or immutable attributes
  • Device busy: shell cwd or process inside the directory—cd out first
  • Read-only filesystem: mount issue, not an rm flag problem
pwd
cd ~
ls -la /path/to/dir
rmdir /path/to/dir || rm -ri /path/to/dir

Order of Operations

  1. Confirm path with absolute names.
  2. du -sh to understand impact.
  3. Backup or move keepers (cp/mv).
  4. Delete files first if you want finer control (rm files).
  5. Remove the directory.
  6. Verify with ls and application health checks.

Special Cases

Git repos: prefer proper cleanup; do not casually rm -rf .git unless you intend to destroy history. Container bind mounts: deleting from the wrong side of a mount can wipe host data—verify mounts with mount | grep.

Directories with only a .gitkeep or placeholder still count as non-empty for beginners who forget dotfiles—list thoroughly.

Verify Success

test ! -d project-old && echo "directory removed"
df -h .

Safer Alternatives to Immediate Delete

mkdir -p ~/Quarantine
mv project-old ~/Quarantine/
# later:
# rm -ri ~/Quarantine/project-old

Quarantine works because it buys time for memory to return: “wait, that folder had the license key.” Combine quarantine with a calendar reminder to finish the delete later. Unfinished quarantine directories should be reviewed monthly so they do not become a second junk drawer.

Recursive deletes on networked home directories can be slow and hard to abort cleanly. Prefer deleting heavy children first after measuring with du. Watching progress via rm -rv on a known subtree is safer than a silent -rf against an entire mount.

Immutable bits (chattr +i on ext filesystems) block removal until cleared—another reason “force” is the wrong first answer. Investigate attributes and mounts before escalating privileges. Root does not fix a read-only NFS export.

Document every production directory removal in the change ticket with path, size, and backup location. Future you will thank present you when auditors ask why capacity suddenly appeared.

Empty-looking directories that refuse rmdir sometimes contain sockets, weird names, or mounts. Use find dirname -xdev -print to see entries without crossing into other filesystems accidentally. If a mount point sits inside the tree, unmount first—deleting across mounts is a classic self-own.

Team shared drives need social process: propose the remove, wait for objections, then delete. Technical skill does not replace communication. A perfectly executed rm -r on the wrong shared dataset is still an incident. Write the proposed path in the ticket title so reviewers cannot miss it during busy on-call weeks, and paste absolute paths copied from pwd—not from memory. Read the path aloud once before you press Enter on any recursive flag, then pause and read it a second time.

After removal, check services that used that path. Web roots, batch jobs, and container volumes may need config updates or redeploys. Deleting the directory is step one; restoring service health is step two. Schedule a short smoke test immediately after the change window closes.

On Linux, remove empty directories with rmdir and non-empty trees with careful rm -r—list hidden files, measure size, and quarantine when unsure before any -f shortcut.

Practice destructive commands only inside a throwaway tree such as ~/tmp-practice. Create sample files with touch, copy them, then delete the copies—not your only originals. Run ls -la before and after every experiment. Ten calm minutes of drills beat one panicked recovery attempt.

Quote paths, prefer -i while learning, and never run recursive deletes against /, ~, or unreviewed globs. Most disasters are one misplaced space or an expanded * away. Slow down on production hosts; go fast only where rebuild is cheap. If a command feels clever, it is probably unsafe without a dry run.

Read rm(1), rmdir(1), and du(1) once. You do not need every flag memorized—you need to know where to look when an error is unfamiliar. Official man pages beat viral “one weird tip” posts that skip warnings and sell false confidence.

On shared machines, announce bulk deletes in chat. Cron jobs and teammates may still expect yesterday’s paths. Prefer moving into an Archive folder for a week before permanent rm when you are unsure. That quarantine habit has saved more teams than any force flag ever will.

Before you delete project folders that hold drafts, export finished DOCX, XLSX, or PDF copies you still need for school or work. Terminal cleanup is permanent on most systems—office-ready exports are your safety net when Trash is not available.

WSL users should delete inside the Linux filesystem for project trees. Cross-deleting under /mnt/c works but follows Windows permissions and can surprise you with locked files. Confirm with pwd before recursive flags. Keep Windows Explorer closed on files you plan to remove from WSL.

Keep a personal checklist: list → measure size if needed → backup or move → delete → verify gone. That order turns risky commands into a boring routine—which is exactly what you want on Friday afternoons and during incidents alike.

Teach juniors the same order out loud before they receive sudo. Narrating pwd, ls, and intended targets catches typos that eyes skip when someone is stressed. Pair programming beats postmortems.

Finally, separate “cleanup” from “secure erase.” Everyday rm/rmdir/du workflows reclaim space and reduce clutter. Classified-data destruction policies require approved wipe procedures beyond this beginner series—follow your organization’s standards when those rules apply.

Cross-link your personal notes to the sibling skills: create, copy, move, and rename. File hygiene is a loop, not a single command. Operators who practice the whole loop make fewer irreversible mistakes when disks fill at inconvenient times.

If you remember only one sentence, make it this: measure and list before you destroy, and verify after. That sentence covers delete file, remove directory, and directory sizing better than any force-flag folklore circulating in chat threads.

After Removing Directories: Leave a Clear Trail

rmdir and rm -r reclaim space fast. Project handoffs still benefit from a short note of which trees were retired and where backups live.

WPS Office for Linux turns that trail into Writer notes, Spreadsheet inventories, or PDF summaries teammates can open without reconstructing your shell history.

WPS Office Writer on Linux documenting file copy, move, and archive steps in a DOCX log
WPS Writer on Linux: log file operations, zip inventories, and cleanup notes as a readable document.
What you need nextWhat WPS gives you
Record directory removals and backup locationsStrong .docx / .xlsx / .pptx layout fidelity when files move between Linux, Windows, and macOS
One place for notes, sheets, slides, and PDFsFree core suite: Writer, Spreadsheet, Presentation, plus PDF view/edit/convert tools
A familiar desktop UI for switchersRibbon-style apps with a lighter footprint than many heavy suites
A trusted install pathOfficial DEB (Ubuntu/Debian/Mint) and RPM packages from the vendor

Install via the official WPS Office for Linux download page with sudo apt install ./wps-office_VERSION_amd64.deb, then store the retirement note next to any archive you kept.

FAQ

Why rmdir says directory not empty when ls shows nothing?

Hidden entries exist. Use ls -la or ls -A.

Is rm -rf the standard way?

It is common but dangerous. Prefer rm -ri until you are expert and path-verified.

Can I remove multiple empty dirs?

rmdir dir1 dir2 dir3

How do I remove a directory I am inside?

cd out first; otherwise you may get busy errors.

Does rm -r delete symlinks to other trees?

It removes the symlink itself by default, not usually the target tree—still verify with ls -l before recursion.

What about trash-cli?

Optional desktop tooling; servers often lack it. Do not assume Trash exists over SSH.

How do I protect a directory from deletion?

Permissions, immutable attributes, and backups—plus not running as root casually.

Should I sudo rm -r?

Only with a documented reason and backups. sudo multiplies mistakes. If permissions block a normal-user delete, understand ownership before escalating.

Removing directories safely is mostly process: list, measure, quarantine, remove, verify. Tools are simple; judgment is the hard part. Build that judgment in practice folders before you touch production data that pays salaries. When in doubt, mv to Quarantine today and decide tomorrow with a clearer head—patience is a valid ops tool, not a delay tactic, and it prevents irreversible mistakes on bad days.

Free Office Download

WPS WriterWPS PresentationWPS SpreadsheetWPS PDF
  • Use Word, Excel, and PPT for FREE, No Ads.

  • Edit PDF files with the powerful PDF toolkit.

  • Microsoft-like interface. Easy to learn. 100% Compatibility.

  • Boost your productivity with WPS's abundant free Word, Excel, PPT, and CV templates.

100% secure
Algirdas Jasaitis

15 years of office industry experience, tech lover and copywriter. Follow me for product reviews, comparisons, and recommendations for new apps and software.