We have some folders where the names are starting with a "-". If I try to enter them with terminal, cd interprets the "-" as an option. How could I manage this?

  • 1
    One would be wise to avoid such characters in directory/folder names. – Mindwin Mar 21 '17 at 18:10
  • Couldn't you just do cd '-my-directory'? – OldBunny2800 Mar 22 '17 at 0:09
  • @oldbunny2800 Did you try that? – nohillside Mar 22 '17 at 9:10
  • 1
    @OldBunny2800 No. Quotes prevent the shell from treating characters separately; strings starting with hyphens are treated as options (or not) by the command that receives the arguments, regardless of how those strings were presented to the shell. – chepner Mar 22 '17 at 13:52
  • 1
    @patrix I did right after I posted the comment, then got distracted by other terminal shenanigans, then forgot about my comment. :) Chepner ohhh, that makes more sense. – OldBunny2800 Mar 22 '17 at 20:48

This should just work:

cd /path/to/-folderthatstartswithdash


According to this question, this works if you only want to input the name:

cd -- -folderthatstartswithdash
  • 5
    The -- method is the correct answer... it will work with most standard utilities that accept a filename as an argument and saves having to fully qualify the path. – Kaithar Mar 21 '17 at 23:58
  • The -- method works as it "turns off" argument parsing. – Tony Williams Mar 22 '17 at 1:15
  • 3
    Why the restriction "if you only want to input the name"? If you need more arguments, just put them before the --. – Federico Poloni Mar 22 '17 at 7:57
  • @FedericoPoloni I'm just saying if you don't want the full path you can use this one. All other arguments should go before the --. – Tom Shen Mar 22 '17 at 10:26
cd ./-folder

usually works quite well here (and also with other commands)

  • One nice thing about this solution is that it works with any program or command, not just 'cd'; it does not depend on the command or program having any special behavior like "--" to turn change the interpretation of subsequent arguments. – Larry Gritz Mar 27 '17 at 18:51

Putting the name in quotes should work.

  • 3
    Why would that work? How is the utility going to know the difference? Quoting is done by the shell, after all, the arguments array the utility receive will be exactly the same either way. I admit that I didn't try your solution, but from what little I know about shells, it seems pretty self-evident, that this cannot possibly work. – Jörg W Mittag Mar 21 '17 at 18:29
  • This should work on Windows IIRC, and is what I thought of first, but I can confirm that it doesn't work on mac. – Cullub Mar 21 '17 at 19:49
  • 2
    If it works on Windows that's only because Windows' command line model is utterly ridiculous and inconsistent. Or just because Windows commands aren't treating - as an option character. But there's no way quoting should be able to change this behavior. – R.. GitHub STOP HELPING ICE Mar 21 '17 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .