The Linux operating system mostly relies on command line operations which are carried out through the terminal. The “rm” command is beneficial for deleting or removing the files in the specified directory. The “rm” command performs the special tasks as per requirements by using the supported options. This post will provide a detailed working and usage of the “rm” command in Linux. The outcomes of the post are:
How Does rm Command Work in Linux?
The main objective of the “rm (remove)” command is to remove files from the available directories from the current working directory. The “rm” command depends on the syntax given below:
The syntax of the “rm” command is defined below:
$ rm [option] [Files]
The “rm” is the main keyword in the command, whereas the “options” provide the functionality of the “rm” command in Linux.
The options of the “rm” command are listed below:
|-i||Ask the user for confirmation before deleting the specific file.|
|-f||Forcefully deletes the mentioned files.|
|-d||Deletes the empty directories from the system or a particular directory.|
|-r||Helps to remove the directories.|
|-I||Remove more than three files at a time.|
|-v||Shows the execution status of the command, i.e., prints the removal of the files/directories.|
The basic working of the “rm” command and the options can be viewed by utilizing the rm “help” command:
$ rm --help
Let’s get into the usage of the rm command.
How to Use rm Command in Linux?
Usually, the “rm” command removes the files and helps free the disk space. This section comprises various examples of the “rm” command with different options that can be utilized to get the particular output according to the requirements.
Let’s see the first example.
Example 1: Remove a Single File in the Directory
The simple“rm” command removes a single file from the directory. For this purpose, type the “rm” command and then mention the specific file name the user wants to remove, as shown in the screenshot:
$ rm File1.txt
The above command removed the “File1.txt” that was present in the home directory.
Example 2: Delete Multiple Files From Specific Directory
The “rm” command is also beneficial for deleting several files from the current or specific directory. For example, the “Pictures” directory has a list of files such as “file2”, “file3”, and “file4” that can be shown via the “ls -l” command:
$ ls -l
Execute the “rm” command with the file names available in the pictures directory:
$ rm file2.txt file3.txt file4.txt
Run the “ls” command to verify that “file2.txt”, “file3.txt”, and “file4.txt” has been removed from the entire “Pictures” directory.
Example 3: Delete All Files in the Directory
All the files placed in the current or specific directory can also be removed via the “rm” command. Suppose, in this case, the “Mydir” contains three files as shown below:
$ ls -l
Type the following command in the terminal that will delete/remove all the files from the “Mydir” directory:
$ rm Mydir/*
For the verification again run the “ls” command that displays that there is no file in the current “Mydir” directory.
Example 4: Ask for Permission Before Deletion
The “-i” option of rm commands is used for confirmation before deleting or removing a file from the directory. For example, to delete the file “file8.txt”, run the below-written “rm” command with the “-i” option run the on the terminal:
$ rm -i file8.txt
After running the above command, press the “y” key to continue the “file8” deletion process:
Hence it is verified that “file8” is successfully deleted from the current Home directory.
Example 5: Delete the Empty Directories
The “-d” option of rm commands helps to delete all the empath directories from the current or a particular directory. For example, there are five empty directories in the “MainDir” directory as shown below:
$ ls -l
To delete two of the displayed directories “Mydir1” and “Mydir2”, execute the “rm” command with “-d” option:
$ rm -d Mydir1 Mydir2
In the end, the “ls” command verified that “Mydir1” and “Mydir2” had been deleted.
Example 6: Delete the Non-Empty Directory
The “-r” option of rm commands helps delete the directories and their content, e.g., files and subdirectories. However, the simple “rm” command can not delete the directory. In this case, the directory “Mydir2” holds some files displayed through the “ls -l” command:
$ ls -l
Execute the “rm -r” command to completely delete the “Mydir2” directory from the currently installed Linux system:
$ rm -r Mydir2
The successful execution of the command ensures that the directory has been removed.
Example 7: Forcefully Remove the Files
The “-f” option of rm commands forcefully delete the file from the directory. Suppose there is a file “file13.txt” the “rm -f” option will delete this file forcefully from the directory:
$ rm -f file13.txt
Example 8: Remove All the Files of Specific Extension
The “rm” command is helpful in such a way that it allows the users to delete the files with extension names, e.g., .png, .txt, JPEG, and many others.
For instance, the command provided below will remove all the text files from the current directory:
$ rm -v *.txt
The “-v” option shows the confirmation of the deletion of the files.
In Linux, the “rm” command can delete or remove files from the specific or the current working directory. The general syntax of the “rm” command is “rm [options] [Files]”. It is also beneficial to remove directories from the Linux system. Following this post, you have learned the working and usage of the “rm” command in Linux.