CS 3750: Operating Systems I
Fall 2020 - Lab 1
Due September 10, 2020, at midnight
This is an individual assignment. All work must be your own.
You should not look at any other student's work (in whole or in part,
on paper or on screen), nor allow anyone else to look at yours, during
the course of this assignment.
Turn in your answers to these questions to Lab 1 in
the CS Homework system.
(Plain text - strongly preferred - only).
This lab will introduce you to the basics of command line interaction with
Unix operating systems.
The assignment begins with a set of examples on the use of Unix commands.
Please work through these examples, as they will help you complete the
assignment you turn in.
You might wish to begin by reading over
our Unix How To.
I. Logging into the Unix system.
Open any UNIX-like terminal that you prefer. Recent versions of Apple's Mac OS
are based on the Unix OS, so the 'Terminal' application available on any Mac
in our department CS lab, or your own home Mac computer, will be fine. Installing
Cygwin on your own Windows machine could
also give you a Unix-like environment on an otherwise Windows machine.
Or you could use the 'ssh' command to remotely log in to a CS lab computer, from your
own computer. If so, you may need to use 'scp' (secure copy) to also copy files
you download (see below) to the CS lab computer before you try to work with those files.
II. Unix shortcuts and wildcards
Some shortcuts and wildcards commonly used in Unix are listed here.
- / (forward slash) The forward slash alone denotes the root of the file system tree
- ~ (tilde) stands for the your home directory
- . (single period) stands for the current directory
- .. (double period) refers to the parent directory of the current directory
(the directory one level closer to the root of the file system)
- * (asterisk) stands for zero or more of any character
- ? (question mark) stands for any single character in a file
- | (vertical bar) activates a Unix "pipe" from one command to the next command
III pwd, ls, mkdir, cd, and cp
The following are some commonly used commands.
- The pwd command (or print working directory) is used to
output the path of the current working directory.
- The ls command lists the content of a directory.
- The mkdir command (or make directory) is used to create
a new directory.
- The cd command (or change directory) changes the current working directory.
-
The
cp
command is used to copy files and directories.
Examples
-
Type in the terminal command line:
pwd
then press enter. What you will see is the path of your current directory,
which, if you have not changed directory, is your home directory.
- To see a list of the files and directories currently present in your home directory type:
ls
- Now, create an empty directory called OpSys by typing:
mkdir OpSys
You can type
ls
to verify that this file was created (OpSys should appear in the output list).
And you can type
ls OpSys
to verify that this directory is in fact an empty directory (no output will be
displayed).
- To move into your OpSys directory type:
cd OpSys
You can verify that you are in the OpSys directory by typing
pwd
- Inside OpSys create a new directory called HW1, and move into this new directory.
- Now, copy into the HW1 directory the file FileA. (Click
on this link to download the FileA file. If you use "Save As" from a web browser,
the browser may rename the file "FileA.txt". If so, all commands below will have
to use FileA.txt instead of FileA.)
Use the "ls" command to verify that the file FileA is in here.
- If you saved FileA somewhere that is not your HW1
directory (like your Downloads folder), use "mv" to move it to your current
location (HW1). For example, to move FileA from ~/Downloads to my current
location (current directory), I would type "mv ~/Downloads/FileA .".
(Remember, . is shorthand for your current directory.)
- Now, if you are in your
HW1 directory, and want to move back into your
OpSys directory you can type " cd .. "
Or if you are in your OpSys directory and want to move back to your home
directory you can type either: " cd ../.. " or " cd ~ "
"cd .." moves you up one directory level. "cd ../.." moves you up two directory levels.
"~" is the shortcut name for your home directory.
IV. The cat command (program)
The cat program can be used to display, combine, and create new text files.
Examples:
- To display the text in FileA type:
cat FileA
- The cat command can be also used to create your own text files.
To create a text file called FileB type:
cat > FileB
then, press enter. Now what you input in the terminal is going to
be written into your FileB file. Type (line by line) the following text:
red cake Jan
blue candy Feb
cream mousse Mar
then press
^D
(that is: "control" key plus "d" key), this will terminate input and save the file.
- Display text in FileB using cat.
- Now, let's append more text to the end of the FileB file. Type:
cat >> FileB
(Note that in this case we are using the symbol " >> " instead of
" > ". " >> " appends to FileB; " > " overwrites FileB.)
Then, type the lines:
orange jelly 10
yellow icing 45
red jam 3
then ^D . Review your file using cat FileB.
- The cat command sends a whole file to the terminal window. If you want to
see only one screen-full of the file at a time, type:
more FileB
or type:
less FileB
To exit out of 'more' or 'less' type q. (Just q, not control-q.)
(There are some differences between the 'more' and 'less' tools, but they aren't
significant right now.)
- The cat command can be also used to combine two or more files. Type:
cat FileA FileB > FileAandB
this will create a new file FileAandB containing the text in FileA and FileB.
Review your file by typing cat FileAandB.
- Typing " ls FileA* "
will list all those items (files or directories) which name begin with " FileA "
(in this case FileA and FileAandB. Similarly, typing " ls *B ", list all those which
name ends with " B " (in this case FileB and FileAandB).
V. The grep command (program)
The grep command searches the given file for lines containing a match to the given strings or words.
Examples:
- To find those lines in FileAandB containing the word " red " type:
grep red FileAndB
-
Similarly, to find those lines containing the string " jelly 10 ", type:
grep "jelly 10" FileAandB
VI. Redirecting output using " > "
The symbol " > " is typically use to redirect the output of a command (generally) into a file.
Examples:
-
Type:
echo "Have a good day" > test1
This will write the text "Have a good day" into a file called " test1 ".
See the text in test1 using cat or more.
- Similarly, typing:
ls ~ > test1
redirects the list of your files and directories in your home directory to the
" test1 " file.
- Now, suppose that information in FileAandB represents
a list of food information, color, type and price. If we are asked to extract
from this list only red food, then to see if any of the red food is cake.
The following steps can be taken:
- First we need to extract from the list only those "red" color.
This can be done using:
grep red FileAandB > test1
- Then, extract only those whose type is " cake ":
grep cake test1 > test2
test2 will contain only red cakes.
VII. Finding what you need using the find command
The find command can be used to find a file based on a wide variety of
search criteria.
(Many more search criteria can be used. Use "man find" to learn more.)
- First, type:
mkdir OpSys1 OpSys2 OPSYS3 opsys4
and also type:
echo "Where am I?" > OpSys2/testfile.txt
- To discover if there is a file named testfile.txt somewhere below
your current location, type:
find . -name testfile.txt
- Or, to discover if there is any file or directory named anything that starts
with "Op" somewhere below your current location, type:
find . -name "Op*"
- Or, to discover if there is any file or directory named anything that starts
with "Op", in any upper or lower case, somewhere below your current location, type:
find . -iname "Op*"
- Replace . with any exact directory name you wish, to conduct searches that start
somewhere other than your current directory.
(Many more search criteria can be used. Use "man find" to learn more.)
VIII. Redirecting the output using " | "
The symbol " | " is use to send the output of a command to the input of
another. This process is know as piping.
Examples:
- Try the following command lines (one by one):
ls
ls | grep OP
ls | grep OP | wc -l
(wc counts. wc -l counts lines. So the last command above counts
how many files or directories have OP in their names. -l for Line -- not 1 as in 1, 2 3.)
IX. Looking at your history
You have been working for a while, and want to re-examine your previous actions.
- Type in the terminal command line:
history
- On the list produced, find one of the earlier grep commands you ran. Each
line of your list is numbered on the left. Suppose the number on the leftmost end
of the line of the grep command is 5. Then, to run that grep command again,
you type at the terminal command line:
!5
Use history to run one of your previous grep commands.
X. Looking at processes using ps command
The ps command can be used to examine processes running on your Unix system.
- Type:
ps
You should see a list of the processes running only in the Terminal application
you are using, probably a short list. One line per process.
- Type:
ps -A
Now the list produced will be of all processes running on the computer (that you
have permission to see).
- Let us start up a couple of processes that we will want to stop from
running. Type:
sleep 500 &; sleep 600 &;
(The sleep processes will run for the given numbers of seconds. The &s put
the processes into the background so that you get the command prompt back.)
- Type:
ps
As long as you typed ps within 500 seconds, you should see the two sleep processes
you started on the list. The pids, or process IDs, are in the leftmost column.
- Suppose you have a program in an infinite loop, and you need to stop it.
First you need the process IDs, which you now have. Suppose the process ID of
the sleep command is 100. Then you type:
kill 100
Carefully kill one of your backgrounded sleep processes. This is
a method for stopping programs that has no safety protections; the process is
killed with no opportunity to save data, so you can lose information.
Lab 1
Download this file, named lab1F20.tar.gz. Create an
empty folder to work in (you choose the name of the folder), and move
lab1F20.tar.gz into this empty folder. 'cd' into your new folder.
Type this command at the terminal window:
tar xf lab1F20.tar.gz
(If your web browser automatically uncompressed and extracted the "lab1/"
folder from the lab1F20.tar.gz file, you can skip this step.)
There should now be two files whose names start with "Popular" in your current
folder. Each line of these two files records information about the popularity
of particular baby names in the USA, between 2011 and 2016.
There should also be a lot of new folders.
In your answers below, the commands you give should work exactly "as is".
The grader should be able to copy your answer and paste it into a Unix command prompt,
and your answer should work.
Follow the next steps, and use the empty spaces to report
the command line(s) you used in each step:
- Use cat to display the contents of each one of the files whose names
start with "Popular". (Full credit for accomplishing the task using only one command.)
-
Use cat to combine the two files whose names start with "Popular" into one file.
- Use grep to select only information about the popularity of names
that start with 'Ala' among female babies.
(Ala and FEMALE will both be
on the line describing them, though the words will not be directly next to one another.)
- There is a file named findme.txt somewhere in the folders you extracted from
lab1F20.tar.gz. Use the "find" command to find it. Report both the find command
you used, and the name of the folder(s) you found findme.txt in.
- Use cat or more to look at the contents of the file you found.
Report here both the command you used and the first line of the file.
- There is a file whose name starts with the letters "hi" somewhere in the
folders you extracted from lab1F20.tar.gz. (Hi are the letters. They might be
upper or lower case.) Use the "find" command to find it.
Report both the find command
you used, and the name of the folder(s) you found the file in.
- Use cat or more to look at the contents of the file you found.
Report here both the command you used and the first line of the file.
- Enter the commands "sleep 500 &; sleep 600 &; sleep 700 &;".
Use ps to list
processes and grep to filter your list to just the processes with sleep in their
names. Report all the commands you entered.
(Cgywin note: The ; does not seem to have the same effect in Cgywin as it does
in normal Unix. You can type "sleep 500 &" <return> "sleep 600 &" as
two separate commands, instead.)
Hint: A full credit answer will use a pipe (or pipes).
-
Turn in your answers to these questions to Lab 1 in the CS Homework system.