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?
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 recursivelyFrom 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
--interactiveflag). 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/file1orexample/*). - Never deletes sub directories
- Never asks for permission, basically the
yes to allin Windows
Below are a few examples, all of them start with the following structure:
example/ file1 file2 file3 .file dir/ file1 file2 file3 .fileI 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 directoryAs you can see, rm does not remove directories by default.
rm example -f
$ rm example -f
rm: cannot remove `example': Is a directoryUsing 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.
6rm -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 directoryon 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):
- you have a shell alias
rmdefined and it passes some defined parameters (like-r) to thermcommand - you are calling a script called
rmwhich also passes additional parameters to the actual command - you have a custom
rmexecutable
You can verify the first two possibilities by executing /bin/rm -f mydir.