4

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?

3
  • What system are you using?
    – techraf
    Sep 20, 2016 at 7:20
  • 2
    On an empty directory they will give the same result as there is nothing to delete. Try it on a tree of files and directories, they will give different results.
    – Mokubai
    Sep 20, 2016 at 7:21
  • What do you mean they give the same result? rm -r emptydir removes that directory, rm -f emptydir does not. These are two completely different command line options, each doing whatever its documentation says is doing.
    – egmont
    Sep 20, 2016 at 7:21

2 Answers 2

6

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.

6
  • The asterisk does not imply unlimited recursion. If it matches subdirectories (e.g. example/subdir), it will not descend any further, and rm will still ignore those directories. (Some shells have ** which does recurse.)
    – user1686
    Sep 20, 2016 at 8:34
  • Huh, I never knew that. I'll update the answer.
    – Cas
    Sep 20, 2016 at 10:09
  • Additionally, * usually does not match "dot" files (I suppose it was meant to ignore . and .., but it ended up ignoring all hidden files), therefore rm -r dir/* alone might not necessarily empty the directory; it'd skip dir/.git for example.
    – user1686
    Sep 20, 2016 at 10:36
  • Thanks, I believe I have now updated the answer to properly display the differences between -r and -f, I might have gone a bit overboard though.
    – Cas
    Sep 20, 2016 at 11:12
  • Thanks for everyones input (including other replays)! All answers combined helped me understand this better. Someone asked my OS - I'm using Ubuntu/Linux. Sep 20, 2016 at 15:23
0

rm -r mydir will remove the mydir directory with all its contents.

rm -f mydir will not remove a directory (neither empty nor with content). It will report an error:

  • on BSD/OS X:

    rm: mydir/: is a directory
    
  • on GNU/Linux:

    rm: cannot remove 'mydir': Is a directory
    

Possible explanations for the rm command behaving regardless of the given arguments (from the most to the least likely):

  1. you have a shell alias rm defined and it passes some defined parameters (like -r) to the rm command
  2. you are calling a script called rm which also passes additional parameters to the actual command
  3. you have a custom rm executable

You can verify the first two possibilities by executing /bin/rm -f mydir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.