The “rm” command in Linux OS is used to remove the files and directories from the command line. However, the removed files and directories do not get moved to the Trash. Instead, the “rm” command removes the files and directories permanently. Hence, you should be careful while using these commands as you will not be able to recover the removed files and directories unless you have a backup.
In this post, we will show you the practical examples of the rm command in Linux. We will also show you the syntax of the rm command and the command line options that are used with it.
- How Does the RM Command Work in Linux?
- Remove a File
- Remove a File in a Specified Directory
- Remove Multiple Files
- Remove a File Interactively
- Remove a Write-Protected File
- Remove a Directory
- Remove Everything from the Current Directory
- Remove the Empty Directories
- Remove the Root Directory
- Remove the File Names Starting with Dash (-)
- Remove Specific Extension Files
Let’s start with the rm command.
How Does the RM Command Work in Linux?
The rm command is used to remove the files and directories in Linux. This is one of the commands that you must know as a Linux System Administrator to manage the files and directories.
The syntax of the rm command is as follows:
Different options of the rm command are as follows:
|-v||To remove a test.txt file from your current working directory|
|-i||(To confirm) you before removing every file|
|-Iv||To confirm whether you want to delete more than 3 files|
|-d||To remove empty directories|
|-rfv||To remove every file and subdirectories|
|-rvi||It keeps asking you to confirm the removal.|
To explore more options of the “rm” command, use the following “help” utility:
The examples of the Linux rm command are as follows:
Example 1: Remove a File
The simplest example of the rm command is to remove a file. For instance, to remove a file, type rm followed by the filename:
This command instantly removes the file named “testfile1”.
Example 2: Remove a File in a Specified Directory
To remove a file that is not in the current directory, mention the path to the file as follows:
This command instantly removes the “test.txt” file located in the “~/Documents” directory.
Example 3: Remove Multiple Files
To remove multiple files at once using a single command, type rm followed by the file names:
This command instantly removes the “filename1”, “filename2”, and “filename3” which are located in the current directory.
Example 4: Remove a File Interactively
The rm command removes the files without asking for confirmation and there is no way to undo it since the removed files are not moved into the Trash. The rm command has a -i option (interactive) that asks for confirmation before removing the files.
This command asks for confirmation whether you want to proceed with the operation (removing the office.txt). If you want to proceed, type y. Otherwise, press n to abort the command.
Example 5: Remove a Write-Protected File
When you remove a write-protected file, the rm command asks for confirmation. To instantly remove the file and ignore the confirmation, use the rm command with the -f (force) option.
This command forcefully removes the “sample.txt” without asking for confirmation.
Example 6: Remove a Directory
The rm command can also be used to remove a directory and its content recursively. For instance, to remove a directory named “testdir2” and its content, use the -r (recursive) option as follows:
Example 7: Remove Everything from the Current Directory
If you want to remove everything from the current directory, use the rm command with a wildcard character as follows:
This command removes all the files and folders from the current working directory.
Example 8: Remove the Empty Directories
To remove an empty directory, use the rm command with the -d option as follows:
This command instantly removes the empty directory named “testdir”.
However, if the directory is not empty, it displays the “Directory not empty” message.
Example 9: Remove the Root Directory
By default, the rm command does not allow the recursive removal of everything from the root directory. However, if you really need to do so, it can be done using the rm command –no-preserve-root option.
This command does not specially treat the root “/”and removes all the files located inside the root partition along with the mounted files inside it.
Example 10: Remove the File Names Starting with Dash (-)
There are some files whose names start with a dash like “-sample.txt”. To remove such a file using the rm command, you cannot simply use “rm test” since Linux commands use dash (-) for the command-line options.
So, to remove a file whose name begins with a dash (-) like “-test”, use the double dash (–) as follows:
Example 11: Remove Specific Extension Files
You can use the wildcard character with the rm command to selectively remove a subset of files. Let’s look at a few examples:
To remove all the files in your current directory whose names end with a specific string like “.txt”, the command would be:
This command removes all the files that end with “.txt” in their names like “test.txt”.
The Linux rm command is one of the GNU Core Utilities. It allows you to remove the files and directories in Linux. Most of the administrators use the rm command to manage the files and directories. In this post, we covered how to use the rm command along with some examples.