Enter your email address below and subscribe to our newsletter

How to use rm command in linux

Learn how to remove files and directories in Linux using the rm command.

Share your love

When you start doing everything in the terminal, removing files and directories is one of the most basic yet important tasks you’d perform on a daily basis.

But the question is how do you remove files and directories in Linux?

The answer is the rm command.

So in this tutorial, I will walk you through how you can use the rm command in Linux to remove files and directories through multiple examples.

Here’s how to use the rm command

To use the rm or any other command, you are advised to start with the syntax and the available options. So here’s a simple syntax that you need to follow to use the rm command:

rm [options] [file(s)/directory(ies)]

Here,

  • [options]: it is used to change the default behavior of the rm command. For example, you can use the -f option for the forced removal of the file (without prompting for confirmation).
  • [file(s)/directory(ies)]: here, you specify the name of one or more files/directories that you want to remove separated by spaces.

Now, let’s take a look at the options you get with the rm command:

OptionDescription
-r or -RDelete directories and their contents recursively.
-fForce deletion without confirmation, even for write-protected files.
-iPrompt for confirmation before deleting each file.
-vDisplay informative messages as files are being deleted.
-dDelete empty directories.
-IPrompt for confirmation once before deleting 3 or more files, or when deleting recursively.
--preserve-rootDo not delete the root directory (/) to prevent accidental system damage.
--one-file-systemWhen deleting a directory recursively, skip files/directories on other mounted file systems.
--no-preserve-rootAllow deleting the root directory (/), which can potentially damage the system.
--helpDisplay help information about the rm command and its usage.
--versionDisplay the version of the rm command.

Practical examples of the rm command in Linux

In this section, I will walk you through some useful examples of the rm command so that you can learn the command with practical scenarios.

Let’s start with the basic one.

1. Delete a file using the rm command

To remove a single file, all you have to do is append the filename or the path to the filename to the rm command and it will remove the specified file:

rm filename

For example, if I want to remove the GG.txt file, then I will use the following command:

rm GG.txt
Delete a file using the rm command in Linux

2. Delete multiple files using the rm command

If you want to remove multiple files at once, append the name of the multiple files to the rm command followed by space as shown here:

rm file1 file2 file3

Let’s say I want to remove File1.txt, File2.txt and File3.txt in the current working directory, so I will use the following command:

rm File1.txt File2.txt File3.txt
Delete multiple files using the rm command in Linux

3. Delete a directory using the rm command

If you try deleting a directory using the rm command without any additional option (like you did with files), it will give an error saying

can not remove, it is a directory error

No, it does not mean that you can not remove a directory using the rm command. It simply means that you need to use a different flag for this purpose.

To remove a directory, you need to remove files recursively which means it will remove everything inside of the given directory resulting in the removal of a directory. For that purpose, you use the -r as shown here:

rm -r directory_name

Here’s how I removed mydir directory using the -r flag:

rm -r mydir
Delete directory using the rm command in Linux

4. Delete files interactively

While removing multiple files at once, you may remove important files accidentally and they can not be recovered. But you can use the rm command to give you an interactive prompt asking if you want to remove the specific file or not.

To remove files interactively, you use the -i flag as shown:

rm -i File(s)/Directory(ies)

For example, here, I removed the File2 file using the interactive prompt:

rm -i File2
Remove files interactively using the rm command

5. Delete files forcefully

When you try removing write-protected files, it will prompt you to ask if you want to remove a file or not. And answering each prompt while removing multiple files is not efficient.

For example, when I tried removing write-protected.txt, it gave me the following prompt:

Prompt when removing write-protected file using the rm command

To bypass the prompt, you can use the -f option to force the removal of files:

rm -f File(s)/Directory(ies)

Here’s how I bypassed the prompt when removing write-protected.txt file:

rm -f write-protected.txt
Remove write-protected files using the rm command in Linux

6. Prompt once before deleting more than 3 files

Previously, I explained how you can remove files interactively. But what if you have hundreds of files to be removed? I mean you would not want to answer each prompt.

To get away with this, you can use the -I flag and it will only prompt once if you’re deleting files that are more than 3 in number:

rm -I File(s)

For example, here, I used the -I flag when removing all the files of a current directory:

rm -I *

7. Use regular expressions to delete files in Linux

Regular expressions are mostly used when you want to delete files that follow a pattern in their names. Sure, there are infinite possibilities of how you can use regular expressions but here, I will only share a few.

Delete files with a specific extension

If you want to remove files that have the same extension then you can use the extension as a wild card to remove everything all at once:

rm *.extension

For example, if I want to remove every text file inside my current directory, then I’ll use the following:

rm *.txt
Remove multiple files having a same extension using the rm command in Linux

Delete files with specific patterns in their names

This is my personal favorite where I can specify a pattern that my target files follow. Sounds complex? Let me help!

For example, if I want to delete each file starting with File, then I can use the following to remove all of them at once:

rm File*
Delete files starting with a specific pattern using the rm command in Linux

Furthermore, if you want to delete files using a common term that comes anywhere in their names, then you can use the pattern in the following manner:

rm *common-term*

Let’s say I want to remove all the files that have the word “backup” anywhere in their names, then I’d the rm command in the following manner:

rm *backup*
Delete multiple files that contains the same term in their names using the rm command in Linux

Conclusion…

In this tutorial, I went through how you can use the rm command to remove files and directories in Linux including syntax, common options, and multiple examples.

Sure, you can use the rm command with other commands to form a robust expression but this tutorial was intended to get you a basic idea of how you can use the rm command.

I hope you will find this guide helpful.

If you have any queries or suggestions, feel free to leave us a comment.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!