Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dark Lain

macrumors member
Original poster
Apr 29, 2005
49
6
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.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
39,789
7,525
Los Angeles
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.
 

Veldek

macrumors 68000
Mar 29, 2003
1,789
1
Germany
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:
 

Dark Lain

macrumors member
Original poster
Apr 29, 2005
49
6
Yes before I tride this board I Googled around and found similar advice but it did not work.

< Why some file names cause trouble >
You can have weird file names on your Linux system if you want. For example, nothing prevents you from having spaces in a file name. You can also use some strange characters in a file name, like &, *, \, $, and ? . The problems start when you try to use these file names at the command line.

You see, when you type commands at the prompt, spaces are often used for separating different commands, the command's arguments, or different files. The shell doesn't know that the bunch of text that follows your command is in fact one single file name. However, there's an easy way to tell the shell that the bunch of text is just one file name, so having spaces in a file name usually isn't a big problem.

A more problematic thing is special characters in file names. There are some characters that have a special meaning to the bash shell. For example, * and ? are wildcards and $ means a variable. If you can avoid using these characters in normal file names, do avoid it. If you have no idea what are wildcards or why $ means a variable, you have even more reasons to avoid these characters in normal file names!

Anyway, if you have files with spaces or special characters in their names, you have two ways of dealing with them: quoting the file name or escaping the confusing characters.

< Method 1: quoting >
This is very straightforward: put the file name in single quotes ( ' ) so spaces or special characters won't bother you anymore:

$ cat 'File With Spaces.txt'

See why the quotes are so important? If you don't use them, cat tries to view three different files: File, With and Spaces.txt.
Or another example:

$ rm 'File*.txt'

Here the quotes are even more important! Because * is a shell wildcard that matches any character, you'll be in a big trouble if you don't use the quotes. Without the quotes the shell removes File*.txt as intended, but in addition File.txt, File2.txt, File22.txt and others will get removed.

< Method 2: escaping >
Another way to deal with special characters in a file name is to escape the characters. You put a backslash ( \ ) in front of the special character or space. This makes the bash shell treat the special character like a normal character:

$ cat File\ With\ Spaces.txt

or:
$ rm File\*.txt

But what to do if the file name contains the \ character? Well, you escape it too!

$ rm File\\.txt

Of course you can also use the quotes:

$ rm 'File\.txt'

It's a matter of personal taste which method you use, quoting or escaping, but personally I prefer quoting. It's much more straightforward in my opinion. However, quoting doesn't always work. For example, if you want to use shell wildcards with a file that has special characters in its name, it's impossible to use quoting because that would escape the wildcards as well, so in these cases it's necessary to escape the special characters with a backslash.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
39,789
7,525
Los Angeles
Veldek said:
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:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.