The difference between rm -r and rm -f

rmterminal

From manual:

  • -f, –force

    ignore nonexistent files, never prompt

  • -r, -R, –recursive

    remove the contents of directories recursively

Though this options description is different, when trying to delete an empty folder (without rmdir for this example) it gives the same result.

-f won't print error or anything compared to -r, is this the only difference or is there a specific type of situations when one option is better than another or situations where one of this option simply won't work while the other will?

Best Answer

This is what the man page in CentOS says:

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

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

From what I gather (thanks to some comments below), the following is true for the -r and -f flags:

-r

  • recursively deletes content of a directory, including hidden files and sub directories
  • depending on your configuration, it may ask for permission (for example, when using the --interactive flag). Some distributions do this by default.
  • can be used to remove a directory, if you want to do so, simply give it the path of the directory (for example: /path/to/directory)

-f

  • does not recursively delete content of a directory, only removes files that directly match the given path (for example example/file1 or example/*).
  • Never deletes sub directories
  • Never asks for permission, basically the yes to all in Windows

Below are a few examples, all of them start with the following structure:

example/
  file1
  file2
  file3
  .file
  dir/
    file1
    file2
    file3
    .file

I enabled verbosity and interactive mode by default for these examples. Some distros do this while others don't.

rm example

$ rm example
rm: cannot remove `example': Is a directory

As you can see, rm does not remove directories by default.

rm example -f

$ rm example -f
rm: cannot remove `example': Is a directory

Using the -f flag still doesn't allow it to remove directories.

rm example -r

$ rm example -r
rm: descend into directory `example'? yes
rm: remove regular empty file `example/file3'? yes
  removed `example/file3'
rm: remove regular empty file `example/file2'? yes
  removed `example/file2'
rm: descend into directory `example/dir'? yes
rm: remove regular empty file `example/dir/.file'? yes
  removed `example/dir/.file'
rm: remove regular empty file `example/dir/file3'? yes
  removed `example/dir/file3'
rm: remove regular empty file `example/dir/file2'? yes
  removed `example/dir/file2'
rm: remove regular empty file `example/dir/file1'? yes
  removed `example/dir/file1'
rm: remove directory `example/dir'? yes
  removed directory: `example/dir'
rm: remove regular empty file `example/file1'? yes
  removed `example/file1'
rm: remove directory `example'? yes
  removed directory: `example'

As you can see, you are asked for permission for every single file and directory, hidden files are also removed.

rm example/* -f

$ rm example/* -f
rm: cannot remove `example/dir': Is a directory
removed `example/file1'
removed `example/file2'
removed `example/file3'

Here, you are not asked for permission, directories are not deleted and neither are hidden files.

rm example/* -r

$ rm example/* -r
rm: descend into directory `example/dir'? yes
rm: remove regular empty file `example/dir/.file'? yes
  removed `example/dir/.file'
rm: remove regular empty file `example/dir/file3'? yes
  removed `example/dir/file3'
rm: remove regular empty file `example/dir/file2'? yes
  removed `example/dir/file2'
rm: remove regular empty file `example/dir/file1'? yes
  removed `example/dir/file1'
rm: remove directory `example/dir'? yes
  removed directory: `example/dir'
rm: remove regular empty file `example/.file'? yes
  removed `example/file'
rm: remove regular empty file `example/file1'? yes
  removed `example/file1'
rm: remove regular empty file `example/file2'? yes
  removed `example/file2'
rm: remove regular empty file `example/file3'? yes
  removed `example/file3'

Here, the contents of the example directory (not the directory itself) are removed, including hidden files.

Related Question