Skip to content

rm Linux Command Guide

The Linux rm command is used to remove files or directories. It is a powerful tool that can help you manage your filesystem effectively. By using rm, you can delete unwanted files or directories quickly and efficiently. Keep in mind that the rm command is irreversible, so make sure you want to permanently delete the files or directories before using it.

rm Syntax:

Terminal window
rm [options] [parameters]

Options:

OptionDescription
-fIgnore nonexistent files and arguments
-iPrompt before every removal
-rRemove directories and their contents
-vExplain what is being done
-dRemove empty directories
-RRecursively remove directories and their contents
-IPrompt once before removing more than three files, or when removing recursively.
-lRemove only symlinks
-POverwrite regular files before deleting them
-WAttempt to undelete deleted files
-wIf an attempt to delete fails, wait until the system is ready to delete the target
file
-xStay on the same filesystem
-helpDisplay this help and exit
Signals the end of options, options following this will be interpreted as parameters

Parameters:

ParameterDescription
fileFiles to be removed
directoryDirectories to be removed
patternFile patterns using wildcards to match files

rm Command Samples:

Remove a Single File

Terminal window
rm filename.txt

This command removes the file named “filename.txt” from the system.

Remove Multiple Files

Terminal window
rm file1.txt file2.txt file3.txt

Deletes multiple files at once, removing “file1.txt”, “file2.txt”, and “file3.txt” from the system.

Forcefully Remove a File

Terminal window
rm -f filename.txt

Forcibly removes the file “filename.txt” without prompting for confirmation.

Remove a Directory and Its Contents

Terminal window
rm -r directory_name

Recursively removes the directory named “directory_name” along with all its contents.

Remove Empty Directories

Terminal window
rm -d empty_directory

Deletes the empty directory named “empty_directory” from the system.

Remove Files Older Than a Specific Date

Terminal window
rm -R --time=202202010000.00 *

Deletes files modified before February 1, 2022, at midnight in the current directory.

Remove All Files Except One

Terminal window
rm -v !(file_to_keep.txt)

Removes all files in the directory except for the file named “file_to_keep.txt”.

How do I use rm in Linux?

To use the rm command in Linux, execute the following command:

Terminal window
rm file.txt

How can I force the deletion of files using rm in Linux?

You can force the deletion of files using the “-f” option with the rm command. Here is an example:

Terminal window
rm -f file.txt

How can I delete a directory and its contents in Linux using rm?

To delete a directory and its contents recursively, use the “-r” option with the rm command. Here is an example:

Terminal window
rm -r directory_name

How can I delete multiple files at once in Linux using rm?

To delete multiple files at once in Linux, you can specify the filenames separated by a space after the rm command. Here is an example:

Terminal window
rm file1.txt file2.txt file3.txt

How can I delete files and directories interactively using rm in Linux?

To delete files and directories interactively, you can use the “-i” option with the rm command. Here is an example:

Terminal window
rm -i file.txt

How can I remove a directory without prompting using rm in Linux?

To remove a directory without being prompted for confirmation, you can use the “-rf” options together with the rm command. Here is an example:

Terminal window
rm -rf directory_name

Applications of the rm command

  • Deleting files
  • Removing directories
  • Deleting multiple files at once
  • Deleting files silently
  • Removing files without confirmation
  • Deleting read-only files
  • Deleting hidden files
  • Removing files based on file type
  • Deleting files based on last modified time
  • Deleting empty directories
  • Removing files using regular expressions