The rm is the command in Linux and Unix-like operating systems. The term rm stands for remove or delete directories or files.
So if you are using without any arguments then be aware that it will delete the files and you will not be able to recuperate (get it back) quickly.
Note: If you are deleting a directory without any argument, it will not delete that.
If you like to watch the rm command video then you can watch the video.
How to get help for the rm command
I always suggest reading the manual page and help. It will give you an in-depth introduction to the command.
So, to get the manual page type the below command,
But, to get the only command options, you can type the below command.
Syntax of the rm command
Now you know how to get help with the rm command. So, let’s talk about rm syntax. The syntax of the rm command is as below,
Syntax: rm [OPTION]… FILE…
Deleting a file using rm command
For example, if you have a file with the name file1.txt and you want to delete it, then the command will be like the below, and it will remove that file without your confirmation.
Deleting a file with confirmation using rm command with -i argument
** If you want to delete the file but before removing you want a confirmation message then you have to use -i argument. Here, -i stands for interactive. So the command will be like something below.
rm -i file1.txt
Deleting a write-protected file using rm command with -f argument
If you are deleting a file that is write-protected then with the standard rm command, you will not be able to remove it. In that case, you have to use the -f argument where -f stands for forcefully.
rm -f file1.txt
Here, file1.txt is the file name. Also, you can change it as per your respective file name.
Note: -f argument will not work for write-protected directories.
Deleting a file or directories recursively
If you want to delete all the files and their subdirectories recursively of the parent directory, then you have to use -r argument. As I have already mentioned that if you are deleting any directory without an argument then it will not be deleted but after using the -r argument, you can delete that.
rm -r /home/sahil/dir1/dir2/file1.txt
(For example, I have created a file with the name raj inside the amit directory using the touch command. And tried to delete using the rmdir command but you can see, it says, it is not empty then I decided to remove using rm -r, it is asking for confirmation, and then it is removed.
Note: If you know what are you deleting, then you can use the below command. But be sure that you want to remove it. It will not prompt any confirmation.
rm -rf file_name
Example: You can look in the below example I have deleted the directories, and it was deleted without confirmation so be sure before removing them.
Deleting multiple files or directories
You can delete multiple files once and directories as well using the arguments -r. So, you can find the below command useful for that.
For files —————–
rm file1.txt file2.docx file3.txt
So, for directories ———–
rm -r dir1 dir2 dir3
In this article, you learned about the rm command in Linux, and you can see that it is easy to use. So, I hope, you understand. But, if you have any questions, then you can also ask in the comment section.
Also, you may like to read about it.