The previous chapter introduced the Unix filesystem, including an extensive discussion of the directory structure, the ls command for seeing what files are on your system, and how to move around using cd and pwd. This chapter focuses on Unix filenaming schemes—which aren't the same as names you'd see in the Finder, as you'll see—and how to view, edit, rename, copy, and move files.
As Chapter 3 explained, both files and directories are identified by their names. A directory is really just a special kind of file, so the rules for naming directories are the same as the rules for naming files.
Filenames may contain any character except /, which is reserved as the separator between files and directories in a pathname. Filenames are usually made of upper- and lowercase letters, numbers, dots (.), and underscores (_). Other characters (including spaces) are legal in a filename, but they can be hard to use because the shell gives them special meanings or otherwise forces you to constantly be changing how you work with these filenames on the command line.
Spaces are a standard part of Macintosh file and folder names, so while I recommend using only letters, numbers, dots, and underscores for filenames, the reality is that you have to work with spaces in file and directory names, because that's what Mac people do. Rather than naming a file myFile.txt as a Unix person would, most Mac folks are used to adding spaces to filenames, such as my file.txt. The Finder, by contrast, dislikes colons (which older versions of Mac OS used as a directory separator, just as Unix uses the forward slash). If you display a file called test:me in the Finder, the name is shown as test/me instead. (The reverse is also true: if you create a file in the Finder whose name contains a slash, it will appear as a colon in the Terminal.)
Though it's tempting to include spaces in filenames as you do in the Finder, if you're planning on doing any substantial amount of work on the Unix side, get used to using dashes or underscores in place of spaces in your filenames. It's 99 percent as legible, but considerably easier to work with.
Further, in the interest of having files correctly identified in both the Finder and Unix, you'd be wise to get into the habit of using the appropriate file extensions, too (i.e., .doc for Microsoft Word documents, .txt for text files, .xls for Excel spreadsheets, and so on). As an added bonus, this makes life easier for your less-fortunate (Windows-using) friends when you send them files.
If you have a file with a space in its name, that space confuses the shell if you enter it as part of the filename. That's because the shell breaks commands into separate words with spaces as delimiters, just as we do in English. To tell the shell not to break an argument at spaces, either put quotation marks around the filename that includes spaces (for example, "my file.txt"), or preface each space with a backslash (\).
For example, the rm program, covered later in this chapter, removes Unix files. To remove a file named a confusing name, the first rm command in the following snippet doesn't work, but the second does. Also note that you can escape spaces (that is, avoid having the shell interpret them inappropriately) by placing a backslash character before the space, as shown in the third example:
ls -ltotal 2 -rw-r--r-- 1 taylor staff 324 Feb 4 23:07 a confusing name -rw-r--r-- 1 taylor staff 64 Feb 4 23:07 another odd name $
rm a confusing namerm: a: no such file or directory rm: confusing: no such file or directory rm: name: no such file or directory $
rm "a confusing name"$
rm another\ odd\ name$
You also need to escape any of the following characters with a backslash (
\), because they have special meaning to the shell:
* # ` " ' \ $ | & ? ; ~ ( ) < > ! ^
Open a Terminal window and change directories to your Library directory. You'll see files that contain spaces, though the other punctuation characters are more unusual components of filenames:
lsApplication Support Documentation Keychains Snapz Pro X Assistants Favorites Logs Sounds Audio FontCollections Mail Syndication Autosave Information Fonts Metadata iMovie Caches Icons Preferences iTunes Classic Indexes Printers ColorPickers Internet Plug-Ins Recent Servers Cookies Keyboard Layouts Safari $
cd Application\ Support/$
The last example shows a useful trick: hitting the Tab key after entering a few characters of the filename invokes the shell's file completion feature. When you hit the Tab key, the shell automatically includes the backslash required to escape any spaces in file or directory names.
One place where you can find all sorts of peculiar filenames is within your iTunes library, because iTunes uses the song title as the filename for the corresponding MP3- or ACC-encoded file. Here are a few examples of filenames from my own library that would be incredibly difficult to work with on the command line:
The Beatles/Sgt. Pepper's /Being For The Benefit of Mr. Kite!.mp3 The Art of Noise/In No Sense? Nonsense!/How Rapid?.mp3 Joe Jackson/Look Sharp!/(Do The) Instant Mash.mp3
True Unix diehards are undoubtedly cringing at those filenames, which include specific wildcard characters and other elements that are important to the shell, all of which would have to be escaped. For example, you see how those filenames look now, just imagine them like this:
The\ Beatles/Sgt\.\ Pepper\'s\ /Being\ For\ The\ Benefit\ of\ Mr\.\ Kite\!\.mp3 The\ Art\ of\ Noise/In\ No\ Sense\?\ Nonsense\!/How\ Rapid\?\.mp3 Joe\ Jackson/Look\ Sharp\!/\(Do\ The\)\ Instant\ Mash\.mp3
One more thing: a filename must be unique inside its directory, but other directories can have files with the same name. For example, you may have files called chap1.doc and chap2.doc in the directory /Users/carol/Documents and also have different files with the same names in /Users/carol/Desktop.
This often causes great confusion for people who are used to just having all their files on their Desktop or in the topmost level of the Documents directory. In that situation, an attempt to save a file as chap1.doc would just generate a warning that the file already exists, but if you create different directories for different projects, it's quite feasible that you'll end up with a dozen or more files with the exact same name.