rm Command Examples in Linux (2024)

Creating and removing files is one of the most basic yet crucial tasks in computing and when you start embracing the terminal, the first thing you search for is "How to remove files in a Linux terminal".

The answer is you use the rm command to remove files in the Linux command line.

So in this tutorial, I will walk you through how to use the rm command with practical examples.

How to use the rm command in Linux

To use the rm command in the most effective manner, you are recommended to start with its syntax so here's a syntax for the rm command:

rm [options] <File(s)>

Here,

  • [options]: it is used to change the default behavior of the rm command as per your linking such as using the -r flag will remove files recursively.
  • <File(s)>: here's where you specify one or more files to remove.

You get multiple options with the rm command and here's a list of the commonly used ones:

OptionDescription
-r or -RRecursively remove directories and their contents.
-fForcefully remove files without a confirmation prompt.
-iInteractively prompt before removing each file.
-vVerbose mode; displays detailed information about actions.
--preserve-rootAvoid removing the root directory ('/') and its contents.
--no-preserve-rootAllow removing root directory ('/') and its contents.
--helpDisplay summary of options for rm command.

For more details, you can always check the man page of rm command.

Now, let's take a look at some practical examples of the rm command.

1. Remove a file

To remove a file using the rm command, all you have to do is append the name or path to the file to the rm command as shown here:

rm <File or path to file>

For example, if I want to delete the sample.txt file, then I will be using the following:

rm sample.txt
rm Command Examples in Linux (1)

2. Remove multiple files

If you want to remove multiple files using the rm command, then all you have to do is append the target filenames to the rm command as shown here:

rm file1 file2 fileN
rm Command Examples in Linux (2)

Similarly, you can use a wildcard to remove all the files having a similar file type. For example, if you want to remove all the text files at once then you can use the following:

rm *.txt
rm Command Examples in Linux (3)

3. Remove an empty directory

When you try to remove an empty directory with the rm command, it will throw an error message saying "rm: cannot remove: Is a directory":

rm Command Examples in Linux (4)

To remove an empty directory, you'll have to pair the rm command with the -d flag:

rm -d <Directory or path to directory>

Here's how I removed the empty directory:

rm -d empty
rm Command Examples in Linux (5)

4. Remove a directory with files inside

When you try to remove a directory with the -d flag, it will throw an error saying "rm: cannot remove directory: Directory not empty":

rm Command Examples in Linux (6)

So if you want to remove a directory containing files, then you have to use the -r flag. It will instruct the rm command to remove files recursively resulting in the removal of the directory:

rm -r <Directory or path to directory>

Here's how I removed the mydir directory (a non-empty directory):

rm Command Examples in Linux (7)

5. Remove files interactively

While removing multiple files, there are times when you remove the important files accidentally. To tackle this problem, you can interact with the rm command and choose which ones to keep and which ones to remove.

To remove files interactively, you'd have to use the -i flag as shown:

rm -i <Target>

The best use of this option is when you use a wildcard to remove multiple files at once. For example, here I used the -I flag while removing all the text files at once:

rm -i *.txt
rm Command Examples in Linux (8)

6. Remove files forcefully

When you try removing a write-protected file, it will show you a prompt if you want to remove a write-protected file or not:

rm Command Examples in Linux (9)

Sure, you can get away by entering Y to remove and N to not remove the file but if you are removing files recursively, then it can be a tedious task.

To solve this issue, you can use the -f flag to force remove files and it will remove files even if they are write-protected:

rm -f <File(s)>
rm Command Examples in Linux (10)

Want to remove directories only? Use rmdir instead

If you only want to remove directories, then you can use the rmdir command for convenience.

For this purpose, we wrote a detailed guide on how to use the rmdir command in Linux:

Using rmdir Command for Deleting Directories in LinuxLearn everything about the rmdir command in Linux which is used for deleting folders in Linux command line.Linux HandbookAbhishek Prakash

I hope you will find this guide helpful.

rm Command Examples in Linux (2024)

References

Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6337

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.