Section 4.2. Unix Directory Structures


4.2. Unix Directory Structures

Because the Web was spawned from the Unix environment, it follows many of the same conventions. Directory structure and pathname syntax are prime examples. It is important for all web designers to have an understanding of how directory structures are indicated on the Unix platform, because pathnames are used in hyperlinks and pointers to images and other resources.

Directories ("places" to store files) are organized into a hierarchical structure that fans out like an upside-down tree. The topmost directory is known as the root and is written as a forward slash (/). The root can contain several directories , each of which can contain subdirectories; each of these can contain more subdirectories, and so on. A subdirectory is said to be the child of the directory that holds it (its parent). Figure 4-1 shows a system with five directories under the root. The directory users contains two subdirectories, jen and richard. Within jen are two more subdirectories, work and pers, and within pers is the file art.html.

A pathname is the notation used to point to a particular file or directory; it tells you the path of directories you must travel to get to where you want to go. There are two types of pathnames: absolute and relative.

4.2.1. Absolute Pathnames

An absolute pathname always starts from the root directory , which is indicated by a slash (/). So, for example, the pathname for the pers directory is /users/jen/pers, as shown in Figure 4-2. The first slash indicates that you are starting at the root and is necessary for indicating that a pathname is absolute.

Figure 4-1. Example of a directory hierarchy


Figure 4-2. Visual representation of the path /users/jen/pers


The advantage to using absolute pathnames in links, image tags, and other places where you provide the URL of a file on the server is mobility. Because the pathname starts at the top of the server hierarchy, you can move the file to another directory on the server and the links won't break. The downside is that it makes it more difficult to test pages on your local machine, because your machine is likely to have a different root directory than the final destination server.

4.2.2. Relative Pathnames

A relative pathname points to a file or directory relative to your current working directory. When building a web site on a single server, relative pathnames are commonly used within URLs to refer to files in other directories on the server.

Unless you specify an absolute pathname (starting with a slash), the server assumes you are using a relative pathname. Starting in your current location (your working directory), you can trace your way up and down the directory hierarchy. This is best explained with an example.

If I am currently working in the directory jen and I want to refer to the file art.html, the relative pathname is pers/art.html, because the file art.html is in the directory pers, which is in the current directory, jen. This is illustrated in Figure 4-3.

Figure 4-3. The path pers/art.html relative to the jen directory


Going back up the hierarchy is a bit trickier. You go up a level by using the shorthand of two dots (..) for the parent directory . Again, consider an example based on Figure 4-1.

If I am currently in the jen directory, and I want to refer to the directory richard/work, the pathname is ../richard/work. The two dots at the beginning of the path take me back up one level to the to the users directory, and from there, I find the directory called richard, and then the subdirectory called work, as shown in Figure 4-4.

Figure 4-4. The path ../richard/work, relative to the jen directory


If I am currently in my pers directory and I want to refer to Richard's work directory, I need to go up two levels, so the pathname would be ../../richard/work, as shown in Figure 4-5.

Figure 4-5. The path ../../richard/work, relative to the pers directory


Note that the absolute path /users/richard/work accomplishes the same thing. The decision whether to use an absolute versus a relative pathname generally comes down to which is easiest from where you are and how likely it is that you will be moving files and directories around. Relative pathnames can break if files or directories are moved.

4.2.2.1. Using relative pathnames in HTML

When pointing to another web page or resource (such as an image) on your own server, it is common to use a relative URL, one that points to the new resource relative to the current document. Relative URLs follow the syntax for pathnames described above. For example, a hypertext link to art.html from another document in the pers directory would look like this:

 <a href="art.html"> 

The URL for the link could also be written starting from the root directory:

 <a href="/users/jen/pers/art.html"> 

Image tags also use pathnames to point to the graphic file to be displayed. For instance, this image tag in the art.html document:

 <img src="/books/4/439/1/html/2/../../daisy.gif"> 

points to a graphic named daisy.gif located in the jen directory. Two uses of ../ indicate that the graphic file resides in a directory two levels higher than the current document (art.html).

If you plan on doing your HTML markup by hand, pathname syntax will come naturally after a little practice. If you are using a WYSIWYG authoring tool (such as Macromedia Dreamweaver, Adobe GoLive, or Microsoft FrontPage ), you have the luxury of letting the tool construct the relative URL pathnames for you. Some even have site management tools that automatically adjust the pathnames if documents get moved.




Web Design in a Nutshell
Web Design in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009879
EAN: 2147483647
Year: 2006
Pages: 325

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