Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 4
Working With Directories


UNIX uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree. The tree has a single root node, the slash character (/), and all other directories are contained below it.

You can use every directory, including /, to store both files and other directories. Every file is stored in a directory, and every directory except / is stored in another directory.

This is slightly different from the multiroot hierarchical structure used by Windows and Mac OS. In those operating systems, all devices (floppy disk drives, CD-ROMs, hard drives, and so on) are mounted at the highest directory level. The UNIX model is slightly different, but after a short time most users find it extremely convenient.

This chapter introduces the directory tree and shows you how to manipulate its building blocks: directories.

The Directory Tree

To explain the origin and advantages of the directory tree, consider a project that requires organization, such as writing a book.

When you start out, it is easiest to put all the documents related to the book in one location. As you work on the book, you might find it hard to locate the material related to a particular chapter.

If you are writing the book with pen and paper, the easiest solution to this problem is to take all the pages related to the first chapter and put them into a folder labeled “Chapter 1.” As you write more chapters, you can put the material related to these chapters into separate folders.

In this method, when you finish the book, you will have many separate folders. You might put all the folders into a box and label that box with the name of the book. (Then you can stack the boxes in your closet.)

By grouping the material for the different chapters into folders and grouping the folders into boxes, the multitude of pages required to write a book becomes organized and easily accessible. When you want to see Chapter 5 from a particular book, you can grab that box from your closet and look only at the folder pertaining to Chapter 5.

You can carry this same method over to a project on your computer. When you start out, all the files for the book might be in your home directory, but as you write more chapters, you can create directories to store the material relating to a particular chapter. Finally, you can group all the directories for the book into a directory named after the book.


As you can probably see, this arrangement creates an upside-down tree with the root at the top and the directories branching off from the root. The files stored in the directories can be though of as leaves.


This brings up the notion of parent directories and child or subdirectories. For example, consider two directories A and B, where directory A contains directory B. In this case, A is called the parent of B, and B is called a child of A.

The depth of the directory tree is limited only by the fact that the absolute path to a file cannot have more than 1,024 characters. I cover absolute paths later in the chapter.

Filenames

In UNIX, every file and directory has a name associated with it. This name is referred to as the file or directory’s filename.

In addition to their filenames, every file and directory is associated with the name of its parent directory. When a filename is combined with the parent directory’s name, the result is called a pathname. Two examples of pathnames are

/home/ranga/docs/book/ch5.doc
/usr/local/bin/

As you can see, each of these pathnames consists of several “words” separated by the slash (/) character. In UNIX, the slash separates directories, whereas the individual words are the names of files or directories. The sum of all the words and the / characters makes up the pathname.

The last set of characters in a pathname is the actual name of the file or directory being referred to: The rest of the characters represent its parent directories. In the first example, the filename is ch5.doc.

The name of a file can be up to 255 characters long and can contain any ASCII character except /. Generally, the characters used in pathnames are the alphanumeric characters (a to z, A to Z, and 0 to 9) along with periods (.), hyphens (-), and underscores (_).

Other characters, especially the space, are usually avoided because many programs cannot deal with them properly. For example, consider a file with the following name:

A Farewell To Arms

Most programs treat this a four separate files named A, Farewell, To, and Arms, instead of one file. You look at a workaround to this problem in Chapter 9, “Quoting.”

One thing to keep in mind about filenames is that two files in the same directory cannot have the same name. Thus both of the following filenames

/home/ranga/docs/ch5.doc
/home/ranga/docs/ch5.doc

refer to the same file, but the following filenames

/home/ranga/docs/ch5.doc
/home/ranga/docs/books/ch5.doc

refer to different files because they are located in different directories. In addition, because UNIX is case-sensitive, you can have two files in the same directory whose names differ only by case. UNIX considers the following

/home/ranga/docs/ch5.doc
/home/ranga/docs/CH5.doc

to be different files. This often confuses users coming from the Windows or DOS environments.

Pathnames

In order to access a file or directory, its pathname must be specified. As you have seen, a pathname consists of two parts: the name of the directory and the names of its parents. UNIX offers two ways to specify the names of the parent directory. This leads to two types of pathnames:

  Absolute
  Relative
An Analogy for Pathnames

The following statements illustrate a good analogy for the difference between absolute and relative pathnames:

"I live in San Jose."

"I live in San Jose, California, USA."

The first statement gives only the city in which I live. It does not give any more information, thus the location of my house is relative. It could be located in any state or country containing a city called San Jose. The second statement fully qualifies the location of my house, thus it is an absolute location.


Previous Table of Contents Next