rm -rf do when used to remove files or directories?
How do the
-f options work together?
rm -rf is the same as
rm -r -f.
rm's man page (type
man rm in a terminal to see it)
remove directories and their contents recursively
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
Once in there, you can search for the option. A man page can be really long to read, so in the terminal type:
So for example, doing:
You can easily land to:
-f, --force ignore nonexistent files and arguments, never prompt
For -r do:
-r, -R, --recursive remove directories and their contents recursively
You can move between search results using
n (next) and
If you need to do something, but you don't know the command name, use
apropos to search in man pages:
apropos directory listing
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).
-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.