Renaming a File Starting with “-” or Dash on Linux

I accidentally created a file named “-C” on one of my Linux servers when using the tar command. I was hitting my head against the wall for a while trying to work out how to use the mv command to rename the file to something more meaningful.

The problem is that the command line thinks that the “-C” is an option for the mv command, so you’ll keep getting an error like this:

-bash-3.2# mv -C archive.tgz
mv: invalid option -- C
Try `mv --help' for more information.

Even if you try and use quotes or the escape character it will still process the characters after the dash as an option to the command.

The solution is to tell the command that you have finished passing arguments by using double dash (“–“). Alternatively you can also use the full path to the file or “./-C”

-bash-3.2# mv -- -C archive.tgz
-bash-3.2#

Hopefully this will help someone else who is looking to rename a file starting with “-“.

5 thoughts on “Renaming a File Starting with “-” or Dash on Linux

  1. rob

    This can be generalized to passing any argument on the command line that begins wish a dash/hyphen. I wasn’t needing to move a file, but had to pass a negative number to a command line util which I couldn’t get to work until Google assisted my stumble upon this article. Thanks for this post.

    Reply
  2. louco73 Post author

    Hey Rob,

    Yes, you are correct; you can use ‘–‘ anytime you want to stop the processing of command options and allow arguments that look like command options through.

    I’m glad my post was useful!

    Reply

Leave a Reply to rob Cancel reply

Your email address will not be published. Required fields are marked *