0

I'm having trouble using using commands on files that start with "-" (i.e. -file 1, -file 2). It is giving me invalid option errors.

For example:

cp '-file 1' '-file 2'

However, echo still works.

echo "Hello World" > '-file 2'

How can I make it so that the "-" does not signify that I'm choosing an option?

Aaron
  • 45
  • 1
  • 1
  • 3

1 Answers1

5

Most POSIX utilities specify that -- can be used to terminate option arguments:

cp -- '-file 1' '-file 2'

You can also reference the current directory using the . hard link to the current directory:

cp './-file 1' './-file 2'
peterph
  • 30,520
  • 2
  • 69
  • 75
Chris Down
  • 122,090
  • 24
  • 265
  • 262
  • 1
    A third option: the leading hyphen isn't mistaken for the beginning of an option if the word is expected as the argument to a preceding option. For example: in `grep -e -foo file.txt`, the `-e` option takes an argument, so `grep` doesn't expect `-foo` to be an option. – chepner Oct 17 '14 at 02:36