Error with cd Command with "-=" in the Target Directory's Name

2.9k Views Asked by At

I came across an interesting phenomenon when trying to use the 'cd' command with some of my directories.

I have named a number of my directories "-= [name]" so that they will be sorted to the top when I sort by name on a Windows machine at work. At home, I use a Linux machine. I use a USB stick to carry files between the two machines. I find I could not get into my directories that have names in form of "-= [name]" using 'cd' command.

The tab auto-complete does recognize the directory, and give the correct form. So the cd command would look something like this:

cd \-\=\ directory_name

However, I keep getting the following error message:

bash: cd: -=: invalid option
cd: usage: cd [-L|[-P [-e]] [-@]] [dir].

Does anyone know me what's going on here?

I know I can just change the names of my directories. But, I'm curious what's going on with the cd command. File managers are able to open up the directory with no problems.

2

There are 2 best solutions below

0
On BEST ANSWER

Use cd -- or prefix ./ before your directory name.

cd -- file_path_to\\-\=\ directory_name

OR

cd ./file_path_to\\-\=\ directory_name

Otherwise - is considered an option to cd command.

2
On

Command line arguments start with -. cd is expecting to see -<option> and -= it not a valid option.

You will see this with almost any other -X string.

$ cd -felkj
-bash: cd: -f: invalid option
cd: usage: cd [-L|-P] [dir]
$ cd -j
-bash: cd: -j: invalid option
cd: usage: cd [-L|-P] [dir]
$ cd -y
-bash: cd: -y: invalid option
cd: usage: cd [-L|-P] [dir]

You need to tell cd that you have ended the arguments with --:

$ pwd
/tmp/f
$ ls
-foo/
$ cd -foo/
-bash: cd: -f: invalid option
cd: usage: cd [-L|-P] [dir]
$ cd -- -foo/
$ pwd
/tmp/f/-foo

Alternatively stick any valid path prefix in front of the name (like ./-foo or /tmp/f/-foo).