The use of the Linux terminal can make many people uncomfortable, especially if they are beginners to the Linux command line. However, the Linux terminal allows both complex and simple operations to be performed quickly. For example, deleting files and folders. So, in this post, we will show you how to use the Linux rm command and it’s options. This is particular useful when working on servers which offer no GUI.

The Linux rm Command

The Linux rm command is one of the simplest and most useful commands in Linux. Its base function is to delete files or folders on the command line. This command is part of GNU Core Utilities whose objectives are based on the basic manipulation of files, text and shells in a GNU system.

By default, the rm command does not delete folders. However, it is possible to do so by using a modifier of the command.

Basic Usage of the rm Command

The command is quite simple to use. If we use the user manual as a reference, we will notice that the syntax of use is the following:

 rm [OPTION]... [FILE]... 

In other words, we have to invoke the command rm, then, if we consider it necessary, we will add options or parameters and then the files we want to eliminate.

To delete a single file, just run the following:

rm [file_path]

Example:

[[email protected] ~]$ rm 1.txt

If you want more than one file, you can list them separated by blank space.

[[email protected] ~]$ rm 1.txt 2.txt 3.txt

If all the files have the same extensions, we can use a wildcard.

[[email protected] ~]$ rm *.txt

For more information, read “Using Standard Wildcards / Globbing“.

This is the simplest and most basic way to use the rm command. However, there are options that modify it’s behavior.

The rm Command Options

Like most Linux commands, the rm command has a series of modifiers called options that adds functionality to it.

Request Confirmation / Interactively Remove Files

The Linux rm command deletes files. The problem is that there is no way to undo it. Once you delete a file you will not be able to undo the operation. So, you have to be very careful when using it. However, rm has an option that asks for confirmation every time we delete a file.

This is the -i (interactive) option. It is advisable to use it whenever possible to verify what is going to be removed. If you are new to the Linux command line you can use an alias to configure rm to always ask for confirmation before deleting a file.

Once you have executed the command along with the -i option, you will be asked to confirm the deletion. To proceed, type “yes” or the “y” key.

Example of an interactive deletion using rm:

$ rm -i 1.txt
 rm: remove regular empty file '1.txt'? 

Force Delete a File with rm Command

If a file is write-protected, rm will ask for confirmation to be deleted, similar to using the interactive option described above. It is possible to ignore this and force deletion using the -f (force) option.

[[email protected] ~]$ rm 1.txt 
rm: remove write-protected regular empty file '1.txt'? n
[[email protected] ~]$ rm -f 1.txt 
[[email protected] ~]$ 

Using the -f (force) option removes any confirmation and will delete the files immediately. This can be dangerous if not used carefully, especially if your use the -r (recursive) option with force.

Recursively Delete Files with rm Command

Recursion is a popular construct in the Linux command line world. In the context of deleting files it means rm will dive into folders and sub-folders. Let’s use this file structure as an example:

files
 ├── old-files
 │   ├── medical-records.txt
 │   ├── receipt.txt
 │   └── taxes.txt
 └── pictures
     ├── David.jpg
     ├── family.jpg
     ├── Floyd.jpg
     ├── Garfunkel.jpg
     ├── Roger.jpg
     └── Stevie.jpg

In the above example we have a directory. Inside this directory is two more directories, each holding some files. If we moved into the top directory (files) and used the -r (recursive) option to rm, we can delete all of the directories, sub-directories and files at once using the following command.

rm -r *

Example:

[[email protected] ~]$ cd files/
[[email protected] files]$ rm -r *
[[email protected] files]$ ls -lrt
total 0
[[email protected] files]$ 

The above example we deleted two directories and all files inside of them using the recursive option.

Deleting a Folder with rm Command

As we stated in the introduction, the rm command is designed to delete files. We also learned above that we can use the recursive option to delete a folder and all of it’s contents.

Example of deleting the pictures folder and all of it’s contents:

[[email protected] ~]$ rm -r /path/to/pictures

Again, the recursive option should be used with care.

Deleting an Empty Folder with rm Command

The -r (recursive) option you saw above works to delete folders that have files. However, you may need to delete empty folders or directories. The rm command has an option specifically for deleting empty folders. It is the -d (dir) option.

Example:

[[email protected] ~]$ rm -d /home/savona/empty_dir

Verbose Mode – Deletion Confirmation

By default the rm command does not create any output, it just silently deletes the files or folders it was told to. If you would like to see the actions it is taking you can use the -v (verbose) option.

Example using the -v (verbose) option:

[[email protected] pictures]$ rm -v Garfunkel.jpg 
 removed 'Garfunkel.jpg'

The verbose option can be used in conjunction with other options.

[[email protected] files]$ rm -rv *
 removed 'documents/taxes.txt'
 removed 'documents/medical-records.txt'
 removed 'documents/receipt.txt'
 removed directory 'documents'
 removed 'pictures/Stevie.jpg'
 removed 'pictures/Floyd.jpg'
 removed 'pictures/Roger.jpg'
 removed 'pictures/David.jpg'
 removed 'pictures/family.jpg'
 removed directory 'pictures'

Conclusion

This quick tutorial covered the rm command and all of it’s options. We talked about basic usage and some more complex usage like deleting directories with the rm command.

Just another warning, using the recursive & force options together ( rm -rf ) has gotten plenty of junior admins in trouble in the past. Please pay special attention when using those two options together.

Resources