47

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

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

TellMeWhy
  • 17,484
  • 6
  • 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. – David Richerby Sep 06 '15 at 13:08
  • 11
    Try it and see. (Spoiler: don't.) – geometrian Sep 07 '15 at 01:29
  • Upvoting because the empirical approach isn't recommended, plus there's some special behaviour associated with trying to do this with the root directory. – Andrew Grimm Sep 07 '15 at 01:37
  • @noonand: you probably rather meant "to suppress the French language... and everything else downhill from it" ! ;-D Warf ! Warf ! you got my +1 for good humor. – Cbhihe Oct 19 '15 at 14:09
  • 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 '15 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)) – syntaxerror Dec 23 '15 at 09:51

5 Answers5

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!

  • @Paul, what would the slash do? I use this command to remove dirs often and w/o a slash. – Octopus Sep 06 '15 at 03: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 06 '15 at 03:50
  • 13
    All the same, I don't recommend trying rm -rf / to see if it's caught properly. – cjm Sep 06 '15 at 03:52
  • @cjm That is what VMs and snapshots are for. – Matt Sep 06 '15 at 18:29
  • 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 06 '15 at 22:18
  • @hvd That it true, but it is trivial to disconnect the network adapter(s) and removable media from a VM. And anyone who has VMs likely has a "quarantined" VM for running unknown executables. – Matt Sep 06 '15 at 22:30
  • @cjm: So does Solaris rm. And as of recently, OpenBSD rm. (Apparently it's even a POSIX requirement?) – user1686 Sep 06 '15 at 22:46
  • 1
    @mattburnett I have VMs. I don't have a quarantined VM. I just don't run unknown executables. :) – hvd Sep 07 '15 at 05:52
  • @Octopus Sorry but can't say. rm seems to have been run against my comment... – Paul Uszak Sep 07 '15 at 21:58
  • find -delete is also dangerous – 0x6773 Sep 13 '15 at 09:42
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
Federico Ponzi
  • 436
  • 4
  • 10
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.

Chris
  • 568
  • 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 05 '15 at 18:25
  • You are right, I will edit my answer. – Chris Sep 05 '15 at 18:27
  • 1
    -r removes directories and their contents recursively. – Jim Balter Sep 05 '15 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:

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
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%.