Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

2 Answers

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

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy