In this publish, we will demonstrate you the sensible illustrations of the rm command in Linux. We will also show you the syntax of the rm command and the command line alternatives made use of with it.
Take note: Several have made use of –v (verbose) choice with all rm instructions. This possibility demonstrates what rm command is accomplishing in the qualifications.
Syntax of rm Command
The syntax of the rm command is:
$ rm [option]… [file]…
rm Command Illustrations
The examples of the Linux rm command are as follows:
Illustration 1: Take out a File
The most straightforward example of the rm command is to get rid of a file. For occasion, to remove a file, style rm followed by the filename:
This command will instantaneously eliminate the file named “testfile”.
In purchase to take away a file that is not in the current listing, point out the route to the file as follows:
$ sudo rm ~/Files/testfile
This command will right away eliminate the file “testfile1” situated in the “~/Documents” directory.
Case in point 2: Get rid of Multiple Files
To eliminate many data files at at the time working with a one command, kind rm followed by the file names:
$ sudo rm testfile1 testfile2 testfile3
This command will instantly remove the data files “testfile1”, “testfile2”, and “testfile3” found in the latest directory.
Illustration 3: Take out a File Interactively
The rm command removes data files without the need of asking for confirmation and there is no way to undo it as the taken out data files are not moved into the Trash. The rm command has an solution -i (interactive) that asks for confirmation ahead of eliminating the data files.
This command will inquire for affirmation that whether you want to commence with the procedure (removing the testfile). If you want to continue, style y, if not press n to abort the command.
Illustration 4: Eliminate a Write-shielded File
When you remove a publish-protected file, the rm command asks for affirmation. To quickly eliminate the file and overlook the confirmation, use the rm command with the -f (drive) choice.
This command will forcefully clear away the testfile without the need of asking for confirmation.
Instance 5: Clear away a Directory
The rm command can also be utilized for removing a directory and its written content recursively. For occasion, to take out a directory named “test_directory” and its content, use the -r (recursive) solution as follows:
$ sudo rm -v -r check_listing
Illustration 6: Clear away Anything from Existing Directory
If you want to take out almost everything from the existing directory, use the rm command with wildcard character as follows:
This command will take out all the information and folders from the present functioning directory.
Case in point 7: Get rid of Vacant Directories
For taking away an empty directory, use the rm command with the -d solution as follows:
$ sudo rm -v -d take a look at_dircetory1
This command will quickly clear away the vacant directory named “test_dircetory1”.
Having said that, if the directory is not empty, it will exhibit the “Directory not empty” information.
Illustration 8: Get rid of the Root Directory
By default, the rm command does not enable to recursively take out all the things from the root directory.
Having said that, if you truly want to do so, it can be performed using the rm command –no-preserve-root option.
$ sudo rm -v -r –no-protect-root /
This command does not address the root “/” specifically and gets rid of all the information located inside the root partition alongside with the mounted documents inside it.
Example 9: Take out Filenames Listed in a Textual content File
To take out a massive amount of data files, record them in a text file. Then use the xargs to browse that listing and pass it to the rm command.
The to start with step you have to do is to checklist all the data files in a textual content file.
Then to take away all the documents shown in the textual content file, use the adhering to command:
$ sudo xargs rm -v < list.txt
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:
$ sudo rm -v — -sample.txt
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:
$ sudo rm -v sample.list
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.