.bp
.ds F1 - % -
.ds Fd
.ds F3  ~
.ds Fl  Unit~II
.ds Fr  Module~II
.ds H1  ~
.ds Hl MP119
.ds Hr Introduction to UNIX
.PH "~~~~~~~~~~~~Module  II----Directory manipulation" 5 2 2 3
.PP
This module covers unit II objectives 4,5,6, and 7.
.PH "Objectives"
.PP 5
Upon completion of this module, the student will be able to:
.sp
.L1 5 2 1
Create directories
.L1
Transfer from one directory to another
.L1
Delete directories
.L1
Transfer files from one directory to another
.L1
Copy files from other user's directories to his own
.E1
.bp
.PH "Creating Directories"
.PP
In the last section it was established that you could create
subdirectories to group related files.  This is done by using the
following command:
.sp
.ti 20
% mkdir <name>
.PP
Where "mkdir"  stands for "make directory", and "name"
is the name assigned to the directory.  Suppose you were assigned the task
of placing this course on the system.  Wouldn't it simplify matters later
if it were placed on the system such that each unit is placed in a
separate directory.  This would obviate the need to make up new filenames
for the modules within each unit.
.sp
.ti 15
Example: % mkdir UnitI
.PP
This would create a directory called unitI.  You would then place the
4 modules for unit I in this directory.  You would do the same for each
of the other units, for example:
.sp
.in 20
% mkdir unitII
.sp
% mkdir unitIII, etc.
.in -20
.PP
All of the above directories will be placed or contained in your
login directory, which in this context is also referred to as a "parent"
directory.  A "parent" directory is any directory which contains at
least one other directory.  The directories created above can be depicted
as follows:
.sp
.nf
			    /u3/ <your login-name>

			    / | \\

			  /   |    \\

		       /      |       \\

	  UnitI    UnitII   UnitIII   UnitIV   UnitV
.fi
.sp
Got the idea? Then, do the following exercises.
.ne 10
.PH "Exercises"
.sp
.L1 5 2 1
Make two subdirectories, name them UnitI and UnitII.
.L1
Issue the command "ls -l"
.L1
There should have been two directories in your login directory
to start. What are their names?
.E1
.PH "Changing Directories"
.PP
Okay, you have created two empty directories, that is, they contain
no files. Therefore, the next thing you want to do is to remedy this
situation by placing some files into the newly created directories.
.PP
Before you can create any files in either of the directories, UnitI or
UnitII, you must transfer from your login directory to that directory.
The command to use is:
.sp
.ti 20
%chdir <name>
.PP
Where "chdir" stands for change directory, and "name" is
the name of the directory you want to transfer to.
.sp
.ti 15
Example:   % chdir UnitI
.PP
UnitI is now the current directory.  You can begin creating files,
or using any of the commands you used in your parent directory.
.sp
.in 20
.ti -10
Example:  % cat > module1
.br
<type information contained in module1>
.br
<ctrl-d>
.ne 8
.PP
Will create the file, module1, in UnitI.  Now, let's create a
directory in UnitI and call it UnitIa.  What command would you use?
.sp 2
% mkdir UnitIa, is correct.
.sp
Our internal structure now looks like this*
.ne 20
.nf
		  /u3/<your login-name>
		  /  |  \\

	       /     |    \\

	     /       |       \\

  UnitI  UnitII   UnitIII    UnitIV    UnitV

    |

    |

  UnitIa
