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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
rm command in Linux Basic Examples
- Removing a file with the
rmcommand is very simple. We can achieve this by typing
rmfollowed 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.isofile located in the
- If you use the
rmcommand without extra options to remove write-protected files, the
rmcommand asks for confirmation. The
rmcommand can be used with the
-foption 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.txtfile without the need to give it the go-ahead.
- In our previous example, we showed how the
rmcommand 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
-ioption for this. You should use this to avoid removing files accidentally, as the
rmcommand 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
ywill tell the command to proceed with the operation, and typing
nwill abort the command.
- Up until now, we’ve shown that the
rmcommand can remove a single file from a given directory, but it can also remove multiple files at once. We can use the
rmcommand 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
file2in our current working directory.
- As we mentioned at the beginning of this article, the
rmcommand can permanently remove both files and directories. Using the
-roption, we can remove an entire directory and its contents.
$ rm -r directory01
This command permanently removes
directory01and any files or subdirectories that it contains.
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.
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
- To swiftly erase all of the files in the current working directory, one would simply use an asterisk following the
rmcommand. This command does not remove the directory itself, only its contents.
$ rm *
- Removing an empty directory requires slightly different steps. That’s where the
-doption comes in.
$ rm -d oldfiles
This command will permanently remove our empty directory named
- You can utilize the
rmcommand in a very fascinating – but equally helpful – way by running it alongside the
xargscommand. 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
xargscommand will be able to read that list, and feed the data to the
rmcommand 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
rmcommand 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
rmcommand followed by a double dash (–).
$ rm -- -dashfile.txt
This command removes
-dashfile.txtwith the double dash option (–).
- You can use the
rmcommand 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
$ rm example.list
This command will remove all three of our aforementioned example files.
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.