What does rm -rf do when used to remove files or directories?
How do the -r and -f options work together?
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.
Please use this command with care!
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).
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
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.
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:
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%.