rm command in Linux with examples

Today we will learn about the rm command in Linux with there examples, which is mainly used to delete files and directories. This is one of the Dangerous commands before deleting any files and folder please check and confirm.

Q. How to DELETE a SINGLE file using rm command in Linux

Let’s look the first rm command to delete the Single File.

So, now we have to delete a file named as “file3”, then we will use the following command.

Command:
# rm file3 

Q. How to DELETE MULTIPLE files using rm command in Linux

Command:
# rm file1 file2 file3 

So, you can see that we have file1, file2, and file3 and will delete all these three files by using above command.

Q. How to DELETE MULTIPLE Files by using PREFIX/SUFFIX using rm command in Linux

Command:
# rm *.txt 

So, now we have four files with the same suffix “.txt” and we want to delete all that type of SUFFIX file.

Also, if we have the PREFIX like A-1, A-2, A-3, and A-4, then we will use the following command.

Command:
# rm A*

If we want to delete the folder then, we can follow the same process by using -r option.

Q. How to USE EXPRESSION to Delete the Files by using rm command in Linux

Suppose you have files like file1, file2, file3 till file50, and you have to delete only 25 files then we will use the expression in the rm command.

Command:
# rm file{1..4}

So, we have used the expression like {1..4} means it will delete file1, file2, file3, and file4 and as per the above, we have to delete 25 files.

So, we will use the expression like {1..25} means this will delete from file1 to file25.

Q. How to Delete the Folder by using rm command in Linux

Suppose we can’t delete the folder with rm command, we have to use -r option to delete the folder in Linux.

Let’s see the error when we will delete the file using only rm command.

Got the error like cannot remove ‘tastethelinux’: Is a directory.

Because it is a directory the rm is not able to remove the tastethelinux directory. So, let’s use -r option.

Command:
# rm -r tastethelinux/ 

Q. How to Delete all Files/Folder by using rm command in Linux

So, to delete all files and folder we will use wild card (*) let’s see the example.

Command:
# rm * 

So, we will delete all the files in the directory BACKUP and if we have to delete all the folder then we will use this same command with -r option.

Command:
# rm -r * 

Q. How to Delete EMPTY Folder by using rm command in Linux.

Now, if we have to delete the Empty Directory, so will be using -d option.

As we have use ls -l command to check whether there is any file or folder are present in the BACKUP directory.

So, there is no files or folder into the BACKUP directory, let’s see the command.

Command:
# rm -d BACKUP 

Q. How to make a command-line INTERACTIVE while deleting.

So, to make the command line interactive we will use -i option.

Command:
# rm -ri Permission 

So, the Permission is a directory that’s why I have used -r option with -i option, and after using this option it makes the prompt whether we have to delete the file or not.

Suppose, if we will press y or yes then it will delete the file and if we will press n or no it will just come out from the terminal.

Q. How to make command-line INTERACTIVE, when we delete THREE or MORE THAN THREE FILE.

If I have to make the command-line interactive when we ill delete 3 files or more than files, for that there is a -I option.

When we have used rm -I aa bb then nothing prompts into the command-line interface.

Command:
# rm -I jj ii hh cc 

But when we use the above command it will make the command-line interactive, and prompt with the number of files and folder as an argument.

If you want to learn ls command refer this link.

A cheat sheet of rm command in Linux.
Q. How to DELETE a SINGLE file using rm command in Linux

Syntax:
# rm [file_name]

Example:
#rm first_file
Q. How to DELETE MULTIPLE files using rm command in Linux

Syntax:
# rm [file_name1] [file_name2] [file_name3] 

Example:
#rm first_file second_file third_file
Q. How to DELETE MULTIPLE Files by using PREFIX/SUFFIX using rm command in Linux

Syntax:
# rm *SUFFIX
#rm PREFIX*

Example:
#rm *.txt
#rm file*
Q. How to USE EXPRESSION to Delete the Files by using rm command in Linux

Syntax:
# rm [file_name{experssion}]
# rm -r [folder_name{experssion}]

Example:
#rm file{1..25}    *This will delete file1 to file25*
#rm -r folder{1..25} *This will delete folder1 to folder 25*
Q. How to Delete the Folder by using rm command in Linux
Syntax:
# rm -r [folder_name]

Example:
#rm -r tastethelinux 
Q. How to Delete all Files/Folder by using rm command in Linux

Example:
# rm * //This will delete all the files in that current directory.
#rm -r * // This will delete all the folder in that current directory.
Q. How to Delete EMPTY Folder by using rm command in Linux.

Syntax:
# rm -d [empty_folder_name]
Example:
#rm -d BACKUP
Q. How to make a command-line INTERACTIVE while deleting.

Syntax:
# rm -i [file]
#rm -ri [folder]

Example:
#rm -i taste   *tastet is the file.*
#rm -ri tastethelinux  *tastethelinux is a folder.*
Q. How to make command-line INTERACTIVE, when we delete THREE or MORE THAN THREE FILE.

Syntax:
# rm -I [file]
#rm -rI [folder]

Example:
#rm -I first_file second_file third_file
#rm -rI first_folder second_folder third_folder

[wp_quiz id=”2360″]

Give your valuable time