1

I was trying to rename a file named 12F-XYZ.pdf to 13F-XYX_ABX.pdf.

Both the original and the required names have hyphens. I know that for spaces we use \ to overcome problems, but what can we do in the case of hyphens or any other character that might be treated specially by the shell or by commands?

Zanna
  • 72,312
DevX
  • 113

1 Answers1

5

As David Foerster pointed out in a comment, hyphens (-) are not treated specially by the shell. So as far as your example is concerned you can simply do:

mv 12F-XYZ.pdf 13F-XYX_ABX.pdf

But if you have a space or literal escape character (backslash) or any other that needs to be escaped, you can escape those with either the escape character i.e. \ or put the whole name inside quotes ' ' so that the content inside the quotes is treated literally.

Here is an example:

mv 12F-XYZ.pdf 50M -XYZ.pdf  ##Wrong
mv 12F-XYZ.pdf 50M\ -XYZ.pdf  ##Right
mv 12F-XYZ.pdf '50M -XYZ.pdf'  ##Right

A rule of thumb would be to escape it while in doubt. This article on special characters would be a very good read for you.

As muru pointed out in comments, you could have problem in case of a leading hyphen as many commands treat arguments beginning with a hyphen as options. In that case you can use either of the following:

mv -- foo.bar -foo.bar
mv foo.bar ./-foo.bar

The -- indicates the end of switches for the previous command (in this case mv). Not all commands support -- so using the second option (./-foo.bar) would be more reliable.

Zanna
  • 72,312
heemayl
  • 93,925