rm

rm linux command cheatsheet by Thamizhiniyan C S

Introduction

The rm command is used to remove files or directories.


Syntax

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


Important Flags

FlagDescription

-f

Forces the removal of all files or directories.

-i

Prompts for confirmation before removing.

-I

Prompts once before removing more than three files or when removing recursively.

-r

Removes directories and their content recursively.

-d

Removes empty directories.

-v

Provides a verbose output.

--help

Displays the help text.

--version

Displays the command version.


Examples

CommandDescription
rm a.txt

Removing one file at a time.

rm b.txt c.txt

Removing more than one file at a time.

rm -d Example

To remove an empty directory.

rm -- [directory name]

To remove a directory whose name starts with a hyphen (-).

rm -- -file.txt

Removes a file whose name starts with a hyphen (-).

rm -r Example

To delete a directory that contains subdirectories and files (Recursive Deletion)

rm -r -v Example

To delete a directory that contains subdirectories and files (Recursive Deletion), and the -v flag to list each step of the process.

rm -r *

-r : Deletes all files and sub-directories recursively in the parent directory.

rm -i d.txt

Asks for confirmation before removing each file (Interactive Deletion).

rm -d -i Example

Use the -i option to display a prompt asking for confirmation before removing a directory.

rm -f e.txt

Overrides write protection and removes the file forcefully (Force Deletion).

rm -d Example

Remove a write-protected directory.

rm -d -f Example

To avoid confirmation when deleting a directory.

sudo rm -d Example

Elevate command privileges to remove a write-protected directory.

rm -rf 

Use this command to remove a write-protected directory that contains other files and directories.

rm --version

Displays the version of rm currently running on the system.

Last updated