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 “-“.
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.
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!
Thanks for posting this. Rather odd that it the “-” within the quotes would be seen as an option.
Thanks! never saw the — trick before.
my “-f” file is now gone! at last!
I’d never seen it before either, so I thought it was worth writing up! 🙂 I know how frustrating it is.