1

Wondering how to remove a file its name is "-f" or "-r" on Mac OS and on Linux. What is the right way to escape? I did some research and it seems the only way is to remove by inode ID?

thanks in advance, Lin

0
1

You can use rm ./'-f' and rm ./'-r', rm -i ./*, or rm -i ./-*. These solutions are widely known.

2
  • Thanks Larssend, what is the purpose of ./? – Lin Ma Oct 16 '15 at 3:32
  • 1
    ./ means 'current working directory'. rm would think -f and -r are arguments/options rather than actual file name without it. – Larssend Oct 16 '15 at 3:35
4

Larssend's solution (specifying a current directory before specifying the filename) works fine. Just to give you another solution, though:

rm -- -f

So, pass a parameter of two hyphens before specifying the challenging filename.

The -- means "quit trying to treat upcoming characters like options", so the hyphens will quit acting like special characters in the later parameters. Helps to ensure that a file that starts with a hyphen doesn't cause you to accidentally specify some unintended options. This is a program feature, not a shell feature, so support for this feature may vary between programs, but many standard Unix programs support it.

2
  • Thanks TOOGAM, you mentioned, "quit trying to treat upcoming characters like options", confused and do you mean not treat upcoming characters like options, so -f is treated as file name? – Lin Ma Oct 16 '15 at 18:44
  • 1
    The filename is a parameter that you type, also known as an "argument" received by the program. However, a filename is not an option. A filename is not an "option" that primarily alters how the program works. So by quitting the behavior of treating the upcoming characters (the "-f") as a potential option, those characters are no longer checked to see how "rm" will run. The result is that the characters lose any sort of special meaning, so "rm" will do the most natural other thing it could do, which is to treat the characters as a filename. – TOOGAM Oct 16 '15 at 20:10

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