Rm Command in Linux

FREE Online Courses: Click, Learn, Succeed, Start Now!

In this article, you will learn all there is to the rm command in Linux. We will be going through various topics like what rm is, why it is used, the syntax of the rm command, and the different options used along with it.

In the end, we will also be looking at some practical commands in the terminal that will help you manage files in Linux using the terminal. So buckle up, and read right till the end!

What is the rm command?

Rm stands for remove. The rm command is a command-line-based utility in Linux-based operating systems that helps in removing files and directories from your Linux filesystem from the terminal itself.

By now we all know that there are 2 ways to do almost anything in Linux, one is the GUI and the other is the terminal. The Terminal is all about efficiency and productivity rather than the GIU where you have to click many buttons and navigate through different items. However, in the terminal, there are no extra Clicks on nicks!

We also know that there are many such commands that help in file management, like pwd, cd, ls, mkdir, cp, mv, and rmdir. Rm is one such command used for managing the filesystem, to be more specific, deleting files, directories, symbolic links, and many more from the system.

By default, the rm command is not capable of removing directories, indeed we have a couple of options that we can use with the rm command to specify what it will do, which we will discuss later. The rm command runs silently and you must be very careful of the files you delete because once deleted, you cannot recover them back.

The only disadvantage of the rm command over the GUI is that it does not have a recycle bin like the GUI. Therefore the files you delete using the rm command are gone forever, so double-check the file name before you delete it.

How does the rm command work?

We might think that the rm command follows a really simple process of removing files, well, maybe, but let us get a better understanding of what happens behind the scenes.

The removal process of the rm command unlinks a file (that you specified to remove) in a file system from its connected or linked data. It then marks that space on the storage device as “usable” by future writes.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

In simpler words, when you delete a file, the data in the files is not changed at all, except it is just no longer associated with a file name. The data of the file is not removed, but after unlinking it using the rm command, it becomes inaccessible.

History of Linux rm command

In the initial stages, the rm command was meant to delete only empty directories. In fact, for old time’s sake, this feature is still available with the usage of the option “d”.

The rm utility that was bundled in the GNU Coreutils was written, designed, and developed by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering. They also included the option “-d” to provide the exact same functionality of the ‘rmdir’ command.

The rm command is not available in Linux-based distributions but is also available as a separate package for Microsoft Windows as a part of the UnxUtils . Even the IBM i operating system has been ported with the rm command.

Syntax of Linux rm command

Now that we are finished up with the theory part, let us touch on the practical part. Let us start by looking at the syntax of the rm command:

rm <options> <file name>

Options used with the rm command

In the syntax, there is a field for the options, where we can choose different options to specify what and how the rm command should function. Let us go through the options that are used with the rm command:

1. -f

This option ignores nonexistent files and does not prompt while removing the file. You can also write this option as “–force”

2. -i

This option prompts you before the removal of every file. It basically acts as you confirming to really delete that file.

3. -I

This option prompts once before removing more than three files, or when removing files recursively.

4. –interactive = <when>

This option prompts according to the <when> you specify. <when> can take 2 values:

  • -I – once
  • -i – always

If you don’t specify <when>, the option will take it as ‘always’.

5. –one-file-system

When you are removing a file hierarchy recursively, this option skips any directory that is on a filesystem different from that of the corresponding command-line argument

6. –no-preserve-root

This option does not treat the root directory (/) in any special manner

7. –preserve root

This option does not remove the root directory (/).

8. -r

This option removes directories and their contents recursively, you can also write this command as either “-R” or “–recursive”.

9. -d

This option removes empty directories. you can also write this command as “–dir”

10. -v

This option provides verbose to the output to give a better understanding of what happened. you can also write this command as “–verbose”

11. –version

This option prints out the version information of the rm command you are using.

version of rm

12. –help

This option prints out the help menu which contains information about the usage of the rm command and information regarding the different options available with the rm command.

help menu of rm

Now that we have laid down the fundamentals of the rm command, let’s go deeper on the practical side and explore some commands in the terminal.

Deleting a file

To simply delete a file, just use the rm command followed by the filename you want to delete.

deleting a file

Providing verbose while deleting

When you delete a file, the rm command says absolutely nothing, whether it has been successfully deleted or not, If you want a better understanding of what is happening, use the option “-v” to provide verbose.

providing verbose while deleting

Even If the file you want to delete does not exist, it will tell you so:

providing verbose even if file is not deleted

Deleting multiple files

If you want to delete multiple files in just one single rm command, you can do so by writing the filenames one after the other separated by a space as follows:

rm <filename-1> <filename-2> <filename-3> <filename-4>

deleting multiple files

If you enter a name of a file that does not exist, rm will remove the remaining files and leave the one that does not exist.

deleting multiple files and showing if some have been failed to be deleted

Ask before deleting a file

If you want rm to ask you before deleting a file, use the option “-i” as follows:

rm -i <filename>

asking before deleting a file

If you are using the option “-i” while deleting multiple files, it will ask you every single time a file is being deleted.

asking before deleting multiple files

Removing an empty directory

If you want to remove an empty directory, use the option “-d” or “–dir” as follows:

rm -d <empty directory name>

removing an empty directory

Removing a non-empty directory

Ok, you now know how to delete an empty directory. Does that mean you can’t delete a directory that is not empty? Well no, in fact, you can even take things to the next level (which we will see in the next section). If you want to delete a non-empty directory using the rm command, use the option -R for recursively deleting the files.

removing a non empty directory

Using a wildcard to delete files of the same kind

Suppose you want to delete all the .txt files or all the .odt files, you can do so by using the wild card (*) as follows:

rm *<extention>

using a wildcard to delete files of the same kind

In the above output, the command only deleted all the “.txt” files and retained the “.odt” files.

Removing a write-protected file

Whenever you remove a write-protected file, the rm command will ask you for confirmation. If you want to delete the file without the prompt, pair the rm command with the option “-f” as shown in the output below:

removing a write protected file

Deleting everything from the current directory

If you want to delete everything from the file you are currently in, use the wildcard asterisk (*) with the rem command. When I say everything, I mean only files, it will not delete the directories.

deleting everything from the current directory

If you observe the output above, the command removed only the files and not the directories.

Deleting filenames starting with a dash (-)

If you want to delete files that start with a dash, use the option “–” along with the rm command as the simple use of rm will not remove such files.

deleting filenames with dash

 

 

Another way of removing files whose name starts with a dash is to specify the whole path of the file as shown below:

deleting filenames starting with a dash

Other than the flag “–”, you can also use “./” to remove files starting with a dash as shown below:

deleting filenames starting with dash

Rm vs unlink

Even the unlink command does exactly what rm does, however, it is strictly defined by the POSIX standard. The main disadvantage of the unlink command is that it can work on only one at a time and also doe nothing to directories.

The worst part of unlink is that there are no options available with it, meaning its behavior can’t be changed at all. The unlink command does only one thing – performs the unlink system call on one file.

The rm command wins any day as it has all the options and features like safety checks, interactive prompting, conditional deletion, and recursive operation. To sum up, rm is much better and friendlier to use than unlink.

Summary

As you have seen the rm command is a simple yet efficient tool to delete files and directories. You have now learned what rm is, how it works, the syntax, options, and practical examples of therm command.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

1 Response

  1. Greg says:

    How could you delete a file every few hours based on article commands above?

Leave a Reply

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