47

What does rm -rf do when used to remove files or directories?

How do the -r and -f options work together?

7
  • 6
    How come this hasn't been asked before? Sep 6, 2015 at 7:51
  • 17
    @DanDascalescu Because it's trivially answered by reading the man page, which should be anyone's first port of call to answer a question like this. Sep 6, 2015 at 13:08
  • 11
    Try it and see. (Spoiler: don't.)
    – geometrian
    Sep 7, 2015 at 1:29
  • 3
    By Golly ! This question must be a prank from DevRobot ! How could you possibly make it to your karma level without ever consulting the man pages. Now... are you human ?!
    – Cbhihe
    Oct 19, 2015 at 14:12
  • 2
    Totally reads like a prank indeed. As if someone tried to ask, "is rain made of H2O?" (A good answer would be: Normally yes, but in future, once man will have been destroyed half of the flora of the Earth, 50% of it will have turned to acid (as in acid rain)) Dec 23, 2015 at 9:51

5 Answers 5

73

The command rm -rf is the same as rm -r -f.

From rm's man page (type man rm in a terminal to see it) -r does:

remove directories and their contents recursively

And -f does:

ignore nonexistent files and arguments, never prompt

So in combination they do both.

In fact it is a very dangerous command because it will recursively remove everything within the directory you choose (or just remove the file you choose), and it will do so without ever prompting you.

Please use this command with care!

10
  • @Paul, what would the slash do? I use this command to remove dirs often and w/o a slash.
    – Octopus
    Sep 6, 2015 at 3:21
  • 2
    @Octopus, slash by itself indicates the root directory. rm -rf / will theoretically delete every file on your computer that resides in a directory you have write permission for. If you're root, that means wiping out the entire system. (In practice, it doesn't work because GNU rm has a special case that refuses to allow rm -r / unless you also say --no-preserve-root.)
    – cjm
    Sep 6, 2015 at 3:50
  • 13
    All the same, I don't recommend trying rm -rf / to see if it's caught properly.
    – cjm
    Sep 6, 2015 at 3:52
  • 2
    @mattburnett It's dangerous in a VM too: you may have mounts that aren't local to the VM. In particular, I believe it's fairly common to mount one of the host machine's directories on the VM to share files between the host and the VM.
    – hvd
    Sep 6, 2015 at 22:18
  • 1
    @mattburnett I have VMs. I don't have a quarantined VM. I just don't run unknown executables. :)
    – hvd
    Sep 7, 2015 at 5:52
28

In addition to the previous correct answer, I would like to teach you how to fish:

When you are not sure about how a command works, what options has and what it does, open a terminal and type

man <command>

For example:

man rm

Once in there, you can search for the option. A man page can be really long to read, so in the terminal type:

/<pattern>

So for example, doing:

/-f

You can easily land to:

-f, --force
              ignore nonexistent files and arguments, never prompt

After typing /-r you'll get:

-r, -R, --recursive
              remove directories and their contents recursively

You can move between search results using n (next) and N (previous).

Bonus:

If you need to do something, but you don't know the command name, use apropos to search in man pages:

apropos <pattern>

For example:

apropos directory listing
9

rm is short for remove. The r flag is to remove directories and their contents recursively and the f means force, and it overrides any confirmation prompts.

3
  • 3
    Strictly speaking r flag is to remove directories is not right..it removes directories along with its contents, it is a recursive operation..according to your words, it would only remove the directories, what about the subdirs, files under it ? as directory entry contains name-inode mapping only, this only does not make any sense..
    – heemayl
    Sep 5, 2015 at 18:25
  • You are right, I will edit my answer.
    – Chris
    Sep 5, 2015 at 18:27
  • 1
    -r removes directories and their contents recursively.
    – Jim Balter
    Sep 5, 2015 at 20:56
3

As has already been mentioned, rm -rf <ARG> is meant to forcefully remove files recursively, where <ARG> is a directory ( though it can be a file just fine).

The whole point of -r ( recursive removal ) is that rm cannot remove directories if they aren't empty, simply because the underlying system call which rm uses ( unlink ) operates only on empty directories. Thus, what -r flag does, is depth-first search descending into directories and removing files first, and only then when directory is empty - it will remove it. This same effect is achieved via find command with -delete flag (when you don't specify filtering by -type, but that's another story).

As for -f, it does two things - one prevents prompting for whether you want to remove the file or not (such as when you are removing a file owned by another user from within your directory, it won't show rm: remove write-protected regular empty file 'f1'? confirmation prompt), and ignores non-existing files. So for instance, with a non-existent file name, you should get rm: cannot remove 'nonexistent': No such file or directory error.

See also:

0
0

Note that in a directory like this

.
├── AAA
│   └── file2.txt
└── BBB
    └── AAA
        └── file1.txt

running rm -rf AAA will produce

.
└── BBB
    └── AAA
        └── file1.txt

It's always better check on dummy folders with rm if you re not sure 101%.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .