# rm — Delete Files and Directories

> Practical guide to rm: delete files and directories, prompt safely with -i/-I, remove recursively with -rf, and avoid disasters like rm -rf /.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/rm/

<!-- PROSE:intro -->
`rm` (remove) deletes files and directories from the filesystem – and it does so for good: unlike moving things to a trash bin, nothing is kept around, and deleted data cannot be recovered without a backup. Day to day you reach for `rm` to clear out temporary files, stale logs or entire project folders. Precisely because it is irreversible, it pays to know the safe variants: `rm -i` asks before each file, while `rm -I` warns once before large or recursive deletions. Take special care with `rm -rf` next to variables or glob patterns – an empty `$VAR` or a stray `/*` can wipe out whole systems.
<!-- PROSE:intro:end -->

## Basic Removal

`rm <file>` — Remove a single file.

```bash
rm temp.txt
```

`rm <file1> <file2> <file3>` — Remove multiple files at once.

```bash
rm old.log debug.log error.log
```

`rm *.tmp` — Remove all files matching a glob pattern.

```bash
rm *.tmp
```

`rm -v <file>` — Verbose mode. Print each file as it is removed.

```bash
rm -v *.log
```

## Interactive & Safe Removal

`rm -i <file>` — Interactive mode. Prompt for confirmation before each file.

```bash
rm -i important-data.*
```

`rm -I <files>` — Prompt once before removing more than 3 files or when removing recursively.

```bash
rm -I *.log
```

`rm --interactive=once <files>` — Same as -I. Prompt once for bulk removals.

```bash
rm --interactive=once /tmp/session_*
```

`rm -f <file>` — Force removal. Never prompt and ignore nonexistent files.

```bash
rm -f lockfile.pid
```

## Directory Removal

`rm -r <directory>` — Remove a directory and all its contents recursively.

```bash
rm -r ./old-project/
```

`rm -rf <directory>` — Force remove a directory recursively without prompts. Use with extreme caution.

```bash
rm -rf ./build/
```

`rm -ri <directory>` — Remove a directory recursively with confirmation for each file.

```bash
rm -ri ./unknown-folder/
```

`rm -d <directory>` — Remove an empty directory. Fails if the directory is not empty.

```bash
rm -d ./empty-dir/
```

`rmdir <directory>` — Remove an empty directory (dedicated command, same as rm -d).

```bash
rmdir ./empty-dir/
```

`rmdir -p <path/to/nested/dir>` — Remove a directory and its empty parent directories.

```bash
rmdir -p ./a/b/c/
```

## Safety Options

`rm --preserve-root` — Refuse to operate recursively on /. This is the default behavior.

```bash
rm -rf --preserve-root /some/path/
```

`rm --preserve-root=all` — Additionally reject any argument that is a mount point.

```bash
rm -rf --preserve-root=all /mnt/data/
```

`rm --one-file-system -r <directory>` — Skip directories on different filesystems during recursive removal.

```bash
rm -rf --one-file-system /var/tmp/
```

## Special Cases

`rm -- <file>` — Remove a file whose name starts with a dash. The -- signals end of options.

```bash
rm -- -strange-filename.txt
```

`rm ./-<file>` — Alternative: remove a dash-prefixed file by prepending ./

```bash
rm ./-strange-filename.txt
```

`rm -f <file_with_special_chars>` — Force remove files with special characters in the name.

```bash
rm -f 'file with spaces.txt'
```

`find <dir> -inum <inode> -delete` — Remove a file by its inode number. Last resort for unremovable filenames.

```bash
ls -i; find . -inum 1234567 -delete
```

## Find & Remove Patterns

`find <dir> -name '<pattern>' -delete` — Find and delete files by name pattern. Faster than -exec rm.

```bash
find . -name '*.tmp' -delete
```

`find <dir> -name '<pattern>' -type f -delete` — Delete only files (not directories) matching a pattern.

```bash
find . -name '.DS_Store' -type f -delete
```

`find <dir> -empty -type d -delete` — Delete all empty directories within a tree.

```bash
find ./project/ -empty -type d -delete
```

`find <dir> -mtime +<days> -delete` — Delete files older than N days.

```bash
find /var/log/ -name '*.log' -mtime +90 -delete
```

`find <dir> -size +<size> -delete` — Delete files larger than a given size.

```bash
find /tmp/ -size +100M -delete
```

`find <dir> -name '<pattern>' -print -delete` — Print each file before deleting it (verification output).

```bash
find . -name '*.bak' -print -delete
```

`find <dir> -name '<pattern>' -print0 | xargs -0 rm -v` — Find and remove files safely with verbose output. Handles spaces in filenames.

```bash
find . -name '*.cache' -print0 | xargs -0 rm -v
```

## Safer Alternatives

`mv <file> /tmp/` — Move to /tmp instead of deleting. Files are cleaned on reboot.

```bash
mv suspicious-file.dat /tmp/
```

`trash-put <file>` — Move file to trash (trash-cli package). Can be recovered later.

```bash
trash-put document.pdf
```

`trash-list` — List all files in the trash (trash-cli).

```bash
trash-list
```

`trash-restore` — Interactively restore a file from the trash (trash-cli).

```bash
trash-restore
```

`trash-empty` — Permanently empty the trash (trash-cli).

```bash
trash-empty
```

`alias rm='rm -I'` — Shell alias to always prompt before bulk deletions.

```bash
alias rm='rm -I'
```

## Dangerous Commands to Avoid

`rm -rf /` — NEVER RUN. Attempts to delete the entire filesystem. Blocked by --preserve-root default.

```bash
# DO NOT RUN — destroys your entire system
```

`rm -rf /*` — NEVER RUN. Bypasses --preserve-root by expanding /* to individual paths. Destroys everything.

```bash
# DO NOT RUN — the glob expansion bypasses root protection
```

`rm -rf $VARIABLE/` — DANGEROUS if variable is empty or unset. Expands to rm -rf / which deletes everything.

```bash
# ALWAYS quote and check: rm -rf "${DIR:?Variable not set}/"
```

`rm -rf "${DIR:?}"/*` — Safe pattern. The :? causes the shell to abort with an error if DIR is empty or unset.

```bash
DIR=/var/cache/app; rm -rf "${DIR:?Variable not set}"/*
```

<!-- PROSE:outro -->
## Conclusion

`rm` is powerful and final: there is no trash bin, and once data is gone it is gone unless you have a backup. For everyday use, `rm <file>`, `rm -r <directory>` and the `find … -delete` patterns cover most needs. Stay aware of the dangerous cases: `rm -rf` with an empty variable, a stray space before `/`, or a `/*` can erase an entire system. Two simple habits protect you – quote and guard variables with `"${DIR:?}"`, and set an `alias rm='rm -I'` so the shell prompts before bulk deletions. When you need recoverability, use `trash-cli` instead of `rm`.

## Further Reading

- [GNU Coreutils: rm](https://www.gnu.org/software/coreutils/manual/html_node/rm-invocation.html) – the official reference for every option
- [rm(1) manual page](https://man7.org/linux/man-pages/man1/rm.1.html) – the Linux man page with concise option descriptions
<!-- PROSE:outro:end -->

## Related Commands

- [mv](https://www.jpkc.com/db/en/cheatsheets/files-text/mv/) – move or rename files instead of deleting them
- [cp](https://www.jpkc.com/db/en/cheatsheets/files-text/cp/) – copy files and directories
- [find](https://www.jpkc.com/db/en/cheatsheets/files-text/find/) – locate files and remove them selectively with -delete

