196

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.

user151851
  • 1,863
  • 2
  • 12
  • 10
  • 2
    Related question on superuser: http://superuser.com/questions/361573/mv-command-confuses-directory-name-with-command-option/361578#361578 – yms Jan 04 '13 at 21:51

6 Answers6

309

At least two ways:

  1. Use the -- argument.

    cd -- -2
    

    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:

    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.

    as well as:

    Guideline 10:
    The argument -- 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 '-' character. The -- argument should not be used as an option or as an operand.

  2. Specify the path explicitly:

    cd ./-2
    

    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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • 25
    It's not just GNU, it's part of the POSIX standard. See [1.11 Utility Description Defaults](http://pubs.opengroup.org/onlinepubs/007904875/utilities/xcu_chap01.html#tag_01_11) and [12.2 Utility Syntax Guidelines](http://pubs.opengroup.org/onlinepubs/007904875/basedefs/xbd_chap12.html#tag_12_02) (guideline #10). From the [`cd` page](http://pubs.opengroup.org/onlinepubs/007904875/utilities/cd.html) "The `cd` utility shall conform to... section 12.2". – Dietrich Epp Jan 03 '13 at 23:40
  • Now I am just curious on how to cd to a directory named --! – sergiol Jan 04 '13 at 14:58
  • 2
    @sergiol The same way. – Michael Hampton Jan 04 '13 at 15:10
  • wouldn't `cd "-2"` work? – Sled Jan 04 '13 at 17:18
  • 1
    @ArtB: No. Quoting just affects field splitting. So `"hello world"` is one argument but `hello world` is two, and `"-2"` is the same as `-2`. – Dietrich Epp Jan 04 '13 at 20:42
  • 1
    @sergiol: Either `cd -- --` or `cd ./--` should work. – Keith Thompson Jan 05 '13 at 02:31
54

This should work:

cd -- -2 

-- means no more option

Tim Haegele
  • 951
  • 6
  • 13
31

This will work if '-2' is in current directory.

    cd ./-2

You can autocomplete by typing - and pressing tab.

newbee
  • 411
  • 3
  • 2
13

cd /home/...../-2 also works. Give the full path to access. 

namgold
  • 103
  • 3
Vivek
  • 286
  • 1
  • 3
8

Just to complement, if you'd like to remove/delete this directory you can use the following command:

rm -r -- -2
Zignd
  • 189
  • 4
  • ...and just to complement, I would never do that, rather I will use graphical tool like mc or I will rename the directory first. – Nick Nov 22 '18 at 12:58
0

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 tried cd -- <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.

jmathewt
  • 111