Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cd to directory starting with '-' dash [duplicate]

Tags:

bash

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 -
like image 229
physicalcog Avatar asked Jan 13 '18 23:01

physicalcog


People also ask

What does cd hyphen do?

The Meaning of – With cd But, using a dash as an argument of cd will change the current directory to the previous working directory. When we use a single dash as an argument of the cd command it contains the path of the previous working directory (OLDPWD).

What does it mean to cd to a directory?

cd or change directory The cd command allows you to move between directories. The cd command takes an argument, usually the name of the folder you want to move to, so the full command is cd your-directory . In the terminal, type: $ ls.


1 Answers

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 -"
like image 81
Daniel Avatar answered Sep 30 '22 03:09

Daniel