Removing files and directories is a common task on Linux to free up disk space and remove duplicate, and unnecessary content. This operation can be easily achieved due to the large variety of built-in commands. Among these, there is a powerful “rm” command line utility that performs this task efficiently on both files and directories if the currently logged-in user has their permissions or not. Moreover, this functionality can also be achieved through external Linux packages.
- What is the “rm” Command in Linux?
- What is the “rm -rf” Command in Linux?
- Why Do We Need the “rm -rf” Command in Linux?
- How Does the “rm -rf” Command Work in Linux?
- How to Use the “rm -rf” Command?
- What Happens When Using “sudo rm -rf /” and “sudo rm -rf/*” Commands in Linux?
- How to Create an Alias of the “rm -rf” Command in Linux?
- What are the Alternatives to the “rm -rf” Command in Linux?
- How to Install Trash-cli Package?
- How to Use Trash CLI to Delete the Directory and Restore it From Trash?
Let’s first start with the basics of the “rm” command.
What is the “rm” Command in Linux?
Linux “rm” is the “remove” command that deletes the specified files from the file system. It does not show any prompt or confirmation box before performing this task. Moreover, it also performs the deletion process permanently and the user cannot recover the deleted content. Hence it is recommended to double-check the content before deleting it ensuring that you target the correct file/directory.
After getting the basics of the “rm” command, now move on to its extended version which is “rm -rf”.
What is the “rm -rf” Command in Linux?
The “rm -rf” command is the combination of “-r” and the “-f” supported options. This command deletes the specified directory “recursively” and “forcefully”. It does not require administrator privileges and deletes the specified directory whether the currently logged-in user has the permissions or not.
This command deletes the specified directory recursively from the system and the trash can.
For better understanding use the “-r” and “-f” options separately along with the “rm” command.
Why Do We Need the “rm -rf” Command in Linux?
By default, the “rm” command only works on the file and generates the following error on deleting the directory:
However, the user must specify the “-r or –R” supported option with the “rm” command to perform the deletion of the specified directory in this way:
rm -r sampleDir
It can be observed that now the specified directory has been deleted successfully from the system:
Like the “-r” flag, the “-f” also plays a significant role. It deletes the specified file forcefully whether the currently logged-in user has its permissions or not.
Suppose there is a text file in the system, and the current user has only its read permissions. If this user deletes the given file, then the “rm” command asks for permissions:
To avoid the above-asked permission, use the “-f” flag along with the “rm” command to delete the specified file forcefully:
rm -f notes.txt
Now the “rm” command removes the given file(having read-only permissions) forcefully without asking any permission:
Hence the “rm -rf” command is used for the deletion of the specified directory forcefully without generating any error or asking for any permissions.
How Does the “rm -rf” Command Work in Linux?
The working of “rm” command works on its below-stated generalized syntax:
rm -rf [directory(s)]
In the above syntax the “[directory(s)]” refers to the unwanted single or multiple directories that the user wants to delete forcefully via the “rm -rf” command.
Let’s remove the files permanently and forcefully from the current Linux system.
How to Use the “rm -rf” Command?
This section carries out all possible use cases to show the practical implementation of the “rm -rf” command telling how it deletes the directories recursively and forcefully:
Example 1: Use the “rm -rf” Command to Delete Directories Recursively and Forcefully
This example uses the “rm -rf” command to delete single or multiple directories recursively and forcefully.
Delete Single Directory
In this scenario, the “ls -l” command lists the content of the current working directory “home”. Among these, there is a “myDirectory” that has only “read-only” permissions:
Specify the name of the above-highlighted directory prepending with the “rm -rf” command to delete it recursively and forcefully from the system:
rm -rf myDirectory
The given directory has been successfully removed from the system without asking any permissions:
For more verification, execute the “ls” command followed by the deleted directory name to ensure whether it has been deleted or not:
The below snippet verifies that now there is no such file or directory named “myDirectory”:
Delete Multiple Directories
To delete multiple write-protected directories specify their names next to the “rm -rf” command in this way:
rm -rf notes sample demo
The successful execution of the above command removes the specified write-protected directories from the system forcefully:
Like the “rm -rf” command type the names of deleted directories along with the “ls” command for verification purposes:
ls notes sample demo
The output confirms that there are no such directories in the current working system:
Example 2: Use the “rm -rf” Command With the “v” Flag to Display Information During the Deletion Process
It can be seen in the first example, that the user is required to execute an additional “ls” command to confirm whether the specified directory has been removed from the current working system or not.
To avoid the execution of the extra command, use the “-v” flag along with the “rm -rf” to get the details of the deletion process:
rm -rfv sampleDir
It shows the details at the time of the deletion process verifying the specified directory is removed:
Example 3: Use the “rm -rf” Command With Administrator Privileges to Delete a Directory Created by the Root User
The “rm -rf” requires administrative privileges to perform the deletion operation on a directory that is created by the root user.
Suppose there is a sample directory named “parentDir” and has a “subDir”, and both are created by the root(sudo) user via the “mkdir” command
sudo mkdir parentDir && sudo mkdir parentDir/subDir
Now, execute the “rm -rf” command to delete the parent directory without the administrator privileges:
rm -rf parentDir
The execution of the above command generates the “Permission denied” error because only the root user can delete the specified directory:
Hence, execute the “rm -rf” command along with the “sudo” having the root user or administrator privileges to delete the specified directory:
sudo rm -rf parentDir
Here the output confirms that the specified root user’s directory has been removed from the system successfully:
That is all from this section.
What Happens When Using “sudo rm -rf /” and “sudo rm -rf/*” Commands in Linux?
The “rm -rf” is a dangerous command and the user should use it very carefully while deleting the system files or directories. This is because it can damage or crash the whole system by typo mistakes.
Dangerous Type Mistakes
The most serious and barely noticeable mistakes are specifying the “forward-slash(/)” and sudo privileges with it like “sudo rm -rf /” and the “forward-slash(/)” followed by “asterisk(*)” such as “sudo rm -rf /*”.
Let’s first start with the “sudo rm -rf/” command.
“sudo rm -rf /” Command
In Linux, everything is connected to the “root(/)” directory and if the user deletes the root directory, then it means the file system of Linux will be removed. Hence it is dangerous to execute the “rm -rf” command having root user (sudo) privileges and specifying the “/” root directory.
In case, the user executes this command unintentionally to check what happens:
sudo rm -rf /
The above command execution shows a warning message that “it is dangerous to delete the root directory recursively”.
Furthermore, it also gives a suggestion to use the “–no-preserve-root” option to override the highlighted warning and delete all files and directories of the system.
“sudo rm -rf / *” Command
The “sudo rm -rf / *” command works the same as the “sudo rm -rf / ” but with some additional functionality due to “*(asterisk)”. The “*” represents multiple files of the same extension with different names. Here, in this scenario, it denotes the current working directory and all its subdirectories.
Therefore, It is considered the most dangerous command because it deletes the root directory recursively and forcefully without prompting any confirmation box. As a result, the whole system will crash:
sudo rm -rf / *
How to Create an Alias of the “rm -rf” Command in Linux?
Alias is a simplified way of complex and multiple commands. It acts as a shortcut to execute the large complex commands that are difficult to remember.
This section creates an alias of the “rm -rf” command to delete the directory recursively and forcefully using a shortcut. To achieve this functionality, follow the below-given steps instructions:
Step 1: Create an Alias
First, specify the “alias” keyword followed by the desired “<alias-name>” equals to the command(“rm -rf”) whose alias needs to be created:
alias del="rm -rf"
The output shows the successful execution of the above command:
Step 2: Save the Changes
Next, execute the below-stated “source” command to save the changes permanently in the “~/.bashrc” file:
The above command saved the changes in the specified file successfully:
Step 3: Remove Directory Using Alias
Now, use the alias of the “rm -rf” command and delete the specified directory recursively and forcefully. After that execute the “ls” command for verification:
del demoDirls demoDir
It can be observed that the specified directory has been removed recursively and forcefully using the alias “del” of the “rm -rf” command:
It’s enough about deleting the targeted directory forcefully and recursively using the “rm -rf” command. Now move on to the other ways to achieve this functionality.
What are the Alternatives to the “rm -rf” Command in Linux?
As discussed above, the “rm -rf” command removes the specified directory from the system and from the trash can. Hence the user cannot recover it from Tash if it is required again.
In such a situation, Linux offers a safer alternative named “Trash-cli” to the “rm -rf” command with an additional safety feature. This package removes the specified directory or file from the system but not from the trash. Therefore, the user can recover the deleted content whenever required.
How to Install Trash-cli Package?
The “trash-cli” is an external Linux package that can be installed easily in any Linux distribution via its default package manager. The installation commands of most used Linux distributions are written below:
In this scenario, the “trash-cli” is installed in Ubuntu, the current working system with the help of its built-in “apt” package manager:
sudo apt install trash-cli
It can be observed that the installation of the “trash-cli” package is completed successfully:
Use the “yum” package manager to install the “trash-cli” package on RHEL or CentOS-based Linux distributions:
sudo yum install trash-cli
Use the below-stated “dnf” installation command to install the “trash-cli” package on the Fedora Linux distro:
sudo dnf install trash-cli
On Arch Linux
For Arch-Linux use its built-in “pacman” package manager to install the “trash -cli” package:
sudo pacman -S trash-cli
How to Use Trash CLI to Delete the Directory and Restore it From Trash?
Once the installation of “trash-cli” is done successfully, use its built-in commands to perform the deletion task based on their functionalities such as:
- trash-put: Deletes the specified files or directories.
- trash-rm: Remove the given individual file from the trash can.
- trash-list: Display deletes files and directories on the terminal.
- trash-restore: Restore the deleted file or folder using its trash id.
- trash-empty: Removes the trash can data.
In this scenario, the “trash-put”, “trash-list” and “trash-restore” commands of the “trash-cli” package are used.
Delete a Directory
Specify the directory name prepended with the “trash-put” command to delete the specified directory from the system:
trash-put demoDirls demoDir
The “ls” command verifies the successful deletion of the given directory:
List Down Trash List
For more verification, execute the “trash-list” command to check whether the specified directory is deleted from the system or not. Moreover, it only displays the content that is deleted using the “trash-put” command not others:
Here, the output shows the trash can content containing the above-deleted directory:
Restore the Deleted Directory From Trash
Now, to restore the deleted directory, run the below-stated “trash-restore” command:
The execution of the above command asks for the specified directory trash-id. Enter that id and hit the “Enter” key to continue the process:
Lastly, execute the below-provided “ls -l ” command to verify whether the deleted directory has been restored from the trash can or not:
It is verified that the deleted directory has been restored to its original location successfully:
That’s all about the rm -rf command and in Linux.
The Linux “rm -rf” command removes the specified directory recursively and forcefully. This command performs the defined task without prompting any confirmation box. Furthermore, it does not allow the users to recover the deleted directory again from the trash can. It is mostly used to remove the write-protected directories that can only be read. Moreover, if it executes with administrative privileges in the user’s home directory then it crashes the whole system by deleting the root(/) directory. This post has briefly illustrated the Linux rm -rf command.