The rm command in Linux

rm removes files and directories. Unlike deleting from a graphical file manager, rm does not move anything to a trash bin: once removed, a file is gone. That makes it worth slowing down for a second before pressing Enter, especially when the command contains -r, -f or a wildcard.

How rm works

rm does not delete data. It calls unlink(), which removes one name from a directory and decrements the file's link count. Only when the count reaches zero — no names left anywhere — does the filesystem mark the blocks free. "Remove" in Unix is precisely "unlink a name", which is why the permission that governs deletion is write permission on the directory, not on the file itself.

There is a second condition for space to be released: no process may still hold the file open. A deleted-but-open file keeps occupying disk until the last file descriptor closes — the classic explanation for "I deleted the huge log but df shows no free space". And recovery after rm is unreliable precisely because the blocks return to the free pool: the next write may claim them at any moment.

directory entries (names)notes.txtsame-file (hard link)inode #48211mode · owner · size · timeslinks: 2data blocksshortcut (symlink)contains a path: "notes.txt"rm deletes a NAME; data frees only when the link count reaches 0 and no process holds it open
The name is not the file: hard links are equal names for one inode; a symlink is a small file holding a path, resolved on access.

Syntax

rm [OPTIONS] FILE...

Common options

OptionWhat it does
-rRecursive: remove directories and everything inside them.
-fForce: never prompt, and ignore files that do not exist.
-iInteractive: ask for confirmation before each removal.
-vVerbose: print each file as it is removed.
-dRemove empty directories (equivalent to rmdir).

How to use rm: examples

$ rm draft.txt

Remove a single file.

$ rm *.log

Remove every file ending in .log in the current directory.

$ rm -r old-project

Remove a directory and all of its contents, recursively.

$ rm -rf build/

Remove a directory tree without any prompts. Powerful and dangerous — double-check the path before running it.

$ rm -i important-*

Ask for confirmation file by file — a good habit when using wildcards.

Real-world use cases for rm

Cleaning build artifacts

Before a fresh build: rm -rf dist/ node_modules/.cache removes generated directories entirely. The -f matters in CI: it stays silent when the directories don't exist yet, so the pipeline doesn't fail on the first run.

Pruning by pattern — carefully

Freeing disk space from old logs: first preview with ls *.log.2025-*, and only when the listing shows exactly what you expect, replace ls with rm. Previewing the glob with ls first is the habit that prevents the famous disasters.

Try rm yourself

This is a live sandbox with a small filesystem (documents/, notes.txt, backup.sh…). Nothing you do here can break anything — experiment freely. Try: rm -r downloads

Pro tips and common mistakes

Frequently asked questions about rm

How do I delete a directory with rm?

Plain rm refuses directories. Use rm -r dirname to remove the directory and everything inside it. For an empty directory you can also use rmdir.

Can I recover a file deleted with rm?

In practice, no — there is no trash bin, and recovery tools are unreliable on modern filesystems. Treat rm as permanent and keep backups of anything important.

What does rm -rf actually mean?

-r removes directories recursively and -f suppresses all prompts and errors. Combined, they delete an entire tree instantly and silently — which is why rm -rf on the wrong path is the classic catastrophic mistake.

Related commands

mkdir

Create one or many directories, including whole nested paths with -p.

cp

Copy files and whole directory trees, preserving attributes when needed.

mv

Move files between directories and rename them — the same operation in Linux.

find

Locate files anywhere in a directory tree by name, type, size, date — and act on them.

Want to build real fluency? The interactive course takes you through rm and every other essential command with guided, checked exercises.