What You Need to Know About the rm Command in Linux and Its Advanced Syntax

The rm (remove) command is one of the most important Linux utilities that every beginner to advanced Linux user should understand how it works and how to use it safely, as it can delete important files without confirmation if used incorrectly.

So let’s start this brief guide, where you will learn some of the basic and advanced syntax of the rm command as well as how to use it safely so you don’t accidentally delete anything important.

What is the rm command in Linux?

The rm (remove) command, which is part of GNU Coreutils, is used to delete old files and directories from your computer, but it can accidentally delete files you actually want to keep, so use caution when invoking the command from your shell.

Since rm is already included in GNU Coreutils, you won’t even need to instal it; all you need to know is how to properly use the command and its syntax. Let’s take a look at the rm command in Linux and see how it operates and get familiar with the basic syntax of rm.

Basic Syntax of rm command

As shown below, the rm command syntax is made up of rm, which is the name of the utility, “OPTIONS,” which is used to specify the type of operation to perform, and “TARGET,” where you need to specify the file or file path on which you want to apply rm command.

$ rm [OPTIONS] [TARGET DESTINATION]

Below is the list of the most commonly used options with the rm command.

  • rm * * * Removing Multiple Files
  • rm .[extension] Find and remove the matching extension
  • rm -- -* Remove target with a dash
  • rm -d Remove one or more empty directories
  • rm -r To delete a target recursively
  • rm -f Remove a target forcefully
  • rm -i Remove a target interactively
  • rm -v Remove a target in verbose mode

What happens when you use the rm command in Linux?

When you run the rm command and specify, which file or directory to delete (by default, it doesn’t delete the directory unless you use the -r option), it will delete the file or directory without putting it in the trash or bin directory.

The reason for this behaviour is that whenever you remove a file using the rm command, it will get unlinked from the file system without being shredded.

This will let other data overwrite the unlinked data block. That’s why, when you delete files in Linux, it is best not to perform any rewrite operation that will overwrite the data, making it hard to recover.

Be very careful while removing the file or directory using the rm command. It’s not like Windows, which lets you recover it from the recycle bin; once the file is gone, it’s gone.

Now that we’ve covered the fundamentals of the rm command, let’s move on to the most engaging part of this article.

Remove a file using the rm command

In order to delete a file with the rm command, simply type the command, followed by the file’s name, and hit enter. For example, if you wanted to delete a file named “file.txt,” you would type:

$ rm file.txt

The result of that command is shown below.

Remove a file using the rm command
Remove a file using the rm command

As you can see, “file.txt” has been deleted from the current directory using the rm command. But what if the file is not located in the current directory?

In that case, you need to specify the exact location of the file you want to delete. For example, I have a file located in the /home/trendoceans/file.txt path. In this situation, I have to specify the exact location of the “file.txt” while using the rm command, as shown below.

$ rm /home/trendoceans/file.txt

The results of that command are displayed below.

Remove a file from the exact location
Remove a file from the exact location

Removing Multiple Files Using the rm Command

If you have multiple files named “file1.txt“, “file.2.txt“, and “file3.txt” in your current path, then you can easily remove them by specifying all files together with the rm command, as shown below.

$ rm file1.txt file2.txt file3.txt

Below is the output of the above command.

Removing Multiple Files using the rm command
Removing Multiple Files using the rm command

Find and remove the matching extension using the rm command

If you have a directory full of files (*.jpg, *.png, *.txt, etc.) and want to delete the files with the common extension “*.txt,” use the rm command instead of specifying each file individually.

You can easily remove all of them via their common extension, as shown below.

$ rm *.txt

Below is the output of the above command.

Find and remove the matching extension
Find and remove the matching extension

You can easily manage to delete other files sharing the matching extension. For example, rm *.txt *.jpg will remove all the text files and jpg images from the specified or current location.

If you want to learn more about how to remove files with specific extensions using wildcards, you should read this article.

Remove target with a dash using the rm command

Sometimes, people create files with illegal syntax, for example, “file%2.txt” or “file.txt.” The special syntax used in between files can easily be deleted. What if you place “” at the beginning of the file name (-file.txt)?

This type of file will be treated as an option, which will make it difficult to delete. If you try to delete it, you will get an invalid syntax error.

To remove a file with “-” at the beginning of the file name, use “--” as an option or specify the file with “./“, as shown below.

$ rm -- -file.txt
OR 
$ rm ./file.txt

The results of the preceding command are displayed below.

Remove target with a dash
Remove target with a dash

Remove one or more empty directories using the rm command

If you only have an empty directory in your system, you can easily remove it with the “-d” option using the rm command, as shown below.

$ rm -d demodir/

Below is the output of the above command.

Remove one or more empty directories
Remove one or more empty directories

You can even delete multiple empty directories at once by using the rm command as rm -d demodir1/ demodir2/ or the rmdir command as rmdir demodir1/ demodir2/ to delete the empty directories in the Linux system.

Remove one or more directories with content recursively using the rm command

Until now, we have learned how to remove directories without any content, but what if the directory holds the content inside it?

The standard way of using the rm command with the “-d” option will not do the job. If you try to remove it, you will get the “rm: cannot remove ‘xyz/’: Directory not empty” error, as shown below.

rm: cannot remove 'demodir/': Directory not empty
rm: cannot remove ‘demodir/’: Directory not empty

To remove such a directory containing content inside it, use the “-r” option to delete the directory recursively with all the content inside it, as shown below.

$ rm -r demodir/

Below is the output of the above command.

Remove one or more directories with content recursively
Remove one or more directories with content recursively

Remove a target forcefully using the rm command

We discovered how to delete a directory with numerous files inside of it. There are times when certain files in the directory are either owned by a different user or do not have the proper permissions (write protection).

This lack of permission will prompt you to choose “y” or “n” while deleting the directory, as shown below.

Write protected file inside the directory
Write a protected file inside the directory

If you have made up your mind and want to delete everything forcefully without prompting yes or no, use the “-f” option with the “-r” option, as shown below.

$ rm -rf demodir/

Below is the output of the above command.

Remove a target forcefully
Remove a target forcefully

Remove a target interactively using the rm command

The rm command is very destructive, and so far, we have seen it not ask permission while deleting files. For the directory, if you were using the -r command, any directory containing general files owned by you would be deleted in a splash of seconds.

If you want to interactively delete your files inside the directory by asking “y” or “n” on screen, use the "-i" flag as shown below.

$ rm -ri demodir/

Below is the output of the above command.

Remove a target interactively using the rm command
Remove a target interactively using the rm command

Remove a target in verbose mode using the rm command

Have you noticed until now that whenever you delete any directory containing files (ignore the interactive way), it will not list the file being deleted on the terminal screen?

Use “-v” to turn on the verbose mode and see a list of all the deleted files inside the directory, as shown below, on the terminal screen.

$ rm -rv demodir/

Below is the output of the above command.

Remove a target in verbose mode using the rm command
Remove a target in verbose mode using the rm command

Remove a target using the wildcard characters

Above, you learned how to use different options to delete files and directories recursively, but you have not yet been introduced to the wildcard character, which makes the rm command more dynamic to remove a large number of files from your system using a certain pattern in one go.

So let me tell you about some of the wildcard characters you can use to get rid of the file:

  • ^ Exclude from match
  • * Match one or number of more occurence
  • ? Match a single occurrence
  • [] Specify a range for matching

Here’s a small snippet of code that uses wildcard characters to delete all files where the substring “ile” appears anywhere in the filename.

$ rm -v *ile*

If you want, you can learn more about this by following this link: Remove files using a wildcard character in Linux.

Wrap up

This article has taught you how to delete files using the rm command, as well as how to delete files recursively, interactively, etc.

In any case, if you have any advice that you think another reader could use, please share it with them.

Leave a Reply