4

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.

yth
  • 103
  • 1
  • 7
  • 2
    A `-=` in the middle of the argument, as in `cd foo/-=bar`, shouldn't cause a problem. The `-` character is only assumed to introduce an option when it's at the beginning of the argument, as in `cd -=bar`. – Keith Thompson Nov 26 '14 at 21:37
  • 1
    This question belongs on unix.stackexchange.com. And there are a number of questions there that address how to deal with filenames that begin with hyphen. – Barmar Nov 26 '14 at 21:38
  • @Barmar: It would probably be a better fit there -- but shell scripting *is* programming. – Keith Thompson Nov 26 '14 at 21:45
  • Yes, but there's no indication he's scripting rather than just typing commands. – Barmar Nov 26 '14 at 21:49
  • @KeithThompson: You are absolutely right. It was cd -=foo that is causing the error. Thank you for pointing that out, and sorry for the confusion. I edited my question. – yth Nov 26 '14 at 21:52
  • @Barmar: I was originally experimenting with Bash scripting - though, I'm not very good at it- when something strange kept happening. Eventually, I find this to be the cause. Not sure if that counts. – yth Nov 26 '14 at 21:55

2 Answers2

4

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

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).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • @gniourf_gniourf If I'm wrong then that's great and I'll remove it. I was vaguely recalling some discussion about it not being portable to the BSD tools or something like that so I could clearly be wrong. – Etan Reisner Nov 26 '14 at 22:10
  • 1
    I tend not to assume people actually mean bash when they use that tag but you are definitely right about the other points. Removed. – Etan Reisner Nov 26 '14 at 22:15