Linux terminal commands are by far the most powerful and efficient way to get anything done on the platform. They may look complicated on the outside, but once you’ve got a grip of the basics, they’re actually quite intuitive.
In this article, we’re talking about the rm command in Linux and how you can use it to get rid of just about anything on your system.
Also read: 25 essential Linux Terminal commands
How to use the rm command?
The very basic syntax for using the rm command is as follows.
rm [file name]
When used like this without any options, the command will simply delete the file specified without prompting the user. A word of caution, deletion from the rm command is permanent, very permanent. So don’t use it lightly.
rm File1.txt
The command above will delete the text file named File1 without prompting the user. At times you may have to use sudo with the command in case you don’t have the permission to delete a particular file or directory.
Do keep in mind that regardless of the flag used, if the file you’re trying to delete is write-protected, you will get a prompt asking if you want to delete the file. You can override this using the -f flag.
Another rather handy flag is -v which stands for ‘verbose’. This will output the names of the deleted files once the command has executed successfully.
If you would like a confirmation prompt before a file specified in the rm command is deleted, use the -i flag.
rm -i file1.txt file2.txt
Now the command will ask you before deleting individual files. To confirm, press y and hit enter.
Finally, to delete a file whose name starts with a hyphen (-), you’re going to have to specify a double dash before the file name.
rm -- -file1.txt
Also read: How to remove a remote Git repository?
How to delete multiple files using RM?
In order to delete multiple files using the rm command, simply list the files one after another.
rm file1.txt file2.txt file3.txt
You can also use regular expressions to specify which files to delete. For example, if you want to delete all text files in a directory, use this command.
rm *.txt
How to delete directories using RM?
In order to delete directories using rm you’re going to have to use the -d flag. Here’s how.
rm -d Directory1
Note that this command is identical in terms of functionality to the rmdir command. Meaning it’ll only work on empty directories.
If you want to delete a directory and all the files inside, use the -r flag to recursively delete files.
rm -r Directory1
Also read: How to increment a variable in Bash?
rm -rf
As mentioned above, the rm command will show a confirmation prompt before deleting a file that’s write=protected. You can override this using the -f flag and the -r flag removes files recursively.
The combination of these two flags makes the rm -rf command perhaps the most dangerous command you can run in the terminal. Run it in the wrong place and you can wipe out the OS itself in no time. Once again, the command can be really dangerous and should be used with extreme caution.
rm -rf Dir1
Also read: How to use the Cat command in Linux?