The Linux rm command helps with deleting files and directories. However, the command may be new to you, and you have yet to understand all the various ways you can use it to delete files and unwanted directories.
In this post, we will walk you through all the different approaches that you can utilize to delete files and directories using the Linux rm command. Additionally, we will give bonus tips on the other two commands that you can use to delete target files.
How to Use the Linux rm Command
Before we give the various approaches to use with the Linux rm command, it’s fair that we discuss its syntax and the different options you can use with the command.
The rm command follows the syntax below.
$ rm [options] [file/directory]
Having seen the syntax, let’s discuss some of the commonly used options when working with the Linux rm command.
Options | Description |
-v | It displays the verbose output for the process. |
-r | It recursively removes a directory and its contents. |
-i | It prompts before deleting a file or directory. |
-f | It forcefully deletes a file or directory without prompting. |
–help | It displays the rm command help page. |
Different Approaches for Deleting Files and Directories using the Linux rm Command
According to its manual page, the Linux rm command removes files or directories. This section gives examples of how to implement the rm command. Before we get there, let’s create the files that we will use for demonstration.
To create our files, we are using the Linux touch command to create multiple empty files. We’ve then verified that the files are created using the Linux ls command.
With that, let’s start the juicy part of learning how to delete files and directories.
Example 1: Linux rm Command Delete File(s)
Deleting a file using the rm command only requires you to run the rm command and specify the target file. If the file is in a different location, specify its path, and it will be deleted.
For instance, we’ve run the rm command to delete the ‘update.sh’, and the ls command verifies that the deletion was successful.
$ rm update.sh $ ls
Instead of deleting a single file, you could also delete multiple unwanted files. There are two approaches you can use. First, if the files share the same extension, you can use the extension in the Linux rm command to match all matching files.
For instance, to delete all text files, you would run the command below.
$ rm *.txt
Alternatively, you can manually add the names of all the files you want to delete, as shown in the example below.
$ rm file1.txt names.txt file4.txt
Example 2: Deleting Write-Protected Files
Sometimes, you may try running the Linux rm command only to get prompted to confirm “remove write-protected regular file?”
Such a case occurs when you try to remove a file that you don’t have the write file permissions, denying you room to delete it freely. If you get such an instance, press the ‘y’ keyboard key and hit the Enter key to confirm the delete action.
Example 3: Linux rm Command Delete Files interactively
When you have the write permissions on a file, you won’t get prompted to confirm deleting it. While this is great, it can lead to accidental deletion of files.
Luckily, you can avoid this by adding the -i option to prompt your confirmation before deleting any file.
Here’s an example.
$ rm -i file2.txt
Notice how, although we have a regular file, we still get prompted, and the file won’t get deleted until we authorize it.
Having to add the -i option every time you want to delete a file using the Linux rm command safely can be tricky, as there is no undo option for the command. The better approach is to create an alias for the rm command to use the rm -i.
To do so, open the .bashrc file using a text editor. We’ve used the nano text editor for this example.
$ sudo nano .bashrc
Once you open the file, add the line below.
alias rm=”rm -i”
Save the changes and exit the file.
You must then run the command below to apply the changes.
$ source .bashrc
With that, we now don’t need to add the -i option when running the command. Running the rm command will automatically prompt us to confirm that we want to delete the file.
Example 4: Verbose Output of Linux rm Command
Even when deleting files or directories, it makes sense to see information for the executed process. By adding the -v option, every rm process you run will display an output of what action gets executed.
For instance, deleting all text files and adding the -v option will display the output below.
$ rm -v *.txt
Example 5: Forcefully Remove a File
If, for some reason, you don’t want to get prompted to confirm deleting a write-protected file, you can use the -f option to bypass the prompt.
However, only use this option when sure of what file you want to delete to avoid accidental deletions of crucial files.
For instance, the ls -l command shows that our target file doesn’t have the write permission for the active user.
$ rm -i file2.txt
Hence, we will get the prompt below when we try deleting the file.
However, if we add the -f option, we manage to delete the same file without getting any prompt.
$ rm -fv hosts.xml
Working with Directories
So far, we’ve only discussed how to delete files. This section dives into using the Linux rm command to delete directories.
First, let’s run the Linux mkdir command to create a few directories.
Example 6: Deleting a Directory Using the Linux rm Command
When you try to delete a directory using the rm command, you will get an error that the target is a directory and hence can’t be removed.
To bypass this error, add the -r option that tells the rm command to delete the directory and its contents recursively.
Our new command would be as follows.
$ rm -rv dir1
The -v adds verbose to the output and confirms that our directory has been removed.
When it comes to deleting directories, the rmdir command provides a better approach to deleting them instead of the rm command. Read our comprehensive guide on using the rmdir command to learn more about it.
Example 7: Deleting Directories Interactively
Similar to what we had when deleting files, adding the -i option will interactively delete a directory by prompting you to authorize the action.
Here’s an example.
$ rm -rvi dir1
Example 8: Forcefully Delete a Write-Protected Directory
If you don’t have write permissions in the directory, you will get prompted to confirm that you want to delete it.
However, adding the -f option will bypass the prompt and forcefully delete the directory.
$ rm -rfv dir2
Bonus Tips: Two Other Ways of Deleting Files in Linux
The Linux rm command is the official way of deleting files in Linux. Nonetheless, you can use two other commands to achieve the same goal.
1. Using the Unlink Command
The unlink command lets you remove a file in Linux using the syntax below.
$ unlink filename
Its drawback is that you can’t delete multiple files with the same command. It will throw an error like the one below.
2. Using the find Command
Yes, even with the find command, you can delete a file. The Linux find command searches for files. However, you can use the syntax below to delete the found file.
$ find -name filename -delete
If searching for files with the same extension, you can batch delete them as in the below example.
$ find -name ‘*.txt’ -delete
Final Take
The Linux rm command is a powerful command for deleting files and directories. We’ve presented examples demonstrating the various approaches you can take to quickly and effectively delete unwanted files and directories. Moreover, we’ve given two other bonus commands that you can use to delete files. With that, you no longer have to get stuck with unwanted files and directories in Linux.