• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

How to use sudo rm -rf safely

Thumbnail for Neeran GulNeeran Gul

Neeran Gul

Staff Site Reliability Engineer, Mozn

Published: 2/1/2024

About Terminus

sudo rm -rf is a highly destructive action. Typically, the meaning of sudo rm-rf is that you are force-deleting all directories and files as a superuser. We are going to break down this command and understand what each step does. Understanding its components can help you create a safer, simpler command - or make sure you only run it in the most controlled environments.

sudo and rm usage

The first part of the command is sudo, sudo allows us to execute a command as another user. By default sudo will run anything after sudo as the root super user. Running as the root user means that this command can pretty much do anything as it has access to all files on the file system. 

The second part of the command is rm. rm is used to delete files and directories on the file system. How does rm work? Let’s look at an example:

 $ touch test.txt
 $ rm test1.txt
 $ mkdir test
 $ rm test
 rm: hello: is a directory

Run in Warp

As we can see, we can successfully delete files, but we cannot use the plain rm command to delete directories. Let’s see how we can delete directories using the rm command.

 $ rm -r test
 $ ls
 # test directory should be deleted

Run in Warp

Above, we show that we can delete directories by passing the -r flag.

Let’s have a look at an example of what using the -f flag means.

 # change to root user
 $ sudo su -
 $ touch cannotdelete.txt
 
 # check it’s owned by root
 $ ls -al cannotdelete.txt
 -rw-r--r--    1 root       root        0 12 Nov 21:43 cannotdelete.txt
 
 # switch to normal user
 $ exit 
 $ rm cannotdelete.txt 
 override rw-r--r-- root/root for cannotdelete.txt?
 $ rm -f cannotdelete.txt
 
 # file is deleted without confirmation

Run in Warp

-f is the force flag, it attempts to remove files without any prompt regardless of permissions.

Deleting multiple files and directories

 # delete multiple files
 $ touch test1.txt test2.txt test3.txt

 # delete only txt files using glob wildcard, *.txt resolves to all files ending with .txt
 $ rm *.txt

 # delete files and directories
 $ touch test1.txt test2.txt test3.txt 
 $ mkdir test1 test2 test3
 $ rm -rf *

 # delete only files in directory
 $ mkdir test1 

 # don’t want any prompts
 $ sudo rm -rf test1/*

 # delete the directory
 $ rm -r test1

Run in Warp

The examples above illustrate when we should use -r and when we should use -f. Essentially, -rf is us saying “I don’t care if it’s files or directories; just delete it”. 

Be Careful With its Usage

So when does it turn destructive? When using rm, the feedback from the terminal is minimal when a file is deleted. It is very easy to delete files, meaning you can end up deleting important files by mistake.

Here’s an example of a particularly scary command:

 # DO NOT RUN THIS
 $ sudo rm -rf /

 # This is just as bad
 $ sudo rm -rf /*

Run in Warp

On Unix systems, the operating system is on the / path. If someone runs the above command, rm will attempt to delete ALL files and directories under the root of your filesystem. Since sudo has been passed, the command is run as the superuser, typically root. This means that all your system files, applications and binaries will be deleted without any prompts, silently. The only way to recover is to restore from a backup or snapshot. Be extra wary and make sure to avoid using sudo rm -rf within any scripts or automation - especially when the path argument is parameterized.

Written by

Thumbnail for Neeran GulNeeran Gul

Neeran Gul

Staff Site Reliability Engineer, Mozn

Filed Under

Related Articles

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash
Thumbnail for Gabriel ManricksGabriel Manricks

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Bash

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Use Cookies With cURL

Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.

Bash

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Thumbnail for Razvan LudosanuRazvan Ludosanu

How To Use sudo su

A quick overview of using sudo su

LinuxUnixBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Thumbnail for Brett TerpstraBrett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Thumbnail for Zev StravitzZev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Thumbnail for Amit JotwaniAmit Jotwani

Bash Aliases

Create an alias for common commands

BashLinuxUnix
Thumbnail for Brett TerpstraBrett Terpstra

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null