Unix 9 -- Using Subdirectories in Unix to Organize Programming Projects

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 10 Homepage | Return to Math 61 Homepage]
[Return to Unix 8 Page ]

Contents


Subdirectories

"Directories" in Unix (and DOS) terminology correspond to "folders" in Mac and Windows terminology. It is a section of a disk in which files are kept, which is separate from any other directory (folder) on the same disk. This separation is so rigid that different directories (folders) can have files of the same name and there is no conflict.

There is additional information about directories, how to create them, how to move to specific directories, etc., in the Unix 7 page, under "Common Unix Practices" and "Directory Path Names."

Any directory may, in turn, have "subdirectories," producing a tree-like structure and organization for files.

Common Practice

It is common practice among some professional programmers always to create a separate directory for each program and never to store more than one program in a directory. This keeps files organized and prevents conflicts of filenames. It also enables the use of unique makefiles for each program (if there is only one program in any given directory).

In general, it is a good idea for students to create a separate directory for each course and then to create separate subdirectories for each program.

Creating a Directory

A directory is created via the Unix "make directory" command, abbreviated as mkdir. This command is followed by the name of the (sub-)directory to be created. For example, to create a subdirectory for Math 10, one could type:
           mkdir Ma10
(A standard convention is to begin the names of any directory with a capital letter.)

Connecting to a Directory

To connect to any directory, one uses the Unix "connect directory" command, abbreviated as cd.

By itself

           cd
will shift to the user's home directory (the directory one is in initially upon login).

To move to a subdirectory, one indicates the name of the directory after the command, for example:

           cd Ma10
To move to a parent directory, one put two dots as the "directory name," for example:
           cd ..
If one has created subdirectories within a directory (for example, a directory Mp1 within Ma10, then one can change to that directory (from the root ["home"] directory) either in two steps, as follows:
           cd Ma10
           cd Mp1
or in one step as follows:
           cd Ma10/Mp1
To verify (or find out) which directory one is in at any time, one can issue the "present working directory" Unix command, abbreviated pwd, which will return the full "pathname" of the current directory

The tilde, ~, indicates the user's home directory (see Unix 7, "Directory Path Names" for more information). By using the tilde with any full directory name, one can change from one subdirectory to another subdirectory. For example, no matter which subdirectory one is currently in, the following command:

            cd ~/Ma10/Mp1
will switch from the current directory to the indicated directory without first changed to the home directory.

One can also use the command pushd ("push" onto the the "directory" stack) or popd ("pop" from the "directory" stack) to shift between subdirectories and "remember" the previous directories one has been previously in. To recall all directories on the "directory stack," one uses the Unix command dirs.

Logging in and Connecting to a Subdirectory

When a person begins a new class, that student should create a new directory for that class (e.g., Ma10 as explained above). For each program, one connects to the appropriate "course" subdirectory first, and then creates a new subdirectory for that program (e.g., Mp1 as explained above).

After the course directory and the assignment subdirectory have been created, for subsequent logins, one need only type in the command, for example:

            cd Ma10/Mp1
to connect to the correct directory for the currect course (e.g., Ma10) and current assignment (e.g., Mp1).

Using this procedure, all work for other courses and other assignments will be in different subdirectories and there will be no name conflicts with files.


[Return to Math 10 Homepage | Return to Math 61 Homepage]
[Return to Unix 8 Page ]

This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 14 April 1999.