The “rm” Command in Linux [7 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The rm command is one of the handiest commands in Linux. It is used to remove files and directories without leaving the terminal. You can modify the rm command with various specific options to permanently remove different contents in different scenarios. Hence, as a Linux enthusiast, you should learn in detail about the rm command in Linux.

A. Description

In Linux, rm is the abbreviation for remove. As the name suggests it removes contents(i.e. files, directories) and the removal is permanent.

WARNING: When you remove something using the rm command, it is removed permanently. You cannot retrieve it by any means. So, Better be careful while using the rm command in Linux.

B. Syntax

The syntax for the rm command is pretty simple which is the command itself followed by some specific options and the file/directory name you want to remove.

rm [OPTION]... [FILE]...
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time.

C. Options

Many useful options are available for the rm command. I have listed some of the most used ones here. However, if you want to know more about options for the rm command you can always check the man page for the rm command.

man rm

Useful Options

  • -d or –dir, Removes an empty directory
  • -f, Removes nonexistent files or ignores prompt.
  • -i, Displays an interactive prompt
  • -I, Similar to the -i option but less prominent as only shows a prompt for every three files or recursive removal.
  • -r or -R or –recursive, Removes the contents of a directory recursively.
NB: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of “rm” Command in Linux

The rm command in Linux is an easy-to-use command which is used to remove contents. Some of the most useful applications of the rm command have been illustrated below. In this article, I have worked with the following directory hierarchy and files.

The directory hierarchy and files I have worked with in this article.

Example 1: Removing a File Using the “rm” command in Linux

I have a directory named “Parent_Directory” on my Desktop. This directory contains two sub-directories and a file named “file.txt”.The desired file.To remove the file using the rm command you can follow the below procedure.

Steps to Follow >

➊ First, open the Ubuntu Terminal inside the directory named “Parent_Directory”.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

rm file.txt

➌ Now, press the ENTER button.

Output >

You can see in both images that the file.txt file is removed. In the first image, I have listed the contents of a directory using the ls command both before and after removing the content.The desired file has been removed by using the rm command in Linux.And, In the 2nd image, you can see from the Graphical User Interface(GUI) that the file has been removed from the directory.The desired file has been removed by using the rm command in Linux(GUI representation).

Example 2: Removing Multiple Files

I have a sub-directory named “General” inside the “Parent_Directory” directory located on my Desktop. This directory contains three files as shown in the following image.The desired files.To remove these 3 files using the rm command you can follow the below procedure.

Steps to Follow >

➊ First, open the Ubuntu Terminal inside the “General” directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

Alternatively, If you’re already in the “Parent_Directory” from the previous example you can just type the following command in the command prompt to jump into the “General” directory:

cd NonEmpty_Directory/Contents/General

Changing directory.➋ Type the following command in the command prompt:

rm EmptyFile1 EmptyFile2 EmptyFile3

➌ Now, press the ENTER button.

Output >

You can see in both images that our desired 3 files are removed. In the first image, I have listed the contents of a directory using the ls command both before and after removing the content.Multiple files have been removed by using the rm command in Linux.And, In the 2nd image, you can see from the Graphical User Interface(GUI) that the files have been removed from the directory.Multiple files have been removed by using the rm command in Linux(GUI View).


Similar Readings


Example 3: Using the “rm” Command to Remove an Empty Directory

I have a directory named “Parent_Directory” on my Desktop. This directory contains an empty folder named “Empty_Directory”.

The desired directory.

To remove the empty directory using the rm command with the -d option you can follow the below procedure.

Steps to Follow >

➊ First, open the Ubuntu Terminal inside the “Parent_Directory” directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

rm -d Empty_Directory

➌ Now, press the ENTER button.

Output >

You can see in both images that the desired directory is removed. In the first image, I have listed the contents of a directory using the ls command both before and after removing the content.The desired empty directory has been removed by using the rm command in Linux.And, In the 2nd image, you can see from the Graphical User Interface(GUI) that the desired empty directory has been removed.The desired empty directory has been removed by using the rm command in Linux(GUI view).

Example 4: Using the “rm” Command to Remove a Nonempty Directory

I have a sub-directory named “Contents” in the directory named “Parent_Directory” located on my Desktop. Which contains a nonempty directory named “Graphics”.The desired non-empty directory.To remove this non-empty directory using the rm command with the -r option you can follow the below procedure.

Steps to Follow >

➊ First, open the Ubuntu Terminal inside the “Contents” directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

Alternatively, If you’re already in the “Parent_Directory” from the previous examples you can just type the following command in the command prompt to jump into the “Contents” directory:

cd NonEmpty_Directory/Contents

➋ Type the following command in the command prompt:

rm -r Graphics

➌ Now, press the ENTER button.

Output >

You can see in both images that the desired directory is removed. In the first image, I have listed the contents of a directory using the ls command both before and after removing the content.The desired non-empty directory has been removed by using the rm command in Linux.And, In the 2nd image, you can see from the Graphical User Interface(GUI) that the desired non-empty directory has been removed.

The desired non-empty directory has been removed by using the rm command in Linux(GUI view).

Example 5: Prompting Before Removing

You might’ve already learned that you can’t retrieve anything once you’ve removed it using the rm command. Therefore, you can use the -i option to prompt a warning system by following the below process.

Steps to Follow >

➊ First, open the Ubuntu Terminal inside the “Parent_Directory” directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

rm -ri NonEmpty_Directory

➌ Now, press the ENTER button.

Output >

You can see in the following image that an interactive message prompts up. You can type “Y” to commence the task or “N” to move to the next task.Enabling interactive promt.


Similar Readings


Example 6: Using the “rm” Command to Remove Contents by Ignoring the Prompt

I have a sub-directory named “NonEmpty_Directory” in the directory named “Parent_Directory” located on my Desktop.The desired contents.Now, I want to remove the “NonEmpty_Directory” directory itself with all its contents. Like the previous example, I’ll use the -i option to prompt a warning system. However, to ignore confirmation every time I’ll forcefully remove the contents using the -f option as well. By following the below process you will be able to do so.

Steps to Follow >

➊ First, open the Ubuntu Terminal inside the “Parent_Directory” directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

rm -rif NonEmpty_Directory

➌ Now, press the ENTER button.

Output >

You can see in the following image that an interactive message didn’t prompt up. However, everything was deleted.The desired contents have been removed by using the rm command in Linux.

Example 7: Using the “rm” Command to Remove Write-Protected Contents

You cannot remove a write-protected file or directory even with the -f(force) option enabled. I have a directory named “Parent_Directory” on my Desktop.The desired directory. Now I’ll make it write-protected and then remove it. By following the below process you will be able to do so.

Steps to Follow >

➊ First, open the Ubuntu Terminal in the Desktop directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ To make the directory write-protected by removing the write permission, type the following command in the command prompt:

chmod a-w Parent_Directory

➌ Now, press the ENTER button.

From the below image, you can see the write permission of the user and group has been revoked for the desired directory.Altering permission.➍ Finally to remove it type the following command in the command prompt:

sudo rm -r Parent_Directory

➎ Now, press the ENTER button.

Output >

You can see in both images that the desired directory is removed. In the first image, I have listed the contents of a directory using the ls command both before and after removing the content.The desired restricted directory has been removed by using the rm command in Linux.And, In the 2nd image, you can see from the Graphical User Interface(GUI) that the desired directory has been removed.The desired restricted directory has been removed by using the rm command in Linux(GUI view).

Conclusion

In this article, I have tried to present you the applications and usefulness of the rm command in Linux. Hope that you’ll be able to explore more things with the help of these practical examples.  However, always be careful while using the rm command with important files as you can not bring back the removed ones.


Similar Readings

Rate this post
Md. Ashakul Islam Sowad

Hi, I am Md. Ashakul Islam Sowad from Dhaka, Bangladesh. I have completed my undergraduate degree in Biomedical Engineering from the Bangladesh University of Engineering and Technology (BUET). I love to watch football and play video games in my free time. Here, I am working as a Linux Content Developer Executive. Furthermore, as a Linux enthusiast, I am always learning new things about Linux-based systems and I’ll be sharing them here. Read Full Bio

Leave a Comment