What Does ‘sudo rm -rf /*’ do?

The command “sudo rm -rf /*” is a powerful command in Unix-like operating systems, including Linux and macOS. It allows users to delete files and directories recursively from the root directory. The root directory contains all essential files and sub-directories that are critical for the operating system to function. After deleting these files, the system can go to a halt.

This article will enlighten the “sudo rm -rf /*” command along with all possible examples in Linux.

How Does the “sudo rm -rf /*” Command Work in Linux?

When users execute the command “sudo rm -rf /*“, this will first ask for your password (because of the “sudo” command), and if you enter the correct password, the command starts executing.

This command is made up of several parts which are discussed here:

  • sudo” is a command that allows users to execute a command with administrative privileges.
  •  “rm” is a command that stands for “remove” and is used to delete files or directories.
  •  “-r” is the “recursive” option, that deletes not only the specified file or directory but also all of its contents, including any subdirectories and files. 
  • “-f” stands for “force”, which means that the command will not prompt the user to confirm the deletion of each file.
  • The “/” character is the root directory of the file system, which means that the command attempts to delete all files and directories from the root directory down, including system files and directories.

Here are some examples of using the “sudo rm -rf” command in Linux:

Example 1: Delete Root Directory and Its Content 

To delete the root directory and all its content, use the “rm -rf” command with the “/*”. It potentially deletes all the files on the computer:

sudo rm -rf /*

It deletes all files and directories in the root directory, including system files and directories.

Example 2: Delete Content of the Current Directory 

An example is carried out to delete all files and directories in the current directory via the “rm -rf” command with the ./*

$ sudo rm -rf ./*

It deletes all directories and files in the current directory in a recursive manner.

Example 3: Delete a Specific Directory and its Contents

An example is considered deleting a directory and its content via the “rm -rf” command. For instance, specify the name of the directory “/home/itslinuxfoss/Folder”:

$ sudo rm -rf /home/itslinuxfoss/Folder

It deletes the directory located at “/path/to/directory” and all of its contents, including any subdirectories and files.

Example 4: Delete Multiple Directories at Once

An example is carried out to delete multiple directories at once using the “rm -rf” command. In our case, specify the name of the directories as below:

$ sudo rm -rf /home/itslinuxfoss/Folder /home/itslinuxfoss/Folder2

It deletes the directories located at “/home/itslinuxfoss/Folder”, “/home/itslinuxfoss/Folder2” and all of their contents.

Example 5: Delete All Files with a Certain File Extension

To delete all files with the specific extension, use the “rm -rf” command by mentioning the extension. For instance, specify the “.txt” in the below command:

$ sudo rm -rf /home/itslinuxfoss/Folder*.txt

It deletes all files in the “/home/itslinuxfoss/Folder” directory having the “.txt” extension.

Conclusion

The “sudo rm -rf /*” command recursively deletes all files and directories from the root directory. With this command, users can delete files and directories in the current directory, delete multiple directories at once, and delete all files with a certain file extension.

This guide has covered each aspect of the “sudo rm -rf /*” command with practical implementation.