rm: how to delete files and folders with this Terminal command

Rm command

Deleting a file on any operating system is usually as easy as clicking on it and pressing the delete key or right-clicking and sending it to the trash. We all know how to do that and it is the best option to delete a file in front of us. But, what happens when what we want is, for example, to delete several files that are within the same folder? It happens that in Linux we have the rm command that will allow us to eliminate practically anything from the terminal.

"Rm" is the abbreviation of «remove», which is «to eliminate» in English. It is used to delete files and if it is used recursively it will also allow us to delete directories. By default it will not remove directories and it will remove any files specified on the command line. The removal process unlinks a file name on a file system from its associated data and marks the storage space as usable for future writes. You have to be careful, because once you delete something with rm it can no longer be recovered.

Options available for rm

-f,
–Force
Ignore non-existent files and never ask before deletion.
-i Ask before deletion.
-I Ask once before deleting more than three files or when deleting recursively.
–Interactive[=WHEN] Question according to WHEN: never, once (-I), or always (-i). Yes WHEN not specified, always ask.
–One-file-system When deleting a hierarchy recursively, it ignores any directory that is in a different directory than the one corresponding to the command line argument.
–No-preserve-root It does not treat the root directory in any special way.
–Preserve-root It does not remove the root directory, which is the default behavior.
-r,
-R,
–Recursive
Recursively remove directories and their content.
-d,
–Dir
Delete empty directories. This option allows us to remove a directory without specifying -r / -R / –recursive.
-v,
-verbose
Verbose mode; Explain at all times what is being done.
-help Show a help message.
-version Displays version information.

Practical examples

rm does not delete directories by default. For this we must use the -r / -R / –recursive options. If a directory is empty, use the n -d / –dir option. If we want to remove a file that starts with a dash (-), we have to add a separate double dash (-) before the file name. If the second dash is not added, rm may misinterpret the filename as an option. It is worth remembering that the asterisk (*) means "everything that matches", adding "*." and behind only the asterisk.

For example, to delete the file «-test.txt» we would have to use the command

rm -- -prueba.txt

The above will be in the case in which it is in our personal folder. If not, we will have to add the full path, which would be something like:

rm /home/pablinux/Documentos/-file

The difference between the two options is that in the second case it has a slash (/) in front of it, which makes the option not confused.

Other examples would be:

  • rm -f test-txt: will delete the file "test.txt" without asking even if it is protected.
  • rm*: it will delete all the files in the directory where we are from the terminal. If it has write protection, it will ask us before deleting it.
  • rm -f *: will remove everything in the directory without asking.
  • rm -i *- Will try to delete all files in a directory, but ask every time to delete one.
  • rm -I*: as above, but will only ask for confirmation if there are more than three files.
  • rm -r directory, where "directory" is a specific one: it will remove the directory "directory" and any files and subdirectories it contains. If any files or subdirectories are write protected, it will ask.
  • rm -rf directory: same as above, but will not ask.

The command you should never use: rm -rf /

And we end with a command that we can see on social networks more as a joke than anything else. The above command means 1- delete, 2- recursively as much as possible and 3- starting with the root. Because of how Linux manages the drives, it will also delete the content of any hard drive that we have connected to our computer. If you use it, do not say that we had not warned.


2 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Cristian said

    Interesting about the clarification regarding the last command, I knew that I could delete all the content of the disk where the system is, but I did not know that it also deleted the content of any other disk that we have connected!

  2.   JUAN said

    Thank you very much, your post was very useful, I solved my problem very easily, but without it I would not have succeeded.