rm
The command that deletes files, and with -r whole directory trees, permanently.
Deletion is permanent, and rm -rf with a mistaken or empty argument can wipe out whole trees.
In more detail
There is no trash can: the name is removed and the space reused. -r recurses into directories and -f never asks and ignores missing files, so rm -rf with an unquoted or empty variable is the classic way to destroy the wrong files. Quote every expansion you pass to it and check the variable is set first.
Examples
rm -r old-build/rm -rf "$work"rm -rf .git deletes a repository's history but leaves the working files.rm works by unlinking: it removes the directory entry you name and decrements the inode's link count. If another hard link exists, or a process still has the file open, the data stays on disk. rm does not overwrite data, and there is no undo, so check the path before running it, especially with -r or -f.Related terms
- rmdir — The command that removes a directory only if it is empty.
- word splitting — The step where bash cuts the result of an unquoted expansion into separate words at spaces, tabs and newlines.
- set -u — The shell option that makes using an unset variable an error that stops the script, instead of silently expanding to nothing.
- recursive option — A command option, usually -r or -R, that makes the command act on a directory and everything inside it, all the way down.
- unlink — The system call that removes one name from a directory and lowers the file's link count.
- hard link — An additional directory entry that points to an existing inode, so one file has two or more equal names.
- link count — The number stored in an inode that says how many directory entries point to it.
- deleted but open file — A file whose last name has been removed but whose data still uses disk space because a process keeps it open.
Taught in
- Making, Copying, Moving And Removing — Bash Scripting & Command Line Automation
- The Unquoted Variable: Splitting And Globbing — Bash Scripting & Command Line Automation
- trap, Cleanup And Temporary Files — Bash Scripting & Command Line Automation
- A Repository Is A Folder With A Memory — Advanced Git Internals & Distributed Workflows
- Ignoring Files, And The Tracked File Trap — Advanced Git Internals & Distributed Workflows
- Inodes, Hard Links And What rm Removes — Linux Fundamentals & Systems Administration
- Providers: How Terraform Reaches Every API — HashiCorp Terraform Associate (004) Exam Prep
- State: Terraform's Memory Of What It Built — HashiCorp Terraform Associate (004) Exam Prep
- Write, Plan, Apply, And What Init Prepares — HashiCorp Terraform Associate (004) Exam Prep
- Custom Conditions: Validation, Pre And Postconditions, Checks — HashiCorp Terraform Associate (004) Exam Prep
- Where Modules Come From — HashiCorp Terraform Associate (004) Exam Prep
- Module Versions: Pin, Upgrade, Re-Initialise — HashiCorp Terraform Associate (004) Exam Prep
- Drift: When Reality Stops Matching State — HashiCorp Terraform Associate (004) Exam Prep
- Moved And Removed Blocks: Refactoring Without Rebuilding — HashiCorp Terraform Associate (004) Exam Prep
- Two Managers Down: The Night The Swarm Froze — Docker Certified Associate (DCA) Exam Prep
- From docker run To docker service: Replicas, Global Mode And Updates — Docker Certified Associate (DCA) Exam Prep
- One File, Five Services: Stacks From Compose Files — Docker Certified Associate (DCA) Exam Prep
- One Edited File, Six Rebuilt Steps: Layers, Cache And Flat Images — Docker Certified Associate (DCA) Exam Prep
- Three Names, One Image: Listing, Formatting, Tagging And Pruning — Docker Certified Associate (DCA) Exam Prep
- Orders That Vanished: Volumes, Shared Storage And Cleanup — Docker Certified Associate (DCA) Exam Prep
- Storage Sprint: Eight Questions On Layers, Drivers And Volumes — Docker Certified Associate (DCA) Exam Prep
- Works On My Laptop: Images, Containers, Volumes And Networks — CompTIA Linux+ (XK0-006) Exam Prep
- The Cleanup That Emptied The Wrong Directory: Shell Scripts Under Exam Conditions — CompTIA Linux+ (XK0-006) Exam Prep
- The Assistant Was Confident: Reviewing AI-Generated Code, Prompts And Data — CompTIA Linux+ (XK0-006) Exam Prep
- No Space Left, Says The Disk With Room To Spare: Inodes, Held Files, Corruption And Repair — CompTIA Linux+ (XK0-006) Exam Prep
- Bridges And Bonds: Two Links, One Interface — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Disk Full At Three In The Morning — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- LDAP Users Through SSSD — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- Mock Exam, Part 2: Real Machines And The Costly Mistakes — Linux Foundation Certified System Administrator (LFCS) Exam Prep
- RPM, YUM, DNF And Zypper — LPIC-1 Linux Administrator (101-500 and 102-500) Exam Prep
- Mock Exam, Part 2: Real Machines And The Patterns That Fail — RHCE in Ansible (EX294): Automate Red Hat Systems
References
Certification courses
- Terraform Associate course and practice exam: HashiCorp Terraform Associate (004)
- DCA course and practice exam: Docker Certified Associate
- CompTIA Linux+ course and practice exam: XK0-006
- LFCS course and practice exam: Linux Foundation Certified System Administrator
- LPIC-1 course and practice exam: Linux Administrator (101-500 and 102-500)
- RHCE course and practice exam: Red Hat Certified Engineer in Ansible (EX294)