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 used with it.
Note: Few have used –v (verbose) option with all rm commands. This option shows what rm command is doing in the background.
Syntax of rm Command
The syntax of the rm command is:
rm Command Examples
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 will instantly remove the file named “testfile”.
In order to remove a file that is not in the current directory, mention the path to the file as follows:
This command will instantly remove the file “testfile1” located in the “~/Documents” directory.
Example 2: Remove Multiple Files
To remove multiple files at once using a single command, type rm followed by the file names:
This command will instantly remove the files “testfile1”, “testfile2”, and “testfile3” located in the current directory.
Example 3: Remove a File Interactively
The rm command removes files without asking for confirmation and there is no way to undo it as the removed files are not moved into the Trash. The rm command has an option -i (interactive) that asks for confirmation before removing the files.
This command will ask for confirmation that whether you want to proceed with the operation (removing the testfile). If you want to proceed, type y, otherwise press n to abort the command.
Example 4: 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 will forcefully remove the testfile without asking for confirmation.
Example 5: Remove a Directory
The rm command can also be used for removing a directory and its content recursively. For instance, to remove a directory named “test_directory” and its content, use the -r (recursive) option as follows:
Example 6: Remove Everything from Current Directory
If you want to remove everything from the current directory, use the rm command with wildcard character as follows:
This command will remove all the files and folders from the current working directory.
Example 7: Remove Empty Directories
For removing an empty directory, use the rm command with the -d option as follows:
This command will instantly remove the empty directory named “test_dircetory1”.
However, if the directory is not empty, it will display the “Directory not empty” message.
Example 8: Remove the Root Directory
By default, the rm command does not allow to recursively remove 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 treat the root “/” specially and removes all the files located inside the root partition along with the mounted files inside it.
Example 9: Remove Filenames Listed in a Text File
To remove a large number of files, list them in a text file. Then use the xargs to read that list and pass it to the rm command.
The first step you have to do is to list all the files in a text file.
Then to remove all the files listed in the text file, use the following command:
This command will remove all the files listed in the text file “list.txt”.
Example 10: Delete 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 -sample.txt” as Linux commands use dash (-) for the command-line options.
So to remove a file whose name begins with a dash (-) like “-sample.txt”, use the double dash (–) as follows:
Example 11: Use Wildcards with rm
You can use the wildcard character with the rm command to selectively remove a subset of files. Let’s look at few examples:
1. To remove all the files in your current directory whose names end with a specific string like “.txt”, the command would be:
This command will remove all the files that end with .txt in their names like “test1.txt”, “test2.txt”, “test3.txt”, and “test4.txt”.
2. To remove all the files in your current directory whose names begin with a specific string like “user”, the command would be:
This command will remove all the files that begin with the string “user” in their names like “user1”, “user2”, “user3”, and “user4”.
3. To remove multiple files, you can also use a wildcard like this:
This command will remove the files named “sample1.list”, “sample2.list”, “sample3.list”, and “sample4.list”.
The Linux rm command is one of the GNU Core Utilities. It allows you to remove files and directories in Linux. In this post, we covered how to use the rm command along with some examples. To view more information, view the rm man page.