rm

file managementlinux
The rm command is one of the most frequently used commands in Linux/Unix-like operating systems. rm The rm command is used to remove files and directories in Linux/Unix systems. rm stands for "remove" and it's a powerful command that can delete files, directories, and their contents permanently.

Quick Reference

Command Name:

rm

Category:

file management

Platform:

linux

Basic Usage:

rm filename.txt

Common Use Cases

  • 1

    File removal

    Permanently delete files from the filesystem

  • 2

    Cleanup

    Remove unnecessary or temporary files

  • 3

    Security

    Securely erase sensitive data from disks

  • 4

    Scripting

    Use in shell scripts to remove files programmatically

Syntax

rm [OPTION]... [FILE]...

Options

Option Description
-f, --force Ignore nonexistent files and arguments, never prompt for confirmation
-i Prompt before every removal
-I Prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
--interactive[=WHEN] Prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
--one-file-system When removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument
--no-preserve-root Do not treat '/' specially
--preserve-root Do not remove '/' (default)
-r, -R, --recursive Remove directories and their contents recursively
-d, --dir Remove empty directories
-v, --verbose Explain what is being done
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the rm command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

rm file.txt

Deletes a single file named file.txt from the current directory. This is the most basic usage of the rm command.

rm file1.txt file2.txt file3.txt

Deletes multiple files at once. This example removes three files named file1.txt, file2.txt, and file3.txt from the current directory.

rm -i important.txt

Removes a file with confirmation. The -i (interactive) option prompts for confirmation before removing each file, which is useful for important files to prevent accidental deletion.

rm -f locked.txt

Forces removal without confirmation. The -f (force) option ignores nonexistent files and never prompts for confirmation, which is useful for removing write-protected files without being asked.

Advanced Examples:

rm -r directory/

Removes a directory and its contents recursively. The -r (recursive) option is required when deleting directories, as it tells rm to remove the directory and all files and subdirectories within it.

rm -rf temp/

Forces recursive removal of a directory. Combining -r (recursive) with -f (force) removes directories and their contents without confirmation, even if they contain write-protected files. Use with extreme caution!

rm -v *.log

Removes all .log files with verbose output. The -v (verbose) option displays the name of each file as it's being processed, and the * wildcard matches any file with the .log extension.

find . -name "*.tmp" -exec rm {} \;

Uses find command to locate and remove all .tmp files in the current directory and its subdirectories. This is a powerful combination for targeted file deletion.

rm -- -file-with-dash.txt

Removes a file that starts with a dash. The -- option tells rm that what follows is a filename, not an option, which is necessary for files starting with a dash character.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Important Warnings:

  • The rm command permanently deletes files - they cannot be recovered from the trash/recycle bin
  • Be extremely careful with rm -rf as it can delete entire directories without confirmation
  • Never run rm -rf / or rm -rf /* as these will attempt to delete your entire system
  • Double-check wildcard patterns before pressing Enter to ensure you're only deleting intended files
  • Consider using rm -i when deleting important files to prompt for confirmation

Common Use Cases:

  • Deleting temporary or unnecessary files to free up disk space
  • Removing old log files or backups that are no longer needed
  • Cleaning up after software installations or compilations
  • Removing directories and their contents during cleanup operations
  • Deleting hidden files that start with a dot (rm .*)

Tips:

  • Use ls with the same arguments before running rm to preview what will be deleted
  • When using wildcards, first test with echo to see what files will be matched
  • Create aliases in your shell configuration for safer rm behavior, such as alias rm='rm -i'
  • For beginners, consider using trash-cli or similar tools that move files to trash instead of permanent deletion
  • Use the find command with -delete for more targeted removals with complex criteria

Related Commands:

  • rmdir - Remove empty directories
  • mkdir - Create directories
  • find - Search for files and perform actions on them
  • mv - Move (or rename) files
  • shred - Securely delete files by overwriting their contents

Tips & Tricks

1

Use the -r option to remove directories and their contents recursively

2

Use the -f option to force the removal without prompting for confirmation

3

Use the -i option to prompt before removing each file

4

Use the -v option to display a message for each removed file

5

Use the --one-file-system option to stay on the current file system

Common Use Cases

File removal

Permanently delete files from the filesystem

Cleanup

Remove unnecessary or temporary files

Security

Securely erase sensitive data from disks

Scripting

Use in shell scripts to remove files programmatically

Data management

Manage files and directories in the filesystem

Related Commands

These commands are frequently used alongside rm or serve similar purposes:

Use Cases

1

File removal

Permanently delete files from the filesystem

2

Cleanup

Remove unnecessary or temporary files

3

Security

Securely erase sensitive data from disks

4

Scripting

Use in shell scripts to remove files programmatically

5

Data management

Manage files and directories in the filesystem

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the rm command works in different scenarios.

$ rm
View All Commands