Quick take: rm deletes files and directories permanently. Use rm -rf to remove a non-empty directory recursively and forcefully. Use rmdir only for empty directories. There is no trash — deleted files cannot be recovered with standard tools.
What Is the rm Command in Linux?
The rm command (remove) deletes files and directories from the filesystem. It is one of the most powerful — and most dangerous — commands in Linux, because it bypasses the trash entirely and offers no built-in undo.
Understanding rm properly, including its flags and the contexts where each should and should not be used, is essential for anyone managing Linux systems. A misplaced rm -rf in a script has caused production outages at companies large and small.
Syntax
The basic syntax of the rm command is:
rm [OPTIONS] FILE...
rmdir [OPTIONS] DIRECTORY...rm operates on files by default. To remove directories, you must use the -r (recursive) flag.
rmdir — Remove Empty Directories
rmdir is a simpler command that removes only empty directories. If the directory contains any files or subdirectories, rmdir will refuse with an error. This makes it inherently safer than rm -r.
# Remove a single empty directory
rmdir old_logs
# Remove a hierarchy of empty directories
rmdir -p parent/child/grandchild
# Remove multiple empty directories at once
rmdir dir1 dir2 dir3The -p flag removes the directory and its empty parent directories, working upward until it hits a non-empty one. This is useful for cleaning up scaffolding created with mkdir -p.
Common Options
| Option | Description |
|---|---|
| -r, -R | Remove directories and their contents recursively. |
| -f | Force — ignore nonexistent files and never prompt. |
| -i | Prompt before each removal (interactive mode). |
| -I | Prompt once before removing more than three files or any recursive delete. |
| -v | Verbose — print each file as it is removed. |
| --preserve-root | Do not remove root directory (default on most systems). |
| -d | Remove an empty directory (alternative to rmdir). |
Practical Examples
# Delete a single file
rm report.txt
# Delete multiple files at once
rm file1.txt file2.txt file3.txt
# Delete all .log files in current directory
rm *.log
# Remove a directory and everything inside it
rm -r old_project/
# Force recursive removal without prompts (use with extreme care)
rm -rf /tmp/build_artifacts/
# Interactive: confirm before each deletion
rm -i *.conf
# Verbose: see what is being deleted
rm -rv old_backups/
# Combine find with rm for targeted cleanup
find /var/log -name "*.gz" -mtime +30 -delete
# Delete files matching a pattern recursively using find
find . -name "__pycache__" -type d -exec rm -rf {} +Safe Deletion Practices
Before running any rm command — especially with -rf — take these precautions:
Echo first: Replace rm with echo rm or just ls to preview what would be matched before actually deleting:
# Check what would be deleted before doing it
ls -la /tmp/old_*
# Then, if it looks right:
rm -rf /tmp/old_*Use absolute paths carefully: When writing scripts, never construct deletion paths by concatenating variables without validation. A bug that results in an empty variable can turn rm -rf $dir/ into rm -rf /.
# DANGEROUS — if $BUILD_DIR is empty, this deletes root
rm -rf $BUILD_DIR/
# SAFE — check the variable first
[ -n "$BUILD_DIR" ] && rm -rf "$BUILD_DIR/"Use trash-cli for workstations: On development machines, install trash-cli (Ubuntu: sudo apt install trash-cli) and alias rm to trash to get a recoverable trash bin workflow.
Common Mistakes
The most catastrophic mistake is running rm -rf /path /to/dir with a space after the first path — the shell sees this as two arguments: /path (root) and /to/dir. Modern GNU coreutils protects against rm -rf / with --preserve-root on by default, but other dangerous roots are not protected.
A second common mistake is using * in the wrong directory. Always run pwd first to confirm your current location, then preview with ls *.ext before running rm *.ext.
Finally, never use rm -rf in a cron job or automated script without logging what is being deleted. The -v flag redirected to a log file gives you an audit trail that can be invaluable when debugging unexpected disappearances.
Using rm with xargs and find for Bulk Operations
For complex cleanup jobs — deleting files matching patterns across a directory tree — combine find with xargs rather than using rm glob patterns, which can fail silently if too many files match:
# Delete all .tmp files older than 7 days, safely
find /tmp -name "*.tmp" -mtime +7 -print0 | xargs -0 rm -v
# Delete empty directories under a path
find /var/log/old -type d -empty -delete
# Remove __pycache__ directories recursively
find . -name "__pycache__" -type d -print0 | xargs -0 rm -rf
# Delete files larger than 100MB in a backup directory (preview first)
find /backups -size +100M -type f -ls
# Then delete:
find /backups -size +100M -type f -delete
The -print0 / -0 pair handles filenames with spaces correctly — it uses null bytes as delimiters instead of newlines.
What to Do If You Delete Something Important
Standard rm does not move files to a trash — they are removed from the directory index immediately. Recovery is sometimes possible if the partition has not been heavily written to since deletion:
# Option 1: Check if file is still open by a running process
# (Open file handles survive rm — content accessible via /proc)
sudo lsof | grep deleted | grep filename
# If found open, recover via:
sudo cp /proc/PID/fd/FD /path/to/restore
# Option 2: Use extundelete for ext4 filesystem recovery
sudo apt install extundelete
sudo umount /dev/sda1 # Must be unmounted
sudo extundelete /dev/sda1 --restore-all
# Option 3: Use testdisk for partition-level recovery
sudo apt install testdisk
sudo testdisk /dev/sda
For important files, the best recovery is a recent backup. Always verify backups before you need them.
Safer Alternative: trash-cli
trash-cli provides a trash-based alternative to rm that moves files to the XDG trash directory, allowing recovery:
# Install
sudo apt install trash-cli
# Move to trash instead of permanent delete
trash file.txt
trash -r old_directory/
# List trash contents
trash-list
# Restore a file
trash-restore
# Permanently empty trash
trash-empty
Many teams alias rm to trash in development environments while keeping real rm available for production scripts where trash-fills could cause disk issues.
Common rm Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
| "cannot remove: Is a directory" | Trying to delete a directory without -r | Add -r flag: rm -r dirname/ |
| "cannot remove: Permission denied" | File is owned by another user or is write-protected | Use sudo rm; or change ownership first with chown |
| "argument list too long" | Shell glob expands to more files than the kernel limit allows | Use find . -name "*.log" -delete instead of rm *.log |
rm refuses to remove / | Built-in --preserve-root protection | This is intentional safety — double-check your target path |
Reference: rm man page — man7.org. Tested on Ubuntu 22.04 LTS with GNU coreutils 8.32.
Final Thoughts
rm is irreversible by design — it gives you maximum control at maximum risk. For everyday interactive use, add the -i flag or alias rm to always prompt. For scripts, always validate path variables before passing them to rm -rf. When you just need to clear an empty directory, prefer rmdir for its built-in safety.
FAQ: rm Command in Linux
How do I delete a directory and all its contents with rm?+
Use rm -rf directory_name. The -r flag removes recursively and -f suppresses prompts. Be careful — this is irreversible and there is no undo.
What is the difference between rm and rmdir?+
rmdir only removes empty directories. rm -r removes non-empty directories along with all files inside them. For most real-world cases, rm -r or rm -rf is what you need.
How do I delete files interactively to avoid accidents?+
Use rm -i which prompts before deleting each file, or rm -I which prompts once before deleting more than three files. This is a safer habit for bulk deletes.
Can I recover files deleted with rm?+
Not through standard Linux tools. rm removes files without sending them to a trash folder. Recovery requires filesystem-level tools like extundelete or testdisk, and is not guaranteed.
How do I delete files matching a pattern?+
Use shell globbing: rm *.log deletes all .log files in the current directory. For recursive pattern deletion, combine with find: find . -name '*.log' -delete
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support