• About Us
  • Contact us
  • Disclaimer
  • Privacy Policy

Linuxtor

Linux Tips, Tricks and DevOps Tutorials

  • Home
  • Ubuntu
  • CentOS
  • Debian
  • Commands
  • DevOps
  • Series
You are here: Home / Commands / Explain rm command in Linux with Examples

Explain rm command in Linux with Examples

March 7, 2022 by Asmita Leave a Comment

There are two methods when it comes to removing files or directories in Linux. If you are using Linux GUI distributions, you can delete the file directly by right click and select the “Delete” option. While the command line method for deleting files is a little bit tricky. The command-based method is to use rm command in Linux to remove a file. It is proven to be useful when we have to delete large amounts of data using the command line.

How To Use rm Command In

In this guide, we will explain how you can use the rm command to remove multiple files and directories. We will cover commonly used options available in the rm command.

Table of Contents

  • 1 How to use the rm command
    • 1.1 How to remove multiple files using rm command
    • 1.2 Use rm to delete file matching with regular expressions
    • 1.3 Remove directories using rm command
    • 1.4 Conclusion on using rm command

How to use the rm command

To use the rm command, follow the syntax mentioned below.

rm [OPTIONS] FILENAME

If you by mistake, type the only rm without any option mentioned or a file name, it will not remove any directories or files.

To delete any single file you have to type rm with a filename that you want to delete.

rm filename.zip

In above example, we assumed that the file you want to delete is filename.zip

Once you hit the ENTER button, if the file is successfully deleted the command will not return any output message. If you remove any write-protected files, the command will ask confirmation to whether you want to delete the file or not.

rm: remove write-protected regular empty file 'filename'?

To allow the deletion of write-protected file type y and press ENTER to delete the file. If you want to get rid of this message, use -f option with the rm command.

rm -f filename.zip

To print out the verbose output of the file you want to delete, use -v option with the rm command.

rm -v filename.zip

How to remove multiple files using rm command

It is possible to remove multiple files in Linux using rm command. While the command and syntax have a slight change. Whatever files you want to delete, provide each file name separated by space.

rm filename.zip sample-video.mkv database.csv

Use rm to delete file matching with regular expressions

Let’s assume you want to delete files starting with any word but ends with .db extension. For that rm can be useful to delete the file by extension.

rm *.db

Remove directories using rm command

rm command helps you to delete files as well as directories too. To remove empty directories you have to use -d option with directory name as an argument.

rm -d emptydirectoryname

To remove non-empty directories and it’s all files recursively, use -r option with the rm command.

rm -r nonemptydirectory

Ask for confirmation before removing the file in rm command
Sometimes, it happens like you accidentally delete useful files using rm command. So, to avoid such a disaster, one novice Linux user always uses -i option with the rm command. This always prompts you whether the given file should be deleted or not. Once you confirm by typing Y and press ENTER, it will proceed.

Remove protected directory in Linux without prompts
To remove write-protected directory in Linux without receiving prompts you have to use -rf option together.

Conclusion on using rm command

The rm command is very useful when you have proper knowledge of it and you are aware of what you are doing with it. While using multiple options together with rm command, make sure to check command before you hit ENTER. We suggest you always use -i option to get a prompt before deleting files.

Filed Under: Commands Tagged With: rm command, terminal

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Linux Guides

  • Explain pkill command in Linux
  • Linux File Command, Explain with Syntax and Examples
  • Explain rm command in Linux with Examples
  • What is the /etc/shadow file in Linux
  • How to List Installed Packages on CentOS
  • How To Create a Sudo User in Ubuntu Linux

Read More On

  • CentOS
  • Commands
  • Ubuntu

What is Linuxtor?

Linuxtor.com is your education source to learn more about Linux, Shopify, DevOps and MEAN/MERN Stack development. We are a team of enthuasist pertaining to provide maximum possible guidance on or related to the Linux platform.

Explore about us

  • About Us
  • Contact us
  • Disclaimer
  • Privacy Policy

Copyright © 2023 All Rights Reserved Log in