1

Just wondering how to delete directory/file named "-f", "-r" or other using terminal.

Vitalii
  • 19
  • 2

2 Answers2

5

This is literally in the manual

To remove a file whose name starts with a '-', for example '-foo', use one of these commands:

         rm -- -foo
         rm ./-foo
glenn jackman
  • 18,218
4

The rm command supports a -- option, which tells it that any further arguments that begin with - should be taken literally and are not to be treated as option flags. eg:

rm -- -r

Many other Unix commands support something similar, and it's an out-of-the-box feature of the commonly-used getopt utility which is often used for command option parsing.

Note that quoting the file name, eg rm "-r" does not work (whereas quoting does help when you're trying to delete a file called, say, * (asterisk), eg rm "*"). That's because the wildcard expansion is being handled by the your Unix shell before your command is invoked, and quoting disables such expansion. But options are effectively normal arguments that the command program (eg rm) itself gets to decide what to do with - they aren't "special" to the shell, so quoting has no effect.

SusanW
  • 185