I have a directory named:
I want to cd into it but the cd complains:
bash: cd: -2: invalid option
With no success, I’ve tried:
cd "-2" cd '-2' cd \-2
Edit: no file browsers like mc, etc. available on the server.
At least two ways:
cd -- -2
This uses a convention common to GNU tools which is to not treat anything that appears after
--as a command line option.
Default Behavior: When this section is listed as “None.”, it means that the implementation need not support any options. Standard utilities that do not accept options, but that do accept operands, shall recognize
"--"as a first argument to be discarded.
The requirement for recognizing
"--"is because conforming applications need a way to shield their operands from any arbitrary options that the implementation may provide as an extension. For example, if the standard utility foo is listed as taking no options, and the application needed to give it a pathname with a leading hyphen, it could safely do it as:
foo -- -myfile
and avoid any problems with -m used as an extension.
--should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the
--argument should not be used as an option or as an operand.
Specify the path explicitly:
This specifies the path explicitly naming the current directory (
.) as the starting point.
cd $(pwd)/-2 cd /absolute/path/to/-2
These are variations on the above. Any number of such variations may be possible; I’ll leave it as an exercise to the reader to discover all of them.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.