- Everything Is a File
- File Types and Extensions
- Maximum Filename Lengths
- Case Sensitivity and Case Preservation in Filenames
- Special Characters to Avoid in Filenames
- Wildcards and What They Mean
- Conclusion
Special Characters to Avoid in Filenames
Every operating system has some restrictions it places on what characters you can use in filenames, and Mac OS X is no exception. In fact, it actually has more complexity to worry about than most systems, if you're going to be working with the shell as much as with the Finder.
Like other Unixes, the command-line portion of Mac OS X forbids you from using the forward-slash (/) character in filenames. This is because slashes are used to delimit directory names in paths; for example, /Users/btiemann/Documents/File1.txt represents a file four folders down from the system root. I can't name a file Taxes/2006.pdf, because the system would think I'm talking about a subfolder called Taxes with a file called 2006.pdf inside it.
Okay, so slashes are fairly easy to avoid. But if you're checking my work, you'll have noticed that you can create a "Taxes/2006.pdf" file without any trouble in the Finder. What gives?
The answer is that, historically, the classic (pre-OS X) Mac OS allowed slashes—because it used the colon (:) as its path delimiter, not the slash. When Mac OS X came out, rather than forcing everyone to go through an upgrade procedure to rename all their files with slashes in the names, it simply interpreted those files on the command line with colons instead of slashes. Similarly, if you create a file at the command line with a colon in it, it will show up as a slash in the Finder. Try it: type touch blah:foo at the command line, and watch the file "blah/foo" appear in the corresponding Finder window.
The upshot is that you can't use colons in the Finder, and you can't use slashes in the shell—but the reverse in both cases is perfectly legal. If you have trouble keeping this straight, don't worry: you're not alone. (Or should that be "don't worry/you're not alone"?)
That's not where the inconveniences end, unfortunately. There's also the unpleasant matter of spaces, apostrophes, quotes, dashes, asterisks, and other characters that seem perfectly natural as names of documents but that will cause you fits if you try to work with them on the command line. Each of the character classes in Table 3.1 has a special meaning for Unix, one that doesn't normally impinge on your life in the Finder, but that can make the shell fall over and twitch if you don't know what you're doing.
Table 3.1. Avoiding Special Characters in Filenames
Character |
Meaning |
Space |
Separator between command arguments |
/ |
Path delimiter |
\ |
Escapes the following character |
- |
Can indicate a command option |
[] |
Shell scripting tokens |
{} |
Shell scripting tokens |
* |
Wildcard (multiple characters) |
? |
Wildcard (single character) |
' |
Command argument grouping delimiter |
" |
Command argument grouping delimiter |
To use any of the preceding characters in a filename in the shell, you have to escape it—precede it with a backslash character, which tells the shell to treat the next character in the filename literally, not as a special command character. For instance, suppose you have a file called My "Road Trip" CDs.txt that you want to address using a shell command (ls). You'd have to write the command like this:
Silver:~ btiemann$ ls My\ \"Road\ Trip\"\ CDs.txt
This tells the shell that the spaces and quotes are part of the filename, not separate arguments for the ls command. Otherwise, ls would be trying to list three separate files: one called My, another called Road Trip, and a third called CDs.txt.
To keep your command-line life simple, I'd recommend that you just avoid using weird characters like the ones described here. For files that you plan on using only in the GUI side of Mac OS X, it's okay to use whatever letters the Finder will accept. But your life on the command line will be a lot happier if you leave out the spaces, quotes, and asterisks in the files you create there.