Search code examples
linuxcommandrm

what's the difference between rm -rf and rm -r?


In linux, what is the difference between 'rm -rf' and 'rm -r', both seem to do the same things (delete an entire directory).

Here is a few commands that I ran to test it:

mohammad@mohammad-ThinkPad-E570:~/testerr$ ls
mohammad@mohammad-ThinkPad-E570:~/testerr$ mkdir foo1 foo2
mohammad@mohammad-ThinkPad-E570:~/testerr$ touch foo1/main.java foo2/main.java
mohammad@mohammad-ThinkPad-E570:~/testerr$ tree
.
├── foo1
│   └── main.java
└── foo2
    └── main.java

2 directories, 2 files
mohammad@mohammad-ThinkPad-E570:~/testerr$ rm -r foo1
mohammad@mohammad-ThinkPad-E570:~/testerr$ ls
foo2
mohammad@mohammad-ThinkPad-E570:~/testerr$ rm -rf foo2
mohammad@mohammad-ThinkPad-E570:~/testerr$ tree
.

0 directories, 0 files
mohammad@mohammad-ThinkPad-E570:~/testerr$ 

Solution

  • -f option is there to remove the prompts.

    -r option is there to work recursively.

    Let's say that we have a folder named stackoverflow with the contents of image.jpg otherimage.jpg mydog.doc

    Upon typing rm -r stackoverflow terminal may say: rm: descend into write-protected directory 'stackoverflow'? and if you say y it will ask you for new questions.

    rm: remove write-protected regular file stackoverflow/image.jpg'?
    rm: remove write-protected regular file stackoverflow/otherimage.jpg'?
    rm: remove write-protected regular file stackoverflow/mydog.doc'?
    

    Basically, it will ask every step if you want to do this operation or not.

    Now let's try with rm -rf stackoverflow

    No questions will be asked this time and, all the content inside the folder is now deleted.