filenames with spaces?

Andy Hughes

Registered
How did they get the OS to take spaces in filenames, since unix treats as space as a delimiter? So for example
cp file newfile
would get confused by a space in the file name.
Do I put files with spaces in the names in quotes? I know I can use * to match things, but how does it keep track of things? Is there some space character I don't see or know about? What is the best way to work with files (or directories) with spaces in their name?

Andy Hughes
 
You have a couple options.

One way, as you suggested, is to enclose the entire file name in quotes:
"/Applications (Mac OS 9)"

If you want to enclose wildcards in the file name, like * or ?, then quotes
may cause problems, depending on the shell you're using in Terminal. For example:
ls -d "/A*"
returns an error instead of printing at least /Applications and /Applications (Mac OS 9)

The other option is to proceed the space character with a backslash:
/Applications\ \(Mac\ OS\ 9\)

Note that I also had to put backslashes before the parenthesis. In general, putting a backslash before a character tells the shell to just use the next character as part of the filename and not to give it any special meaning.

However, if the portion of the name that you're typing doesn't include any special characters, then you don't need to bother. For example, "ls -d /A*" will work just fine, even though one of the printed directories contains spaces in the name. You can also use auto-completion in tcsh (by typing part of the name and then hitting the tab key) and the shell will deal with the spaces and other special characters for you.
 
An even simpler solution is to put the filename in single quotes. Ex:
cd /'Applications (Mac OS 9)'
You only have to put the single quotes around the file/folder name with space(s) in it (yes, this works with double-quotes too). This just saves the trouble of pressing the shift key to get the double quotes. :p
 
Just so you all know, there is actually no limitation on the characters you can use in a filename in UNIX, and consequently OS X, with one exception. You cannot have a forward slash in a name. (Consequently, if you know how to input the characters correctly, you can actually have a carriage return in a filename)

With OS X, the Mac inherits the / limitation, but because Classic is still here which uses colons to delimit folders in a path, so you cannot use either colon or slashes in filenames if you want to be able to use it all the time.

One interesting thing in the implementation of filenames via the Finder, though, is that you CAN actually input slashes into the Finder. But it actually puts a COLON in the filename at the UNIX level. Which is very interesting because Classic can still handle all of these files. Another tidbit is that when you try and put a colon in the Finder, it auto-changes it to a hyphen. Which is weird because you'd think it should do the same for a slash. Very bizarre implementation.
 
I just became aware of the Finder's forward-slash-to-hyphen behavior yesterday. My wife was attempting to save some graphic images that she had named using a date format (mm/dd) in the title. When she saved the files, they changed from "mm/dd" to "mm-dd". This of course changed their location in her sorted-by-name Finder windows, so at first she couldn't find her new files.
We figured it out eventually, but it did cause confusion and consternation for a while.
:rolleyes:
 
Back
Top