Lab #3
CS 2351
UNIX Operating System
Name _________________________________
Section ___________________
This lesson examines directory files and plain files. The following UNIX commands are used: pwd, cd, mkdir, and rmdir , lp, wc, date and cal. |
Directory and Plain Files
The UNIX file system is a flexible and easy to use system that is particularly well suited for software development. The UNIX file system is a tree structure which allows files to be placed into convenient groups by collecting them into a directory file.A file is a collection of related information. These are also called plain files. Files could be text, or some type of data base with both text and numbers, or it could be a computer program. Ordinary files are organized into special files called directories.
A directory contains zero or more (sub) directories and zero or more files. Directories are used to keep like items together in one place, similar to having a different note book for each class. A plain file corresponds to a leaf node on a tree since a plain file cannot have any successor. A directory file, corresponding to an internal node on a tree, may have one or more successors of either type.
The hierarchical structure of the file system is possible because of the directory files. The entire file system is a single tree with the root of the tree specified as /. The tree structure is like an upside down tree with the root at the top. A reference to the file named / is a reference to the root directory. Each user has a main directory, called the home directory. The home directory is the root of a subtree in the file hierarchy. The name of the user's home directory usually corresponds to the user's login name.
There are some rules for file or directory names. You choose names for any files and directories you make. UNIX file names may contain from one to fourteen of the following characters, in any combination:
Uppercase letters (A to Z)
Lowercase letters (a to z)
Numerals (0 to 9)
Period (.), underscore (_), comma (,)
The file names should NOT contain spaces or any of the following special characters:
& * \ | [ ] {} $ <> ( ) # ? " / ; ^ ! ~ %
It is always a good idea to choose reasonably short names to save typing. You should also choose names that convey information about the contents of the file. You will be learning many UNIX command names. DO NOT USE command names as file names.
EXAMPLES OF A UNIX DIRECTORY STRUCTURE

