rm Command

The rm command removes (deletes) files and directories in Linux and Unix systems. It's a powerful but potentially dangerous command that permanently deletes files, so it should be used with caution.

⚠️ DANGER: Permanent Deletion

The rm command permanently deletes files! Unlike moving files to trash, rm immediately removes files from the filesystem. Always double-check your commands and consider backups.

Syntax

rm [options] file...

Description

The rm command removes files and directories from the filesystem. By default, it only removes files, not directories. To remove directories, you must use the recursive option.

Key behaviors:

  • Permanently deletes files (no trash/recycle bin)
  • Requires appropriate permissions to delete files
  • Can remove multiple files in one command
  • Supports wildcards for pattern matching

Common Options

Option Description Example
-i Interactive mode (prompt before each removal) rm -i file.txt
-r, -R Remove directories recursively rm -r directory
-f Force removal (ignore nonexistent files) rm -f file.txt
-v Verbose mode (show what's being removed) rm -v file.txt
-d Remove empty directories rm -d empty_dir

Examples

Remove a single file

rm document.txt

Removes the file "document.txt" from the current directory

Remove multiple files

rm file1.txt file2.txt file3.txt

Removes multiple files in one command

Interactive removal (safer)

rm -i *.txt
# Prompts: rm: remove regular file 'file1.txt'? y
# Prompts: rm: remove regular file 'file2.txt'? n

Asks for confirmation before removing each file

Remove directory and contents

rm -r old_project/

Recursively removes directory and all its contents

Force removal without prompts

rm -rf temp_files/

Forces removal without confirmation (use with extreme caution!)

Verbose removal

rm -rv backup/
# Output: removed 'backup/file1.txt'
# removed 'backup/file2.txt'
# removed directory 'backup/'

Shows each file as it's being removed

🚨 EXTREMELY DANGEROUS COMMANDS

NEVER run these commands unless you know exactly what you're doing:

rm -rf /
rm -rf /*
rm -rf ~

These commands can destroy your entire system!

🛡️ Safety Practices

  • Use ls first: List files before removing them
  • Use -i option: Interactive mode for confirmation
  • Double-check paths: Verify you're in the right directory
  • Backup important data: Always have backups
  • Use absolute paths: Be explicit about file locations
  • Test with echo: Use "echo rm ..." to preview commands

⚠️ Common Mistakes to Avoid

  • Typos in filenames: Double-check spelling
  • Wrong directory: Use pwd to verify location
  • Wildcard accidents: Be careful with * and ?
  • Space in commands: "rm file .txt" removes "file" and ".txt"
  • Root privileges: Extra caution when using sudo

Safer Alternatives

  • trash-cli: Move files to trash instead of permanent deletion
  • mv to trash: Move files to a trash directory
  • Backup first: Copy files before deletion
  • Version control: Use git or other VCS for code

See also