The rm command removes files and directories.
It is powerful and irreversible in normal usage, so safe option selection and path validation are essential.
Overview
Use rm when you need to:
- Delete one or more files
- Remove directory trees recursively
- Clean build artifacts and temporary files
- Perform controlled cleanup in automation scripts
Syntax
rm [options] file...
rm [options] directory...
Common forms:
# Remove a single file
rm old.log
# Prompt before each removal
rm -i important.txt
# Recursively remove a directory tree
rm -r build/
Common Options
| Option | Purpose | Example |
|---|---|---|
-i |
Prompt before every removal | rm -i file.txt |
-I |
Prompt once before removing many files | rm -I *.tmp |
-f |
Force removal; ignore nonexistent files; never prompt | rm -f stale.pid |
-r, -R |
Recursive removal for directories | rm -r dist/ |
-d |
Remove empty directories | rm -d emptydir |
-v |
Verbose output | rm -rv old-cache/ |
-- |
End option parsing for unusual filenames | rm -- -strange-file |
Core Semantics
- By default,
rmremoves files, not directories. - Removing directories requires
-r(or-R) unless directory is empty and-dis used. rmdoes not move items to a recycle bin.-fsuppresses many safety checks and prompts.
Safe Deletion Patterns
Preview before delete:
ls -la *.log
rm -i *.log
Use explicit path anchors for recursive deletes:
rm -rf -- ./build
Handle filenames beginning with - safely:
rm -- -example-file
Examples
# Remove a single file
rm notes.txt
# Interactive delete for critical files
rm -i production.env
# Remove empty directory only
rm -d old-empty-folder
# Recursive delete with verbosity
rm -rv ./tmp/cache/
# Force delete generated artifacts
rm -rf ./dist ./coverage
# Delete files by extension in current directory
rm -i -- *.bak
Script Guidance
Safer cleanup in scripts:
target_dir="./build"
if [[ -d "$target_dir" ]]; then
rm -rf -- "$target_dir"
fi
Recommendations:
- Validate variables before passing to
rm. - Prefer explicit paths (
./dir) over ambiguous relative values. - Avoid constructing dangerous commands from untrusted input.
Troubleshooting
rm: cannot remove 'dir': Is a directory
Cause: Attempted to remove directory without recursive option.
Fix:
rm -r dir
Permission denied
Cause: Missing write/execute permission on parent directory or file restrictions.
Fixes:
- Check permissions with
ls -ldandls -l - Adjust ownership/permissions if appropriate
- Use elevated privileges only when required
Deleted wrong files
Cause: Broad glob or incorrect path.
Prevention:
- Preview with
lsorfindfirst - Use interactive mode for risky operations
- Keep backups for important data
Notes
Use caution with rm -rf.
Many teams adopt aliases such as rm='rm -i' for interactive shells, but scripts should be explicit and predictable.
For command details and implementation-specific behavior, run:
man rm
rm --help