The rm Command in Linux

In this Linux guide, we will be showing you how you can use the rm command to delete files or directories.

rm command in Linux

The rm command stands for “remove” and is used to delete files and directories on Linux/Unix based systems.

The way the command works is by unlinking the filename within the filesystem from its associated data. That space then gets marked as writeable, and the operating system will be able to write data to those locations when it wants to.

This means that when you remove a file using this command, the data will still exist on the hard drive.  However, it no longer has a filename attached to it and is now marked as “free” space.

Even though the data still exists on the storage device, it is now inaccessible as the link has been deleted. It is not possible to undo a file removal when you use the rm command.

If you would like to remove the file from a directory altogether, you will have to use something like the shred command instead.

The shred command works by overwriting the contents of the file with empty data (Often Zeroes), making the original data completely unrecoverable.

Syntax of the rm Command

The syntax of the rm command is relatively simple. It takes two arguments. One of which is entirely optional.

rm [OPTIONS] FILES/DIRECTORIES...

The first argument lets you input options that will control the removal behavior of this command.

We will go over some of the options that you can use with this command later on in this guide.

The second argument allows you to specify the file or directory that you want to be removed from your filesystem.

It is possible to specify multiple files and directories to remove.

Using rm to Delete a Single File

The most basic usage of the rm command involves removing a single file.

For example, if we wanted to remove a file called pimylifeup, we can run the following command.

rm pimylifeup

Please note that you cannot delete a directory without using additional options.

Deleting Multiple Files With the rm Command

It is also possible to add multiple files to the command to remove. You do this by specifying them after the first filename.

You need to use a single space to separate them (filename1 filename2).

rm FILENAME1 FILENAME2 FILENAME...

By using this we, can remove, for example, a file called pimylifeup1 and pimylifeup2 by using the command below.

rm pimylifeup1 pimylifeup2

Removing Directories using the rm Command

By default, you will not be able to delete directories using this command.

To remove a directory using the rm command, you will need to utilize the -r option.

rm -r FILES/DIRECTORIES...

The r of this option stands for recursive and tells rm to remove all files and directories recursively. This means if you have several subdirectories and files within the specified directory, they will be deleted.

For example, if we had a directory called pimylifefolder, we can delete it and any files and directories within it by running the following command.

rm -r pimylifeupfolder

You can also still remove multiple files and directories by using this option.

For example, if we wanted to remove a file called pimylifeup and a directory called pimylifeupfolder we can use the command like below.

rm -r pimylifeup pimylifeupfolder

Force removing Files / Directories

If any file is write-protected, you will be met with a prompt from the rm command asking if you want to remove it. You can check what permissions are set for a file/directory by using the ls command.

To show you this message, we have provided an example of removing a file with read-only permissions.

$ rm pimylifeup
rm: remove write-protected regular file 'pimylifeup'?

To avoid having to respond to this message for every file that is write-protected, we can utilize the -f option.

rm -f FILES...

This option tells the rm command to force the removal of the file. It will suppress any warning messages and try to remove the file regardless of any prompts.

For example, if we have a file called pimylifeup with read-only permissions, we can force remove it by using the following command.

rm -f pimylifeup

This option will also work alongside the recursive argument to remove directories.

rm -rf pimylifeupfolder

Be careful when using the recursive (-r) and force (-f) options together. Misusing these can quickly remove a large number of files, and when combined with superuser privileges, you can end up damaging your operating system.

Interactively Removing Files with rm

The rm command also can ask you if you want a file to be removed.

The option that allows this to happen is the -i option, also known as the --interactive option.

This option can be useful to stop you from accidentally permanently deleting files that you did not want to.

rm -i FILES...

With this option enabled, you will be greeted by the following message every time a file is about to be deleted.

To delete the file, you will need to respond with Y. Otherwise, you will need to respond with N.

$ rm -i pimylifeup
rm: remove regular file 'pimylifeup'?

It can be especially useful to combine this option with the recursive option. The reason for this is that it can stop you from doing a lot of damage quickly.

Deleting Files that Start with a Hyphen

If you have ever needed to delete a file with a filename that starts with a hyphen (-), you will find that the rm command will get confused.

The reason for this is that by seeing the hyphen (-), the command will think that you are trying to specify an option.

To get around this, we need to specify an empty option. We do this by using two hyphens (--) before we specify a filename.

rm -- -FILENAME

For example, if we wanted to remove a file called -pimylifeup we can use the following command.

rm -- -pimylifeup

Hopefully, at this point, you will now understand how to use the rm command on a Linux or Unix based operating system.

If you have any suggestions or have run into any issues, please leave a comment below.

Be sure also to check out our other Linux command guides and Linux tutorials.

Leave a Reply

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