Pathnames


This section discusses absolute and relative pathnames and explains how to use them to your advantage.

Absolute Pathnames

Every file has a pathname. Figure 4-5 shows the pathnames of directories and ordinary files in part of a filesystem hierarchy. An absolute pathname always starts with a slash (/), the name of the root directory. You can build the absolute pathname of a file by tracing a path from the root directory through all the intermediate directories to the file. String all the filenames in the path together, separating each from the next with a slash (/) and preceding the entire group of filenames with a slash (/). This path of filenames is called an absolute pathname because it locates a file absolutely by tracing a path from the root directory to the file. The part of a pathname following the final slash is called a simple filename, or just filename.

Figure 4-5. Absolute pathnames


~ (Tilde) in Pathnames

In another form of absolute pathname, the shell expands the characters ~/ (a tilde followed by a slash) at the start of a pathname into the pathname of your home directory. Using this shortcut, you can display your .bashrc startup file (page 259) with the following command, no matter which directory is your working directory:

$ less ~/.bashrc 


A tilde quickly references paths that start with your or someone else's home directory. The shell expands a tilde followed by a login name at the beginning of a pathname into the pathname of that user's home directory. For example, assuming he has permission to do so, Zach can examine Max's .bashrc file with the following command:

$ less ~max/.bashrc 


Refer to "Tilde expansion" on page 324 for a more extensive discussion of this topic.

Carbon Pathnames

The Carbon programming environment was used on Macintosh systems prior to the introduction of Mac OS X. Carbon pathnames are radically different from OS X pathnames and are discussed here because many applications still use them.

The Carbon environment uses the historical Macintosh model of a desktop holding mounted disks, which does not correspond well to the UNIX filesystem's single directory hierarchy. Furthermore, the Carbon environment represents pathnames differently from the UNIX environment. Carbon pathnames use a colon (:) as a separator, instead of a slash (/). No top-level root exists for Carbon pathnames; instead, they start with the name of a disk. For example, the UNIX-style pathname /Volumes/Images/backups equates to the Carbon-style pathname Images:backups.

Caution: Know which kind of pathname you are using

If you enter a UNIX pathname when a Carbon pathname is expected, or vice versa, the system may display an error message, not retrieve the correct file, or save the file in the wrong location.


Relative Pathnames

A relative pathname traces a path from the working directory to a file. The pathname is relative to the working directory. Any pathname that does not begin with the root directory (/) or a tilde (~) is a relative pathname. Like absolute pathnames, relative pathnames can describe a path through many directories. The simplest relative pathname is a simple filename. A simple filename identifies a file in the working directory. The examples in the next sections use absolute and relative pathnames.

Caution: When using a relative pathname, know which directory is the working directory

The location of the file that you are accessing with a relative pathname is dependent on (is relative to) the working directory. Make sure you know which directory is the working directory before you use a relative pathname. You can use pwd to verify the directory. If you are using mkdir and you are not where you think you are in the file hierarchy, the new directory will end up in an unexpected location.

It does not matter which directory is the working directory when you use an absolute pathname.


Significance of the Working Directory

To access any file in the working directory, you need only a simple filename. To access a file in another directory, however, you must use a pathname. Typing a long pathname is tedious and increases the chance of making a mistake. This possibility is less likely when you are using a GUI, where you click filenames or icons. You can choose a working directory for any particular task to reduce the need for long pathnames. Your choice of a working directory does not allow you to do anything you could not do otherwise, but it does make some operations easier.

Refer to Figure 4-6 as you read this paragraph. Files that are children of the working directory can be referenced by simple filenames. Grandchildren of the working directory can be referenced by short relative pathnames: two filenames separated by a slash. When you manipulate files in a large directory structure, short relative pathnames can save time and aggravation. If you choose a working directory that contains the files used most often for a particular task, you need to use fewer long, cumbersome pathnames.

Figure 4-6. Relative pathnames





A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net