Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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.
Table of Contents
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:
Option | Description |
---|---|
-r or -R | Delete directories and their contents recursively. |
-f | Force deletion without confirmation, even for write-protected files. |
-i | Prompt for confirmation before deleting each file. |
-v | Display informative messages as files are being deleted. |
-d | Delete empty directories. |
-I | Prompt for confirmation once before deleting 3 or more files, or when deleting recursively. |
--preserve-root | Do not delete the root directory (/) to prevent accidental system damage. |
--one-file-system | When deleting a directory recursively, skip files/directories on other mounted file systems. |
--no-preserve-root | Allow deleting the root directory (/) , which can potentially damage the system. |
--help | Display help information about the rm command and its usage. |
--version | Display the version of the rm command. |
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.
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
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
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
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
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
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:
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
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 *
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.
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
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*
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*
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.