I have a directory named:
-2
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
Any solution?
Edit: no file browsers like mc, etc. available on the server.
I have a directory named:
-2
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
Any solution?
Edit: no file browsers like mc, etc. available on the server.
At least two ways:
Use the
--
argument.This uses a convention common to GNU tools which is to not treat anything that appears after
--
as a command line option.As a commenter noted, this convention is also defined in the POSIX standard:
as well as:
Specify the path explicitly:
This specifies the path explicitly naming the current directory (
.
) as the starting point.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 should work:
-- means no more option
This will work if '-2' is in current directory.
You can autocomplete by typing - and pressing tab.
cd /home/...../-2
also works. Give the full path to access.Just to complement, if you'd like to remove/delete this directory you can use the following command:
I know this question has already been answered. If anyone has a situation like mine, this is for them:
I ran a java app and it was looking for a directory starting with
<path>
, I was supposed to replace that with proper path before running the app. However, I forgot to do that. The app created a directory called<path>
.I tried to
cd <path>
- gave me error "-bash: syntax error near unexpected token newline" Based on the suggestion here (I understand that its for directory starting with - and not <) I triedcd -- <path>
. However, I got the same error.When I tried
cd \<path>\
- this worked!Background-information:
The symbols "<" and ">" are used to redirect STDIN and STDOUT, therefore they need to be escaped in order to make the shell not interprete them as redirects.