One of the most powerful and controversial tools of Linux is the terminal. For many people, it is quite a strange thing to use, but it is one of the most efficient tools out there. In fact, if you plan to use Linux on a home or production server, the terminal will always be your main ally.
In the terminal, we will be able to do many things, but keep in mind that it is not mandatory to do it there. In this sense, Linux has evolved so much that it is possible to use the system without using the terminal, but it never hurts to know how to use some terminal commands.
So be prepared that in this post, we will introduce you to the Linux rm command.
The Linux rm command is part of the GNU Coreutils
Within the operation of a Linux distribution, there are many components that go unnoticed. One of them is the GNU Coreutils. This is a suite of file manipulation commands belonging to the GNU project.
Besides this, it is fair to say that the GNU Coreutils commands have in common that they are quite easy to learn. They are also present in all Linux distributions because they act very much like the kernel.
So the Linux rm command is part of this suite, which guarantees that it is easy to learn and is present in all Linux distributions.
The Linux rm command
The Linux rm command is basic in the manipulation of files in a terminal. Thanks to this command you can delete files or directories. It is that simple.
Although files are deleted, it is always possible to recover them with more advanced tools because it removes the file reference on the disk. However, actions performed with the rm command, cannot be undone without these tools. So you have to use it carefully.
In case you want to permanently delete files without the possibility to be recovered, the rm command is not for you, better use the shred command.
Basic use of the Linux rm command
The rm command, like most commands in Linux, has a basic syntax. It must also receive arguments that are necessary to know which files or folders to delete.
Also, there are some options modify the behavior of the command.
So, the basic syntax of the rm command is as follows:
:~$ rm [option]... [file]...
In case you want to show in the terminal all the available help that incorporates the command, execute the following:
:~$ rm – help
There you can see all the options of the rm command. Some of them are rarely used and others are very common and useful. So let us learn how to use the Linux rm command with a series of practical examples.
Remove one or more files with the Linux rm command
This is the most basic operation of the command, so you only have to specify the name of the file, including the file extension.
:~$ rm file1.txt
In this case, the command assumes that the file1.txt is located where the prompt is. But you can also delete files from another location. To do this, simply specify the full path of the file. For example:
:~$ rm /home/user/Documents/file2.txt
Remember, I am working with fictional files. You always have to substitute your files.
Similarly, you can remove several files at once. You only have to specify them separated by a space. For example:
:~$ rm file1 file2 file3
Also, you can delete several files from various paths:
:~$ rm file1 /home/user/Documents/file2 /home/user/Downloads/file
Again, I remind you that the files have to have the extension.
Remove one or more directories
So far, you have deleted files. There are no problems there, but what if you want to delete a folder or directory? Well, you have to use the -r or -R option and then specify the folder.
An example of this is the following:
:~$ rm -r example
The above command deletes a folder called example at the same location as the prompt. As with files, you can delete a folder from another location by specifying the full path:
:~$ rm -r /home/user/Pictures/album
It is also possible to delete several folders in the same command, as well as deleting files.
:~$ rm -r example /home/user/Music/cd1 /home/user/test
However, the -r or -R option is used for folders that are not empty. Therefore, the -r option recursively removes that location. If the folder is empty, use the -d option.
:~$ rm -d empty_folder
You can also delete several empty folders or folders from different locations in the same way as explained above.
Prevent errors with options with the Linux rm command
As I mentioned at the beginning, what you do with the rm command cannot be undone. Sure, the files can be recovered with more advanced tools, but it’s a headache. So you have to be careful.
Well, the rm command has two options that help prevent these errors, because it will ask us for confirmation before deleting the file. This is the function of the -i option.
An example of the use of this command is as follows:
:~$ rm -i file1.txt
You will be asked for confirmation as follows:
rm: remove regular empty file ‘file1.txt’?
And you must type “y” and to confirm the removal. Quite useful to avoid errors.
The -I option does the same but when more than 3 files are to be deleted. So it is less intrusive than the previous one but still quite useful.
:~$ rm -I file1 file2 file3 file4
So, you will be asked to confirm the operation.
rm: remove 4 arguments?
Type y to delete the files.
Force removal with the -f option
There are files that are write-protected. If you want to remove this small restriction, you can use the -f option which forces this deletion.
Also, this option is used to remove any type of screen output. That is, just delete it.
Note that this option is a bit dangerous so you should not use it with root privileges.
:~$ rm -f file1 file2
Or to delete folders, you can combine it with the -r or -d options as the case may be.
:~$ rm -rf folder1
The Linux rm command is one of the easiest to learn, but it is also very useful because it allows you to remove files and folders. Widely used in configuration scripts as well as in server administration.
So, we invite you not to be afraid of the Linux terminal as it is a powerful tool and has easy to learn commands like this one.