The complete guide to rm command on Linux

A Complete Guide To The Rm Command In Linux

The rm command in the Linux terminal is used to remove or delete objects such as files, symbolic links and directories. However, to delete a directory, you have to use extra options with the command. You should be very careful while using the command, as you cannot the file(s) once you remove it. And using it with the root privileges (with the sudo command) can also be very dangerous.

In this article, we will learn in detail about the working of the rm command and also learn the different options that can be used by this command.

Basic usage of the rm command

Just like any other command in Linux/UNIX-based system, the usage of this command is also something like this:

rm {Option} file1 file2

Let’s have a look at this with the help of an example, to remove a file, you do not have to use any kind of option with the rm command.

rm file3 file1
Removing Multiple Files With The Rm Command
Removing Multiple Files With The Rm Command

We have successfully removed two files using the remove command. And you can delete as many files as you want if you type their full name one after the other and separate each with a ‘space’.

If you use a wildcard such as an asterisk (*), you can also make the multiple file deletion really easy. Let’s say you want to remove all the PDFs from a directory, you can type :

rm *.pdf
Removing Files Using The Wildcards
Removing Files Using The Wildcards

Using the rm command with options

  • Let’s try the interactive mode first of all. You can use the -i option along with the command to use it. The command will then ask you for confirmation if you want to delete a file or not. This can be helpful to you if, for example, you want to delete all the PDF files in a directory except some important ones.
rm -i *.pdf
The Interactive Mode Of Rm Command
The Interactive Mode Of Rm Command
  • For the write-protected files, you can use the -f (Force Delete) option. It overrides the minor protection layer and removes the file. In the below example, I’ve removed a file named ‘important.pdf’ which is write-protected for everyone.
rm -f important.pdf
Removing A Write Protected File
Removing A Write Protected File
  • With the help of -r option (recursive deletion), you can delete all the files and subdirectories inside a directory at once. At each level, the rm command will delete everything.
rm -r A
Recursively Removing The A Directory
Recursively Removing The ‘A’ Directory
  • The -d option of the rm command allows you to remove empty directories at any location. You can use this option in the following manner :
rm -d dir1 dir2
Removing Empty Directories
Removing Empty Directories

Also Read: Find and Delete empty directories on Linux

  • Like any other command, you can use the verbose option (-v) to display what rm command is doing in the background.
rm -v *.txt
Remvoing Files With Verbose Option
Removing Files With Verbose Option

Summary

While running this command with the admin privileges, be extremely careful as you can accidentally delete any important file which might be important for your system. Do not forget to run the ls command before running rm as a precaution.

References

RM command – Manual Page