.fi
.PP
Got the picture, see how easily you can build your network of
directories.  Now, suppose you want to transfer to UnitII, how
can this be done?  Well, one way is to issue the command:
.sp
.ti 15
% chdir /u3/<your login-name>/UnitII
.PP
You are probably wondering why your login directory was specified
here, and it wasn't specified when you transferred to UnitI.  The
answer is quite simple; a pathname isn't required if the directory
referenced is directly contained or was created within the current
directory.
.PP
Would a pathname be required to reference directory "UnitIa" from
your login directory?
.PP
Yes, of course, UnitIa is directly contained within UnitI, not your
login directory, therefore a pathname is required.
.PP
Another way to transfer from UnitI to UnitII is to transfer to your
login directory first, eg.
.sp
.in 20
% chdir /u3/<your login-name>
.br
% chdir UnitII
.in -20
.sp
You could have omitted the pathname; thus specifying only:
.sp
.ti 20
% chdir
.bp
.PP
The system will transfer you to your login directory
when a directory name isn't specified. Now, suppose you are in directory
"unit1a" and you wish to return to UnitI, what command would you use?
.sp
.ti 15
% chdir /u3/<your login-name>/UnitI
.sp
You're correct if your answer matches the above command. You could
have also used: %chdir .. ; if you specify ".." (two periods) instead
of the directory name, the system will transfer you to the directory
in which the current directory is directly contained, ie. the parent
directory for the directory you are currently in.
.sp
Note: chdir may be abbreviated as cd.
.PH "Transferrring to another's directory"
.PP
You may also transfer to another user's directory by using the "chdir"
command.  This feature doesn't offer any additional privileges; however,
it is recommended if you have a need to reference another user's
directory several times in a session. Of course, the other user must
have given you permission to work in his directory. This is normally
only done for memebers of the same group.
.PP
For instance, assume you wish to list the file names, display a couple of
files, and print a couple of files in user abc's login directory.  There
are a couple of ways you can do this.  One is by using pathnames
to reference abc's directory:
.sp
.in 20
% ls -l /u3/abc
.br
% cat /u3/abc/file1
.br
% cat /u3/abc/file2
.br
% opr /u3/abc/file1
.br
% opr /u3/abc/file2
.br
.ti -20
The other is to transfer to abc's directory:
.sp
% chdir /u3/abc
.br
% ls -l
.br
% cat file1
.br
% cat file2
.br
% opr file1
.br
% opr file2
.in -20
.sp
.ne 10
.in 9
.ti -9
NOTE:(1) The latter approach doesn't bog you down with the tediousness
of repeatedly specifying the pathname. This would be even
more effective if the referenced directory is more deeply
nested (farther up in the Unix tree).
.ti -9
~~~~~(2) If you try to transfer to someone else's directory and you
receive the response:  bad directory.  This means that there is no
such directory or that the owner has denied you permission to use
the directory.
.in -9
.ne 10
.PH "Copying files"
.PP
While browsing through your neighbor's directory, you may see a file
that you would like to have in your directory.  Well, why not copy it;
you can by using the "cp" (copy) command.  First transfer to your directory,
whichever directory you want to hold the copied file, then issue this command.
.sp
.ti 20
% cp <pathname> <filename>
.PP
Where "pathname" is the pathname of the file to be copied, and
"filename" is the name of the file to hold the copied file. For example,
suppse you wish to copy "file1" from abc's directory, then you would issue:
.sp
.ti 20
% cp /u3/abc/file1 file2
.sp
abc's file1 is now your file2.  "file1" remains unchanged.
.PH "Transferring files"
.PP
You may also copy or transfer your files from one directory  to
another directory by using the copy command. Assume there
is a file "junk" in your login directory, and you would like to
have it in your UnitI directory.  To accomplish this task, you
would issue the following sequence of commands:
.sp
.in 15
% cd UnitI
.br
% cp /u3/<your login-name>/junk junk
.br
% rm /u3/<your login-name>/junk
.sp
.in -15
This would copy the file "junk" from your login directory into the
directory, UnitI, and delete the file "junk" from your login directory.
.PP
Now something for you to do. Copy the file "junk" from the directory
"UnitIa" to your login directory. You are in UnitIa.
.ne 10
.sp 2
If your answer is the following, you deserve an A+.
.sp
.ti 10
% cd ..  or   %cd /u3/<your login-name>
.br
.ti 10
% cp /u3/<your login-name>/UnitI/UnitIa/junk  junk
.PP
The file "junk" now resides in both directories, UnitIa and your parent
directory.  This exercise also illustrates that the same filename may
exist in more than one directory.
.sp
.in 6
.ti -6
NOTE: In order to copy a file, you must be given the read privilege
for that file.
.in -6
.PH "The Move command"
.PP
The Move command can also be used to transfer files between your
directories.  It is very similar to the "cp" command except that
the file being copied is deleted from its directory. The form of
the command is:
.sp
.ti 18
% mv <pathname> <filename>
.PP
Where "pathname" and "filename" have their previous meanings.  Use
of this command would eliminate the need to use the "rm" command as
in the previous example.
.PH "Removing directories"
.PP
If you have deleted all the files from a directory, the next step
is to delete the directory also.  The command to use is :
.sp
.ti 18
% rmdir <directory-name>
.PP
This command is issued in the directory in which the directory
to be deleted is directly contained, ie. the parent directory of the
directory to be deleted.
.PP
In which directory would the above command be issued if you
wished to delete the directory "UnitIa" from our previous
example?
.sp 2
Right you are--you would issue the command, "% rmdir UnitIa", in directory
"UnitI".
.sp
Note: A directory must be empty before it can be deleted.
.bp
.PH "Exercises"
.L1 5 2 1
Transfer to the previously created directory "unitI", and create a file
named "junk".  It doesn't matter what you type in the file.
.L1
Copy the newly created file "junk" from UnitI and place it in "UnitII"
such that it exist in both directories.
.L1
Copy the file "junk" from "UnitII", place it in your " login directory"
and remove it from "UnitII".
.L1
Create the directory "U1a" in "UnitI"; issue the
"ls -l" command, and return to your login directory.
.L1
Copy the file "junk" from "UnitI" and place it in "U1a".
.L1
Delete the directory "UnitII".
.E1
.sp
.in 4
.ti -4
****If you are unable to perform these exercises, please review this
module or contact your instructor before attempting the Unit II
proficiency test.
.in -4
