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


Absolute Pathnames


An absolute pathname represents the location of a file or directory starting from the root directory and listing all the directories between the root and the file or directory of interest.

Because absolute pathnames list the path from the root directory, they always start with the slash (/) character. Regardless of what the current directory is, an absolute path points to an exact location of a file or directory. The following is an example of an absolute pathname:

/home/ranga/work/bugs.txt

This absolute path tells you that the file bugs.txt is located in the directory work, which is located in the directory ranga, which in turn is located in the directory home. The slash at the beginning of the path tells you that the directory home is located in the root directory.

Relative Pathnames


A relative pathname enables you to access files and directories by specifying a path to that file or directory within your current directory. When your current directory changes, the relative pathname to a file can also change.


To find out what the current directory is, use the pwd (print working directory) command, which prints the name of the directory in which you are currently located. For example
$ pwd
/home/ranga/pub

tells me that I am located in the directory /home/ranga/pub.


When you’re specifying a relative pathname, the slash character is not present at the beginning of the pathname. This indicates that a relative pathname is being used instead of an absolute pathname. The relative pathname is a list of the directories located between your current directory and the file or directory you are representing.

If you are pointing to a directory in your pathname that is below your current one, you can access it by specifying its name. For example, the directory name:

docs/

refers to the directory docs located in the current directory.

In order to access the current directory’s parent directory or other directories at a higher level in the tree than the current level, use the special name of two dots (..).

The UNIX file system uses two dots (..) to represent the directory above you in the tree, and a single dot (.) to represent your current directory.

Look at an example that illustrates how relative pathnames are used. Assume that the current directory is

/home/ranga/work

Then the relative pathname

../docs/ch5.doc

represents the file

/home/ranga/docs/ch5.doc

whereas

./docs/ch5.doc

represents the file

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

You can also refer to this file using the following relative path:

docs/ch5.doc

As mentioned previously, you do not have to append the ./ to the beginning of pathnames that refer to files or directories located within the current directory or one of its subdirectories.

Switching Directories


Now that you have covered the basics of the directory tree, look at moving around the tree using the cd (change directory) command.

Home Directories

First print the working directory:

$ pwd
/home/ranga

This indicates that I am in my home directory. Your home directory is the initial directory where you start when you log in to a UNIX machine. Most systems use either /home or /users as directories under which home directories are stored. On my system I use /home.

The easiest way to determine the location of your home directory is to do the following:

$ cd
$ pwd
/home/ranga

When you issue the cd command without arguments, it changes the current directory to your home directory. Therefore, after the cd command completes, the pwd command prints the working directory that is your home directory.

Changing Directories

You can use the cd command to do more than change to a home directory: You can use it to change to any directory by specifying a valid absolute or relative path. The syntax is as follows:

cd directory

Here, directory is the name of the directory that you want to change to. For example, the command

$ cd /usr/local/bin

changes to the directory /usr/local/bin. Here, you used an absolute path.

Say that the current directory is

$ pwd
/home/ranga

From this directory, you can cd to the directory /usr/local/bin using the following relative path:

$ cd ../../usr/local/bin

Changing the current directory means that all your relative path specifications must be relative to the new directory rather than the previous directory. For example, consider the following sequence of commands:

$ pwd
/home/ranga/docs
$ cat names
ranga
vathsa
amma
$ cd /usr/local
$ cat names
cat: cannot open names

When the first cat command was issued, the working directory was /home/ranga/docs. The file, names, was located in this directory, thus the cat command found it and displayed its contents.

After the cd command, the working directory became /usr/local. Because no file was called names in that directory, cat produces an error message stating that it could not open the file. To access the file names from the new directory, you need to specify either the absolute path to the file or a relative path from the current directory.


Previous Table of Contents Next