rm Command in Linux with Examples

Today you will learn How to use rm command in Linux.

rm stands for Remove.

As you can understand by the name itself, you can use the rm command to remove Files or Directories.

By default, rm does not remove directories.

Therefore, to remove the directories, you have to use -r or -R option which I have explained in this guide.

Features of rm Command:

  • Removes each specified File.
  • Removes empty Directories.
  • Removes Directories and their contents recursively.
  • Prompt before every Removal.
  • Removes each specified File/Directory Forcefully. In this case, the command will never prompt before removal.
  • Displays Verbose output.

In this article, I will teach you about the complete features of the rm command with examples.

Let’s get Started.

First of all, let’s focus on some of the most important options that we can use with the rm command.

OptionsExplanation
-f, --forceRemoves each specified File/Directory Forcefully. Never prompt before every removal.
-iPrompt before every removal.
-IPrompt once before removing more than three files, or when removing recursively.
-r, -R, --recursiveRemove directories and their contents recursively.
-d, --dirRemove empty directories.
-v, --verboseDisplay the output message.
--helpDisplay help page of rm Command.
--versionCheck the version of rm command.

You must follow the syntax given below to use the rm command.

rm [OPTION]... [FILE]...

1. Remove a File

To remove a file type the following command.

~$ rm file1.txt

2. Remove multiple Files

To remove multiple files, pass the names of the files to rm command.

In this example, I am deleting three files named file2.txt, file3.txt, and file4.txt.

~$ rm file2.txt file3.txt file4.txt

3. Remove empty Directories

To remove an empty directory pass the -d option to rm.

~$ rm -d data/

To remove multiple empty directories, type the following command.

In this example, I am deleting three directories named data, data1, and data2.

~$ rm -d data/ data1/ data2/

You can also remove empty directories using the rmdir command.

Suggested Read: mkdir Command : How to Create a Directory in Linux

4. Display the output message

To display output message for every removal pass -v option to rm command.

Ex #1 Remove a file

~$ rm -v file.txt 
removed 'file.txt'

Ex #2 Remove a Directory

~$ rm -rv data/
removed directory 'data/'

5. Prompt before every removal

To get prompt before every removal of specified file/directory pass the -i option to rm.

Ex #1 Remove a file

~$ rm -i file.txt 
rm: remove regular empty file 'file.txt'? y

Ex #2 Remove multiple files

~$ rm -i file1.txt file2.txt file3.txt file4.txt file5.txt 
rm: remove regular empty file 'file1.txt'? y
rm: remove regular empty file 'file2.txt'? y
rm: remove regular empty file 'file3.txt'? y
rm: remove regular empty file 'file4.txt'? y
rm: remove regular empty file 'file5.txt'? y

Ex #3 Remove multiple directories

~$ rm -di data1/ data2/ data3/
rm: remove directory 'data1/'? y
rm: remove directory 'data2/'? y
rm: remove directory 'data3/'? y

According to me whenever we delete a file or directory using this command, we should always use the option -i.

If you feel that you will forget, you can set an alias for this command.

~$ alias rm="rm -i"

6. Prompt once before removal

If you want the rm command to give a prompt once before removing more than three files OR when removing recursively, in that case, you can use the -I option.

~$ rm -I file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt 
rm: remove 6 arguments? y

7. Remove a directory Recursively

What is Recursive?

Recursive means the executed command in Linux will work on a complete directory tree.

As per the following scenario, if I run the rm command to delete the contents of the directory dir1 recursively, it will delete the contents of all three directories (dir1, dir2 & dir3).

Directory Tree

You can use any of these options -r or -R with rm command to remove directory Recursively.

I am combining the -v option to display the output.

Let’s take some examples:

Example #1

To remove the directory named data recursively pass the -r option to rm.

~$ rm -rv data/
removed directory 'data/data3'
removed directory 'data/data2'
removed directory 'data/data1'
removed directory 'data/'

Example #2

you can also use the -R option to remove a directory recursively.

~$ rm -Rv data/
removed directory 'data/data3'
removed directory 'data/data4'
removed directory 'data/data2'
removed directory 'data/data5'
removed directory 'data/data1'
removed directory 'data/'

8. Remove multiple directories Recursively

To remove multiple directories recursively pass the -r option to rm.

~$ rm -rv data/ data1/ data2/
removed directory 'data/dir1'
removed directory 'data/'

removed directory 'data1/dir2'
removed directory 'data1/'

removed directory 'data2/dir3'
removed directory 'data2/'

you can also use the -R option to remove multiple directories recursively.

~$ rm -Rv data/ data1/ data2/
removed directory 'data/dir1'
removed directory 'data/'

removed directory 'data1/dir2'
removed directory 'data1/'

removed directory 'data2/dir3'
removed directory 'data2/'

9. Remove specified File/Directory Forcefully

To remove specified File/Directory forcefully pass the -f option to rm.

Caution: Use this command carefully. After being sure, use such commands for data removal. This command will never prompt before every removal.

Let’s take some examples:

Example #1

Remove a file forcefully.

~$ rm -fv file.txt
removed 'file.txt'

Example #2

As you know, we can remove empty directories using the rmdir command but there should be a command to delete a directory with contents.

Remove a directory with its contents forcefully.

~$ rm -rvf data/
removed 'data/file2.txt'
removed 'data/file3.txt'
removed 'data/file5.txt'
removed 'data/file1.txt'
removed 'data/file4.txt'
removed directory 'data/'

Example #3

Remove multiple directories with its contents forcefully.

~$ rm -rvf data/ data1/
removed 'data/file2.txt'
removed 'data/file3.txt'
removed 'data/file5.txt'
removed 'data/file1.txt'
removed 'data/file4.txt'
removed directory 'data/'

removed 'data1/file2.txt'
removed 'data1/file3.txt'
removed 'data1/file5.txt'
removed 'data1/file1.txt'
removed 'data1/file4.txt'
removed directory 'data1/'

10. Usage of Wildcards with the help of rm command

What is Wildcard?

In simple language, a wildcard is a symbol or a special character.

Due to which we can match the pattern of a word or a string to get our desired output. It helps a lot to save time.

Here are some popular Wildcard Characters :

  • * : Asterisk
  • [] : Brackets
  • % : Percent
  • ? : Question mark
  • ! : Exclamation Mark
  • # : Number sign (Hash)
  • - : Hyphen

Let’s take some examples so that your concept becomes clear.

Here I have listed some contents, and with the help of this data, we will try to understand wildcard.

Suggested Read: 35 ls Command Examples in Linux (The Complete Guide)

~/data$ ls
data1  data2  data3  data4  data5  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

Ex. # 1 – Remove those directories that start with “da

~/data$ rm -d da*

Ex. # 2 – Remove those files whose extension is “.txt

~/data$ rm *.txt

11. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of rm command.

~$ rm --help
~$ man rm

12. Check the version of rm command

Check the rm command version using the following command.

~$ rm --version

Infographic

Refer to this Infographic for complete rm command options.

rm command in Linux

You can visit at following websites to get more information on rm command.

Conclusion

I hope you have learned something from this article and you may have found that rm is a very important command in Linux.

I have tried my best to include all the features of rm command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.