Chapter 3 introduced the Unix filesystem,
including an extensive discussion of the directory structure and how
to move around using cd
and
pwd
. In this chapter, we focus on Unix file naming
schemes — which aren’t the same as names
you’d see in the Finder, as you’ll
see — and how to rename, edit, copy, and move files.
You’ll also learn how to use Unix-based file search
utilities, which tend to be dramatically faster than Sherlock and
other GUI-based find utilities.
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. However, spaces
are a standard part of Macintosh file and folder names, so while we
recommend using only letters, numbers, dots, and underscore
characters for filenames, the reality is that you will have to work
with spaces in file and directory names.
The Finder, by contrast, dislikes
colons (which older versions of Mac OS
used as a directory separator, just as Unix uses the 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.)
Tip
T hough 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 lieu of spaces in your filenames. It’s 99% as legible, but considerably easier to work with. Further, in the interest of having files correctly identified in both the Finder and Unix, it’s a good habit to get into using the appropriate filename suffixes 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 spaces in its name, the shell will be
confused if you type its name on the command line.
That’s because the shell breaks command lines into
separate arguments at the spaces. To tell the shell not to break an
argument at spaces, either put quotation marks (“)
around the argument 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 using a backslash character, as shown in the
third example:
$ls -l
total 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 name
rm: 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 should also use a backslash
(\
) before any of the following special
characters, which have meaning to the shell: * # ` " ' \ $ | & ? ; ~
( ) <
> ! ^
.
A filename must be unique inside its directory, but other directories may have files with the same names. For example, you may have the 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.
Get Learning Unix for Mac OS X Panther now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.