Command Line problem

Discussion in 'Mac Basics and Help' started by Dark Lain, Jul 13, 2006.

  1. Dark Lain macrumors member

    Dark Lain

    Joined:
    Apr 29, 2005
    #1
    I've been getting my toes wet in exploring the command line in terminal and I am have a problem getting into a directory named -+-Pictures

    I type

    cd -+-Pictures

    and I get the following error message in Terminal

    -bash: cd: -+: invalid option
    cd: usage: cd [-L|-P] [dir]

    I know what some of you are going to say "Why not rename the folder?", well I happen to organize my folders by name and I want this particular folder at the top. Is there some sort of work around for this other than renaming the folder?

    Thanks for any advice.
     
  2. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
    The explanation is simple. The solution is harder.

    Shell commands usually (not always) consist of a command name, zero or more switches separated by spaces and each starting with a minus sign, and then zero or more non-switch arguments separated by spaces.

    Example: command -a -b -c arg1 arg2

    A problem occurs when arg1 happens to start with a minus sign, since commands almsot always confuse it with the switches.

    Example: command -a -b -c -3.14159 +2.71828 (is -3.14159 a switch or an argument?)

    Some commands allow you to use a double minus sign as a final switch, which indicates that anything following is an argument, not a switch.

    Example: command -a -b -c -- arg1 arg2

    So let's look at real examples:

    I try to list a file named "-z" and it fails:
    $ ls -z
    ls: illegal option -- z
    usage: ls [-ABCFGHLPRSTWZabcdefghiklmnopqrstuwx1] [file ...]

    I use the "--" workaround for ls and it works:
    $ ls -- -z
    -z

    I try to change to a directory named "-z" and it fails:
    $ cd -z
    -bash: cd: -z: invalid option
    cd: usage: cd [-L|-P] [dir]

    I use the "--" workaround for cd and it works:
    $ cd -- -z

    Note: I didn't actually test this.

    For commands that don't recognize "--", there are often other tricks you can use such as using wildcards, or rearranging arguments, or adding dummy arguments, so that the first argument doesn't start with a minus sign.

    In your example, you could try this command:
    cd *+-Pictures
    because the wildcard * hides the leading minus sign. This will fail if you have another folder that matches that pattern, e.g., ++-Pictures, but I'll bet that's unlikely.

    Bottom line: It's best not to have files or folders with names that start with a minus sign because that makes it hard to use them in a shell. The same is true for files with spaces in their names, although the workarounds for those are simpler.
     
  3. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #3
    Alternatively you could try:
    cd ./-+-Pictures​

    This works for me.
     
  4. Dark Lain thread starter macrumors member

    Dark Lain

    Joined:
    Apr 29, 2005
    #4
    Both cd -- -z and cd ./-+-Pictures work!

    Thanks for your help:)
     
  5. Veldek macrumors 68000

    Veldek

    Joined:
    Mar 29, 2003
    Location:
    Germany
    #5
    I think under Unix you can mask special characters, so the command line interprets them the correct way. IIRC, you can use a \ for this, but it's been so long ago that I used it, so I forgot about it. :eek:
     
  6. Dark Lain thread starter macrumors member

    Dark Lain

    Joined:
    Apr 29, 2005
    #6
    Yes before I tride this board I Googled around and found similar advice but it did not work.

     
  7. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #7
    That won't work in this case. The \ can be used to prevent the shell from "processing" a character that means something to the shell (spaces, wildcards, quote marks), but in this case (a file whose name starts with a minus sign) the shell isn't the problem, because - isn't a character with special meaning to the shell.

    The problem in this case is that the minus sign means something special to the cd command. So no \ is needed in front of the \ and it won't help either.
     

Share This Page