Fall 2008 CSci 553 Lab 1

Lab 01: Part 1, In Class

Using the Unix Command Line


Instructions

Exercises 1.1 – 1.8 are to be completed in class before you leave. You need to create a directory in your home directory named “lab01” (you must use the exact same name, and remember that Unix is case sensitive, so “Lab01” is not the same name). Inside of the directory “lab01” you will create and edit a file called “lab01-part1.txt”. You should edit the file and type in your answers to the questions 1.1 – 1.8 before the end of class, and ask the instructor to confirm you are done with this part before leaving.


Use this lab time not only to find the answers to the questions, but to practice you skills in using the command line and editor to create directories, files and run commands. The first portion of the lab is to be completed in class so that you may ask the instructor for help and suggestions for any problems you may be experiencing learning the bash shell and command line commands. You may use any of the resources we have discussed to find the answers to the exercises, as well as the textbook and online source materials. It is of course suggested that you try out and test your answer where possible to verify it correctly answers the question.


Exercise 1.1


Answer: Use the man command to read manual pages. Also accept use the -h and/or –help flags which are standard on many unix commands to get help from them.


Exercise 1.2

Makefile biography.txt data enrolment.txt programs thesis


thesis programs enrolment.txt data biography.txt Makefile


Answer: ls -r



Exercise 1.3


Answer: Changes to your (the current user's) home directory, and to the user hpotter's home directories respectively. If you don't have permission to change into user hpotters directory, you will receive a Permission Denied error.


Exercise 1.4


Answer: head (by default prints 1st 10 lines, or head -10 works as well), head -25 and tail -12 respectively




Exercise 1.5


Name: Earth

Period: 365.26 days

Inclination: 0.00 degrees

Eccentricity: 0.02

Satellites: 1


$ diff earth.txt earth2.txt

3c3

< Inclination: 0.00

---

> Inclination: 0.00 degrees

4a5

> Satellites: 1



Answer: diff -B or diff --ignore-blank-lines

diff -i or diff --ignore-case











Exercise 1.6

-rwxr-xr-x 1 aturing cambridge 69 Jul 12 09:17 mars.txt

-rwxr-xr-x 1 ghopper usnavy 71 Jul 12 09:15 venus.txt



Answer: The user (aturing) , group (cambridge) and other users can all read mars.txt.

Only the user (aturing) can write the file mars.txt.

mars.txt was modified on Jul 12 at 9:17 am (times are shown in military time by default, so 9:17 pm would have been 11:17 time stamp.

chmod ugo+w mars.txt Can add write to group and other and/or use octal notation to update the file permissions. Also chmod a+w mars.txt can be used, a is shorthand for all. Having write permission to a file also implies that one can delete the file as well, so only need to add write permission to also add delete permission.


Exercise 1.7


Answer: rm a??.txt


Exercise 1.8


Answer: rm *.ch removes all files in current directory with the .ch extension

rm *.[ch] removes all files with either .c or .h extensions in the current directory


Lab 01: Part 2, Outside Assignment

Using the Unix Command Line


Instructions

Exercises 1.9 and higher are to be completed in your own time out side of class, but are to be finished no later than the beginning of next class period (Tuesday Jan XX). Use a new file to answer the questions for part 2, called “lab01-part2.txt”. This file should also be placed in your “lab01” directory.


These questions involve more of the advanced concepts, such as using pipes and I/O redirection, discussed in Lecture 04. To successfully complete some of these, you may need to combine 2 or more commands together using a pipe to obtain the requested results.


Exercises 1.G.* are more advanced questions, and only graduate (Csci 553) students are required to answer those questions.


Exercise 1.9


Answer: ls | wc -w (or wc -l) Gives a word count of normal (non-hidden files) .

ls -l | wc -l Is close but its count is 1 too high since it counts the total line as a file when it isn't.


Exercise 1.10

Name: Earth

Period: 365.26 days

Inclination: 0.00

Eccentricity: 0.02


$ grep Period *.txt

earth.txt:Period: 365.26 days

venus.txt:Period: 224.70 days



Answer: grep -l expression filepattern* or grep –files-with-matches expression filepattern* (grep -H is incorrect, it turns on file names when other options would normally turn them off, but it still displays matching lines. -l displays only file names of files with matches, not the matching lines as the question asks for.)


grep -n expression filepattern* or grep --line-number expression filepattern*



Exercise 1.11


Answer: ls data >> tmp.txt


Exercise 1.12


Answer: export PATH=$PATH:~/bin ( /home/csci553/username/bin works as well)

















-------------------------------------------------------------------------------------------------------------

Exercise 1.G.1


Answer: tar cvfz datafiles.tgz list-of-files-or-directories (to create a gziped archive, the c flag means create archive, and z means gzip the result)

tar cvfj datafiles.tgz list-of-files-or-directories (to create a bziped archive, again the c flag means create archive, and j stands for bzip the result)


Extra Credit: To do this as a 2 or 3 stage pipe, need to specify that result of tar command is sent to standard output rather than a file, and pipe standard input into the bzip/gzip command. For example, to do this with gzip/bzip we would:


tar cv list-of-files-or-directories | gzip > datafiles.tgz

tar cv list-of-files-or-directories | bzip2 > datafiles.tbz2



Answer: tar xvfz datafiles.tgz (extracts a gziped archive into current directory)

tar xvfj datafiles.tgz (extracts a bziped archive into current directory)


Extra Credit: Have to do decompression first and send uncompression results to standard output, then pipe into tar to unarchive

gunzip -c datafiles.tgz | tar xv

bunzip2 -c datafiles.tgz | tar xv











Exercise 1.G.2


Answer: find /usr/local -name “*.c” -print



Answer:

[harry@nisl lab01]$ find /usr/local -name "*.c" -print | xargs grep "KNEE"

/usr/local/webots/projects/samples/howto/controllers/biped/biped.c:#define KNEE 1