Why does rm -rf not yield an error?

2

ubuntu 9.10 if you type rm you get a missing operand error, same thing with rm -r but rm -f yields no error. -f is supposed to be 'force' not 'dispell errors'

Stu

Posted 2010-07-07T14:21:24.210

Reputation: 1 044

Answers

8

From the Unix manpage

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

ignoring nonexistant files means that there will be no error if there is no files to remove, which is what happens when you use rm -f with no file(s) specified. This is the expected behavior as far as I know. It does not ignore all errors, but -f does not require the file to be listed an operand, so there is no error to throw.

MDMarra

Posted 2010-07-07T14:21:24.210

Reputation: 19 580

2seems a little fishy to me, The man page says this: rm [OPTION]... FILE... as in FILE is not optional. – Stu – 2010-07-07T15:04:54.630

3@nixomose unless -f is specified :). The point is that you can specify multiple files on the command line. If any one of them is missing and you didn't pass -f, you get an error. If you do pass -f, any errors related to files are suppressed. – Felix – 2010-07-07T18:50:51.383

1right, that's what happens, but that's not what the man page leads you to believe. -f, --force ignore nonexistent files, never prompt Say nothing about ignoring errors. – Stu – 2010-07-07T20:19:12.900

2+1 for nixomose, " but -f does not require the file to be listed an operand" is plain non-sense. The man page you link to claims no such thing and actual explicitly declares it as required. This is simply a case of the man page and program behavior not matching. The man page needs to be updated to more accurately reflect the effect of the 'f' switch – Dan McGrath – 2010-07-07T22:46:24.420

10

SUSv3 says:

-f Do not prompt for confirmation. Do not write diagnostic messages or modify the exit status in the case of nonexistent operands. Any previous occurrences of the -i option shall be ignored.

Florian Diesch

Posted 2010-07-07T14:21:24.210

Reputation: 3 380

1

To understand why the file parameter is really optional, you have to consider that in many scripts the list of files to remove is stored in a variable and using -f allows you to not care whether the list is empty or not. You can simply do:

rm -f $FILES

insead of the more cumbersome:

-z "$FILES" || rm $FILES

So it's logical that an option called "ignore nonexistent files" does not fail when no file is given on the command line.

Raphaël Hertzog

Posted 2010-07-07T14:21:24.210

Reputation: 264

1This may be true and make sense and programmers tend to do what's personally convenient more than what follows the pattern, but this is 'rm'. It's older than my dad and the man page has been wrong for how many decades? – Stu – 2010-07-08T14:28:12.947