24

I'm learning Git and my very first task is to navigate to a directory where my project will live. Unfortunately, my primary folder for my documents has the following form - folder1 - for sorting purposes, and each time I enter;

cd - folder1 -

I receive the error;

bash: cd: too many arguments

After searching for a solution I have tried;

cd -- folder1 -
cd ./- folder1 -
cd .- folder1 -
cd /- folder1 -

and even changing the directory to the parent directory and trying a longer path but this doesn't work either

cd ..
cd parent/- folder1 -
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
physicalcog
  • 399
  • 1
  • 2
  • 9
  • 1
    This seems to have nothing to do with `git`. – Lasse V. Karlsen Jan 13 '18 at 23:38
  • If you have embedded whitespace in the directory name then you need to quote it `cd '- folder1 -'`. Doesn't really matter if you use single or double quotes here. I can't stop myself say that you made a poor choice of directory names, whitespace in name always causes more work. – cdarke Jan 13 '18 at 23:44
  • If you explain why you need the spaces in the filename to do a proper sorting we can propose a better way of naming them. Using spaces in filenames means extra work if you need to write them on the command line, and I would not want to start a filename with a `-` either because of ugly things like: "let's remove my file that is called `-rf .`" – Daniel Jan 13 '18 at 23:50
  • I apologies for the incorrect label. What should it have been? – physicalcog Jan 13 '18 at 23:50
  • I have only used GUIs to this point. "-" as a starting character for Windows to list it first in the directory and the rest is simply aesthetic. Thank you for your comments. I will take them on board for future projects. – physicalcog Jan 13 '18 at 23:54

3 Answers3

44

As a rule of thumb, if your filenames have spaces or special chars like $ put them in single or double quotes. As another rule of thumb, if one of your arguments starts with a - and your command is interpreting it as an option instead of a filename (an option like the -n in in echo -n myfile) then you need to put a -- as an argument to your command.

So to solve your problem try this:

cd "- folder1 -"

If cd continues to think your folder name is an option, then do this:

cd -- "- folder1 -"
Daniel
  • 21,933
  • 14
  • 72
  • 101
  • 6
    +1 `cd -- "- folder1 -"` for me. The other one `cd "- folder1 -"` didn't work though. I was using gitbash – abdev Mar 03 '20 at 13:13
6

This has nothing to do directly with Git itself, but with your shell.

The too many arguments error is due to the dashes(or tacks, depending on how you call'em) being interpreted as either arguments being passed to the cd command or invalid flags/switches/options, etc.

In order to fix this, you'll have to prevent the dashes from being interpreted as flags.

In this case, you'll enter cd -- -\ folder1\ -/ OR cd -- "- folder1 -"

the -- after cd basically says "no flags please" and then the backslashes are escaping the spaces after the dashes in the folder name.

I highly suggest using a different naming convention as this can get VERY cumbersome and time consuming.

0

try using double-quotes:

cd "- folder1 -"
Matt
  • 314
  • 1
  • 7
  • It doesn't seem to work on my end. Neither single or double quotes work. Instead I get; "bash: cd: - : invalid option \\ cd: usage: cd [-L|[-P [-e]] [-@]] [dir]" – physicalcog Jan 13 '18 at 23:47
  • are you in the directory containing -folder-? if you list the contents (use 'ls' to list the contents of the directory) can you see -folder-? If so, type 'cd -' and then hit the tab key; the rest of the line should auto-complete. – Matt Jan 13 '18 at 23:55
  • It doesn't work this way since `cd` tries to interpret the directory name as a flag regardless of quotation marks. The only solution is to put two dashes before passing the directory name. – Andrey Semakin Sep 09 '20 at 07:21