rm command in Linux with examples

The rm command is one of the most common and basic commands in Linux. Even if you have a limited amount of experience with Linux, you’ve most likely heard of it. The main purpose of this command is to remove files and directories. Using this command to remove files and directories cannot be undone.

That means that you should utilize this command with care as files and directories are impossible to recover without a backup. Deleting files in a GUI desktop environment is a safer option, since files will usually go to the recycle bin before being permanently deleted. However, using the rm command is much more versatile. This is the usual case for most things in Linux that can be done from either the command line or GUI.

In this guide, you’ll learn how to use the rm command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the rm command on Linux
rm command in Linux with examples
rm command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software rm
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The rm command is used to delete files and directories from the command line. The command doesn’t take long to learn and there aren’t many essential options to use with it. But, it still comes with a lot of options that come in handy in certain situations. Follow along with the examples below to learn about some of the most common ones.

Various examples of the rm command in Linux
Various examples of the rm command in Linux

rm command in Linux Basic Examples

  1. Removing a file with the rm command is very simple. We can achieve this by typing rm followed by the filename.
    $ rm file.txt
    

    Running this command removed the file file.txt. But we can also remove files from a different directory than our current one, like in the command below.

    $ rm ~/Downloads/linux.iso
    

    Running this command will remove our linux.iso file located in the ~/Downloads directory.

  2. If you use the rm command without extra options to remove write-protected files, the rm command asks for confirmation. The rm command can be used with the -f option to bypass this process and instantly remove the file.
    $ rm -f file.txt
    

    Running this command will – as the -f (force) option implies – forcefully remove our file.txt file without the need to give it the go-ahead.

  3. In our previous example, we showed how the rm command by itself would ask for confirmation if you try to remove write-protected files. But if you try to remove regular files, you won’t get a confirmation prompt. You’ll have to use the -i option for this. You should use this to avoid removing files accidentally, as the rm command cannot be undone.
    $ rm -i file.txt
    

    This command will ask if you wish to proceed with the removal of the file in question. Typing y will tell the command to proceed with the operation, and typing n will abort the command.

  4. Up until now, we’ve shown that the rm command can remove a single file from a given directory, but it can also remove multiple files at once. We can use the rm command on its own to do this by typing the command followed by the desired number of filenames.
    $ rm file1 file2 file3
    

    This command will permanently remove file1 and file2 in our current working directory.

  5. As we mentioned at the beginning of this article, the rm command can permanently remove both files and directories. Using the -r option, we can remove an entire directory and its contents.
    $ rm -r directory01
    

    This command permanently removes directory01 and any files or subdirectories that it contains.

NOTE
You can always use the man command to read more about the rm command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The rm command is pretty simple, but as you’ve observed throughout the examples section of this article, it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know them. However, they can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser-known options of the rm command that we think are useful.

rm command in Linux Advanced Examples

  1. To swiftly erase all of the files in the current working directory, one would simply use an asterisk following the rm command. This command does not remove the directory itself, only its contents.
     
    $ rm *
    
  2. Removing an empty directory requires slightly different steps. That’s where the -d option comes in.
    $ rm -d oldfiles
    

    This command will permanently remove our empty directory named oldfiles.

  3. You can utilize the rm command in a very fascinating – but equally helpful – way by running it alongside the xargs command. Doing this will allow you to remove large numbers of files with efficiency. To do this, you will want to list the desired file names in a regular text file.And after which, the xargs command will be able to read that list, and feed the data to the rm command through actionable means.
    $ xargs rm < filelist.txt
    

    The above command will remove all of the files whose names we’ve listed in our text file named filelist.txt.

  4. The rm command is incapable of removing files with dashed filenames without using a specific option. Because Linux commands typically use dashes for the command line options. So, in this scenario we type the rm command followed by a double dash (–).
    $ rm -- -dashfile.txt
    

    This command removes -dashfile.txt with the double dash option (–).

  5. You can use the rm command with wildcard characters to remove multiple files. Removing multiple files in this way is different from rm [file]. . . [file] . . ., as this will only remove files named example1.list, example2.list, example3.list.
    $ rm example[123].list
    

    This command will remove all three of our aforementioned example files.

Closing Thoughts

You should now have a pretty solid grasp of the rm command. In this article, you learned how to use the rm command to remove files and directories with easy to understand examples. We covered the basics of the rm command and advanced applications of its command line options.