Words that are in boxes are directories. Words that are not enclosed in boxes are plain files.
1. The top directory is the root.
2. The first line of directories (bin, contrib, d, k, etc) are called the systems directory. These are similar for all UNIX systems. The boxes that have one letter in them (d, k, p, etc.) are considered partitions. There are many partitions on the SUN machine. There are several students in each partition. Each student is in one of these partitions.
3. The next row contains other directories. Notice the one marked "srp". This is a person's home directory. Each one of you will belong to a partition and have a home directory. The path name for this particular person is: /d/srp. This is an absolute path name because it starts with the root (/) and gives the entire path name.
4. Each person can have some files in his or her home directory. He or she can also have several directories where other files that are related can be kept. It is a way to keep things organized and easier to find. In this particular example, the person has one directory for MSIS homework and another for Cunix homework. These are then broken down into two other directories, prog and labs. The student can keep programs in the program directory and lab assignments in the labs directory. In each of these directories are files.
Pwd Command
The command pwd (print working directory) can be used to find out what directory you are in.
Type in: pwd
What is printed out? ___________________________________________________
This should also be the path for your home directory. When you make new directories and change to that directory, your working directory will change.
Mkdir Command
The mkdir (make directory) command is used to create a new directory. The reason you might make new directories is to keep files that are alike in one directory. An example would be class assignments for certain classes could be kept in one directory. Other class assignments could be in other directories.
Now, make a directory named sample within your own directory by issuing the command:
mkdir sample
Next, create another new directory named homework. What command did you use? ____________________________________________
Type in: ls -l
You should see sample and homework listed. You will see lists of files that look similar to this.
drwxr-xr-x 2 2351jje 2351jje
512 May 5 08:16 sample
drwxrw-rw- 2 2351jje 2351jje 512 May 5 08:20 homework
Cd Command
The cd command allows you to change to a new directory. Change to the directory homework by using the cd command:
cd homework
What command can you use to check that you are in the new working directory? _______________________________
What is listed now as your working directory?___________________________________
Create these next two items in the homework directory:
1) Create a new directory called cunix.
What command did you use to make this directory?______________________________
2) Create a new file assign1. This file is still in the homework directory.
Create assign1 by doing the following:
cat > assign1
Assign1 is a plain
file.
<ctrl-d>
Notice that you can use the cat command to enter information into a file instead of using the vi editor. This is a short cut method. However, if you make a mistake after you have completed a line and then hit the return key, you cannot make a correction on this file. For very short input files, this can be another method of making a file besides using the vi editor. When you have finished typing, you press the ctrl and d keys to get back to the prompt.
Type in the following: ls -l
Remember that there is always a space after a command and any options that are used. the "l" is a lower case "L" and not the number "1"(one).
What files are listed? ________________________________________________________
Is one of the files a directory?________________
If so, how do you know that it is a directory?______________________________________
In any directory there are two invisible files named "." and "..". These will only show up if you type in the type in the ls -a command. At this time, type in:
ls -a
You should see the "." and the ".." files. These refer to the current working directory (found by typing in pwd) and its parent respectively. The parent directory is the directory above the directory you are in at the time. It is one level up on the tree. You can change your working directory to the parent of the current working directory with the command cd .. . Make sure that you leave a space after the cd before you type in the two periods.
Enter the following sequence of commands.
pwd
cd ..
pwd
What is printed to the screen?______________________________________________
Your home directory is the parent to the directory "homework".
You can change to your login directory (also called home directory) at any time by typing:
cd
Rmdir Command
When you no longer need a directory, you can remove it by using the rmdir command. The command would be:
rmdir <directory name>
First, remove the sample directory that you made. Type in:
rmdir sample
Next, try to remove the homework directory. What message did you get when you tried to do this?
__________________________________________________________________________
You should have received an error message saying rmdir: directory "homework" Directory not Empty.You cannot delete a directory that has files or other directories in it. The files (and directories) must be removed from the directory first (using rm or rmdir). You must return to the directory you want to delete and remove files and any directories in that directory first. Then you must be in the level above (the parent directory) in order to remove the directory.
Return to the homework directory. Remove the directory, homework, by first removing the file and directory from it and then moving up to the parent directory. What commands did you use to remove the directory homework? _________________________________________________________________________
_________________________________________________________________________
Checking out different directories
Make sure that you are in your home directory. Type in:
pwd
What is the response?_________________________________________________________
If you are not in your home directory, type in cd before you do the following. You will now go browsing up the hierarchical structure of the UNIX file system. You will go to the root of the tree and over to some other directories. Type in:
cd ..
Next, issue the command pwd. What was the result? _________________________________
You are now in the subdirectory that contains the names of other users who are in the same partition as you are. You can find out how many other users have accounts in the same partition as you by issuing the following command:
ls | wc -l
What was the result?________________________________________________________
Wc Command
The wc command is used for counting the number of lines, words, and characters in a file or directory. The wc has three options:
-c Characters This option causes wc to display only the number of characters in a file. -l Lines This option causes wc to display only the number of lines in a file. -w Words This option causes wc to display only the number of words in the file.
If none of the options are listed after the wc, then you will receive three numbers. The first is the number of lines, the second is the number of words, the third is the number of characters. Type in the following:
ls | wc
What was the result? _______________________________________________________
In this particular case, the names make up one word and each line contains one file, so the number of words and the number of lines will be the same. Next, type in:
cd ..
pwd
What is the working directory now? ___________________________________________
You have reached the root of the tree. Next type in:
ls -l
You will get a list of the directories that are in the systems directory. You will now go to one of the subdirectories. Next, Issue the command:
orcd /class
You are now in the class directory. This directory contains special subdirectories for different classes. There is one for this class. It is called cs2351. This directory has files that you will use to copy back to your account. The system administrator sets up these special directories so others can access them. Change to the cs2351 directory.
What commands did you use to do this? _______________________________________________
What answer did you get? _______________________________________________________
Some directories are protected so others cannot access them. That is what is happening here.
Return to your home directory by typing in:
cd
The Print Command
Sometimes you want to print out files. Later your instructor may have you do other assignments that need to be turned in. You can print out files by using the lp command. This command requests that files be sent to a printer. The lp command also uses the -d option for destination.
NOTE: If you are at home or somewhere else where you are connected by a modem, the lp command will not work on your home printer. The lp command does not work for local printers. You can still issue the command from home, but the specified file will be printed at the designated printer on campus.
The command will look like this:
lp -d<printer name> <filename>
The laser printers in MS 108 are:
ms108pr1
and ms108pr2If you are located in some other lab besides MS 108, there are other printers in the rooms at OSU in Stillwater and also at OSU-Tulsa. You may need to ask your instructor for names of other printers.
If you happen to be in some other room on campus, the printers will have different names. There is usually a sign above the printer telling you the network name.
You don't have any long files to print out yet. In order to try out the print command, you can copy a file from the newuser directory that has been set up for students and faculty who are new to the Computer Science Department. This file will be used later in another lab. NOTE: There is a space between the cp and the directory address. One way to do this is to type in the following:
cp /pub/htdocs/newuser/employee employee
Another method that is shorter is to type:
cp /pub/htdocs/newuser/employee .
The first method above repeats the name of the file in the directory where you are located. You can change the name of the file and call it something different. You do not need to call it the same as what it was called in the directory you copied it from.
The second one allows you to copy the file to your current directory --noted by the . (period) at the end of the copy command. This saves you some time and typing and less chance of error since you have less to type.
Look at the file, employee. What did you have to type in to do that? _______________________
Next, combine the file practice with the file employee. Do this by typing in:
cat practice employee > combine
This should put the two files together. You are going to print the combine file out. However, there will be several of you printing out these files to the printer and they will all look alike. In order to tell which one is your file, you will add your name to the file. If you are using the vi editor, type in:
vi combine
At the beginning of the file, open a new line at the top of the file. Add your name on it and also the class (such as CS 2351, Section 1) that you are in.
Save the file again. Next, print it out. Type in:
lp -dms108pr1 combine
NOTE: If you are in another room besides MS 108, you will have use the name for the printer in the lab that you are in. Write down the printer name if you printed this outside of MS 108. Using a home printer is not an option here. _________________________________________________________________________
Notice that when you submit your request to the printer, you get back a comment:
request id is printer_1-365 (1 file) ***************************************************************************
Attach to this lab, the printout of the file that you
printed and submit it with the lab
***************************************************************************
The Date Command
The date command displays the time and the date. It gives the weekday, month, day, time, time zone, and year. Type in the following:
date
What message was printed? ____________________________________________________
The Calendar Command
This command displays the calendar. It can be used to find out what the calendar would be for a year or you can find out only a month. Type in the following:
cal 2003
What was the result of this command? _____________________________________________
Since this command fills more than one screen, what is the full command could you use to see the 2003 calendar it one screen at a time? _________________________________________________
If you wish to know what day your birthday will be on for this year, you can type in the month (from 1 to 12) and then the year. Use the cal command to type in the month and this year to find out what day your birthday is on. What command did you use to find out when your birthday will be?
_________________________________________________________________________
What day of the week is your birthday on this year? __________________________________
If you just want the calendar for the current month, you just type in:
cal
What is the current month? ___________________________________
You can find dates for years in the future or for years in the past. Spring semesters usually start the second Monday of January. Check the calendar for January of the year 2004 to see what the date will be when the semester starts. What will that date be? ________________________________________
At the end of this lab, you should have the following files in your home directory:
practice
filea
fileb
filec
employee
combine
List out the files to see if they are there. Do not remove any of these files. What command did you use to do this?______________________________________________
Please indicate any areas of this lab that were difficult to understand.
_______________________________________________________________________
_______________________________________________________________________
05/28/03