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

Lin Ma
  • 271

2 Answers2

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.

TOOGAM
  • 16,486
1

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

Larssend
  • 3,971