Skip Navigation
 Search | Directories | Reference Tools
UW Home > Discover UW > IT Connect > Unix > Unix Basics 

Working With Directories

Included on this page:

You will probably find that you eventually will accumulate dozens (or even hundreds!) of files. Grouping them into different directories makes them easy to keep track of. For example, a directory named Receipts might contain monthly receipt figures with files named jan, feb, and mar. You may want to create directories and add new files to them, or delete existing files or directories you no longer need.

Your home directory is assigned to you when your account is created. Whenever you log in to your account, your session begins in your home directory. Your home directory is named with your userid name, the name you use to log in. You control your home directory--you can set permissions that determine who can read, change, and execute your files. You also determine the structure of your home directory--you can create any other directories, or files you want within your home directory.

Figure 1: A Home Directory With Files and Directories

[Graphic: A Home Directory With Files and Directories]


Directory Names

Use the same rules for naming directories as you do for naming files (see File Names).

Make a Directory

To make a directory, use the mkdir (make directory) command. Enter:

    mkdir  directory

This will make a directory with the name you specify

To make a directory for your chemistry papers and assignments, enter:

    mkdir Chemistry

This makes the directory named Chemistry. If you begin your directory names with a capital letter, they are easier to distinguish from your file names.

List the Contents of a Directory

To view the file and directory names in your directory, use the ls (list) command. Enter:

    ls

To view the names of the files and directories in a different directory, enter:

    ls  directory

The ls command takes several command options that let you display additional file and directory information. The following list summarizes some of the most popular options:

    ls -l    Displays files in long format (see File and Directory Permissions and Access)

    ls -t    Displays sorted by time of last modification

    ls -r    Displays in reverse order

    ls -F    Displays contents and indicates file type

You can also use the ls command with several options at once:

    ls -lFt  directory

Change the Name of a Directory

To change the name of a directory, use the mv (move) command. Enter:

    mv  directory1  directory2

This changes the name of directory1 to directory2.

Delete a Directory

You can use the rmdir (remove directory) command to delete a directory. Before you can delete a directory, it must be empty of all files (see Delete a File).

To delete a directory, enter:

    rmdir  directory

Navigating Directories

The following section shows you how to move between directories. The directory you are working in is called your current directory. When you log in, your current directory is your home directory. When you move to another directory, that directory becomes your current directory.

Determine Your Current Directory

If you forget the name of your current directory, you can use the pwd (print working directory) command.

To display the name of your current directory, enter:

    pwd

For a user with the userid "jjaudubon," who is working in their home directory, the pwd command display would look something like this:

    /ua19/S0/jjaudubon

Pathnames

A pathname identifies the exact location of each file and directory in the Unix file system. Pathnames are described in two ways: absolute and relative.

Figure 2: A Unix Directory Structure

[Graphic: A Unix Directory Structure]

Absolute Pathname

In the Unix file system, the uppermost directory is known as the root directory (see Figure 2). All other directories are located under the root directory. An absolute pathname starts with a slash (/) to represent the root directory, then traces a path through the file system to a specific file or directory. You can specify any file or directory in the file system by means of its absolute pathname.

Because the absolute pathname starts at the root directory, it always begins with a /. All the names in a pathname are also separated by a /. Do not be confused by the dual meaning of the / character. It is both the symbol of the root directory and a separator between names. In the Unix file system, it is possible for more than one user to have a file called jan. Two files with the same name, for example jan in Figure 2, are distinguished by their absolute pathnames:

    /ua19/S1/User1/Receipts/jan
    /ua19/S1/User2/Vacations/jan

Relative Pathname

A relative pathname shows the route to any file or directory from your current directory.

Both User1 and User2 can access their files named jan from their home directories using relative pathnames:

    Receipts/jan
    Vacations/jan

Directory Abbreviations

You can use directory abbreviations to shorten the pathnames of a complex directory structure. The following list shows some useful abbreviations:
  ~   Your home directory
  ..   Parent directory-the directory just above your current directory
  .   Current directory
To copy a file from your home directory to your current directory, enter:

    cp ~/file  .

This command uses the abbreviations for your home directory (~) and your current directory (.) to copy the file you specify.

Move From One Directory to Another

The cd (change directory) command lets you move from your current directory to another directory, which then becomes your current directory. When you give the cd command you must specify the absolute or relative pathname of the directory to which you want to move.

To move to another directory, enter:

    cd  directory

This moves you from your current directory to the directory you specify.

To move to your home directory from anywhere in the file system, enter:

    cd