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.

Introduction

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 dir3

The -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

OptionDescription
-r, -RRemove directories and their contents recursively.
-fForce — ignore nonexistent files and never prompt.
-iPrompt before each removal (interactive mode).
-IPrompt once before removing more than three files or any recursive delete.
-vVerbose — print each file as it is removed.
--preserve-rootDo not remove root directory (default on most systems).
-dRemove 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.

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