OpenWindows Advanced User's Guide

3.3 Directories and Hierarchy

By now you know how to list, copy, rename, and delete files. However, you may be wondering about larger issues. Where are these files located? This section discusses the directory hierarchy. Read the following narrative carefully, and then try the examples in the sections that follow.

3.3.1 Directory Hierarchy

Files are grouped into directories, which are themselves organized in a hierarchy. At the top of the hierarchy is the "root" directory, symbolized by "/".

As shown in the following example, Figure 3-1, each directory in the file system can have many directories within it. The convention is to distinguish directory levels with the / character. With this in mind, notice that the directory / (root) contains the subdirectories /usr, /bin, /home and /lib, among others. The subdirectory /home contains user1, user2, and user3.

You specify directories (and files within them) by including the names of the directories they're in. This is called a path name. For example, the path name for the user3 directory in the illustration above is /home/user3.

Figure 3-1 File System Hierarchy


All subdirectory and file names within a directory must be unique. However, names within different directories can be the same. For example, the directory /usr contains the subdirectory /usr/lib. There is no conflict between /usr/lib and /lib because the path names are different.

Path names for files work exactly like path names for directories. The path name of a file describes that file's place within the file system hierarchy. For example, if the /home/user2 directory contains a file called report5, the path name for this file is /home/user2/report5. This shows that the file report5 is within the directory user2, which is within the directory home, which is within the root (/) directory.

Directories can contain only subdirectories, only files, or both.

3.3.2 Print Working Directory (pwd)

The command pwd (print working directory) tells you where you are in the file system hierarchy:

$ pwd

Your output will look somewhat different from that in the example, as your directory structure will be different. Remember that your working directory is your current location within the file system hierarchy.

3.3.3 Your Home Directory

Every user has a home directory. When you first open the Command Tool or Shell Tool window in the OpenWindows environment, your initial location (working directory) is your home directory. This directory is established for you by the system administrator when your account is created.

3.3.4 Change Working Directory (cd)

The cd (change directory) command allows you to move around within the file system hierarchy:

$ cd /usr/lib
$ pwd

When you type the cd command by itself, you return to your home directory. For example, if your home directory was /home/user1:

$ cd
$ pwd

In the C shell, the tilde (~) is used as a shortcut for specifying your home directory. For example, you would type the following to change to the subdirectory music within your home directory:

example% cd ~/music

You can also use this shortcut to specify another user's home directory. For example:

example% cd ~username

where username is another user's login name, would change to that user's home directory.

Note -

If you are using the Bourne shell, the ~ shortcut will not work.

If you are using the Bourne shell, it may be possible that your system administrator has configured the system so that you can type $home to specify your home directory. If this is the case, then typing:

$ $home/music

changes you to the subdirectory music in your home directory. Likewise, typing:

$ $homeusername

changes you to the specified user's home directory, where username represents another user's login name.

The directory immediately "above" a subdirectory is called the parent directory. In the preceding example, /home is the parent directory of /home/user1. The symbol .. ("dot-dot") represents the parent directory. Therefore, the command cd .. changes the working directory to the parent directory, as in this example:

$ pwd
$ cd ..
$ pwd

Suppose your current working directory is /home/user1 and you want to work with some files in /home/user2. Here is a useful shortcut:

$ pwd
$ cd ../user2
$ pwd

../user2 tells the system to look in the parent directory for user2. As you can see, this is much easier than typing the entire path name /home/user2.

3.3.5 Creating a Directory (mkdir)

It is easy to create a new directory. Type the mkdir command followed by the name of the new directory:

$ mkdir veggies
$ cd veggies
$ mkdir broccoli
$ cd broccoli
$ pwd

3.3.6 Relative Path Names

The full path name of a directory or a file begins with a slash (/) and describes the entire directory structure between that file (or directory) and the root directory. However, you can often use a much shorter name which defines the file or directory relative to the current working directory.

When you are in a parent directory, you can move to a subdirectory using only the directory name and not the full path name. In the previous example, the command cd veggies uses the relative path name of the directory veggies. If the current working directory is /home/user2, the full path name of this directory is /home/user2/veggies.

Try creating several different subdirectories, and then move around within this directory structure. Use both full path names and relative path names, and confirm your location with the pwd command.

3.3.7 Moving and Renaming Directories

You rename a directory by moving it to a different name. Use the mv command to rename directories:

$ pwd
$ ls
$ mv broccoli carrots
$ ls

You can also use mv to move a directory to a location within another directory:

$ pwd
$ ls
$ mv carrots ../veggies2
$ ls ../veggies2

In this example, the directory carrots is moved from veggies to veggies2 with the mv command.

3.3.8 Copying Directories

Use the cp -r command to copy directories and the files they contain:

$ cp -r veggies veggies3

This command copies all files and subdirectories within the directory veggies to a new directory veggies3. This is a recursive copy, as designated by the -r option. If you attempt to copy a directory without using this option, you will see an error message.

3.3.9 Removing Directories (rmdir)

To remove an empty directory, use the rmdir command as follows:

$ rmdir veggies3

If the directory still contains files or subdirectories, the rmdir command will not remove the directory.

Use rm -r (adding the recursive option -r to the rm command) to remove a directory and all its contents, including any subdirectories and their files, as follows:

$ rm -r veggies3

Caution - Caution -

Directories removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -rcommand.