Solaris Advanced User's Guide

Directories and Hierarchy

This section describes the directory hierarchy that the Solaris operating environment uses to manage and organize files.

Directory Hierarchy

Files are grouped into directories, and directories are organized in a hierarchy. At the top of the hierarchy is the “root” directory, symbolized by “/”.

In the following example, Figure 3–1, each directory in the file system contains many subdirectories. The / character distinguishes directory levels. The directory / (root) contains the subdirectories /usr, /bin, /export/home and /lib, among others subdirectories. The subdirectory /export/home contains user1, user2, and user3.

When you initiate commands, you specify directories and files by including the names of the directories that contain the files or directories you want to work with. The names of directories and the files beneath them, combined with slash separators, constitute a path name. For example, the path name for the user3 directory in the following illustration is /export/home/user3.

Figure 3–1 File System Hierarchy

The preceding and following contexts describe the diagram.

All subdirectory names 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. No conflict occurs 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 /export/home/user2 directory contains a file called report5, the path name for this file is /export/home/user2/report5. This example shows that the file report5 is within the directory user2, which is within the directory home, which is within the root (/) directory.

Printing the Working Directory (pwd)

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


$ pwd
/home/user1

Your output might be different from the example, as your directory structure is different. Your working directory is your current location within the file-system hierarchy.

Your Home Directory

Every user has a home directory. When you first open a terminal or a window, your initial location is your home directory. Your system administrator creates your home directory for you at your account creation.

Changing the Working Directory (cd)

The cd (change directory) command enables you to move around within the file-system hierarchy.


$ cd /usr/lib
$ pwd
/usr/lib

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


$ cd
$ pwd
/home/user1

Although your home directory is /export/home/user1, the cd command returns you to the/home/user1 because home directories are mounted on the /home directory by the automounter.

In the Bourne Again, C, Korn, TC, and Z shells, 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, you would change to that user's home directory.


Note –

The Bourne shell does not support the ~ shortcut.


If you use the Bourne shell, your system administrator might have configured the system so that you can type $home to specify your home directory. With this configuration, you type the following command to change your working directory to the subdirectory music in your home directory.


$ cd $home/music

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. The command cd .. changes the working directory to the parent directory, as in this example:


$ pwd
/home/user1
$ cd ..
$ pwd
/home

If your current working directory is /home/user1 and you want to work with some files in /home/user2, type the following command.


$ pwd
/home/user1
$ cd ../user2
$ pwd
/home/user2

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

Creating a Directory (mkdir)

To create a new directory, type the mkdir command and follow it with the name of the new directory.


$ mkdir veggies
$ cd veggies
$ mkdir broccoli
$ cd broccoli
$ pwd
/home/user2/veggies/broccoli

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 shorter name that defines the file or directory relative to the current working directory.

When you are in a parent directory, you can move to a subdirectory by 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.

Create 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.

Moving and Renaming Directories

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


$ pwd
/home/user2/veggies
$ ls
broccoli
$ mv broccoli carrots
$ ls
carrots

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


$ pwd
/home/user2/veggies
$ ls
carrots
$ mv carrots ../veggies2
$ ls ../veggies2
carrots

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

Copying Directories

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


$ cp -r veggies veggies3
$

The command in the previous example 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, the system displays an error message.

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 does not remove the directory.

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r.


$ rm -r veggies3
$

Caution – Caution –

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