Set 8
Home Up Introduction Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set 11

 

Shell and Command Skills

Due: Monday January 11, 1999

Hand in your output during class on Monday.  (I expect you to complete most of the lab during Friday.)

Purpose:

Develop skills using Unix tools.

Assignment:

  1. You are in your home directory. Give the shortest shell pattern that would match all the files named "UNTITLED" in all the home directories on your machine. (How many of these files are there currently?  Count them using a Unix command pipeline.)  You may assume that all home directories are sub-directories under the same directory (though that may not always be true for all Unix systems).
  2. What is the contents of file "hoopla" after the following sequence of commands?
        echo first >hoopla
        var=">hoopla"
        echo second $var         
  3. What appears on your screen after the following sequence of commands?
        var="hello ; echo goodbye"
        echo $var         
  4. What appears on your screen after the following sequence of commands?
        var=".*"
        echo First "$var" and then $var 
  1. Can you leave vi using ":q" (colon q) if you have unsaved changes in the work buffer?
  2. Create three terminal windows on your desktop. Create a file named "woops" with the text "first" in it.  Open the file in the vi editor in the first terminal window.  Change the word "first" to be "FIRST".  Do not save the work buffer back to the original file yet. Look at the contents of the "woops" file using the third terminal window.  Note that it has not yet changed, since we have not yet written out the changes back to the file.

    In a second terminal window, open the same file "woops" using vi again. Note that, since you haven't saved the work buffer of the first vi back to the file, the file still has the content "first". Change the word "first" to "SECOND", save the work buffer, and exit vi.

    Go back to the first terminal window.  Save the work buffer and exit vi.

    What is the content of the file?

For the next set of questions, give your Unix command lines to perform the following functions.  Where standard input processing is required, use Unix commands, do not simply read a file into an editor to answer the questions. 

Your answers must be made up of a Unix pipeline of commands that can read from a file, or from standard input, and write to standard output.  For example, to perform the sort operation on standard input that has leading numbers on each line, the command would be "sort -n", and it could be used any of these ways:

$ sort -n <filename         # sort reads from stdin
$ cat filename | sort -n    # sort also reads from stdin

Your Unix command pipeline answers must all be able to use standard input in a similar way.

  1. A pipeline to display the first 5 lines of standard input, numbered with line numbers.
    (Hint: See the manual page for cat to get line numbered output.)
  2. A pipeline to display the lines of standard input in reverse order, last to first.
    (Hint: sort can sort numbers backward as well as forward, using the right command line option.)
    If you add any text or fields to the lines to make this work, you do not have to remove the added information in your output.  As long as the lines are there, in reverse order, other extraneous information is acceptable.  Bonus: Remove the extraneous information.
  3. Display everything after the first 5 lines of standard input.
    That is, start the display of the file on line 6 and go to the end.
    (Hint: "man tail")
  4. Display only lines 10-15 of standard input.  (Try doing this both with and without using sed.)
    What happens if the input only has 12 lines? What happens if the input only has 3 lines?
  5. Display from the 20th line from the end of standard input to the 15th line from the end of standard input.
    For example, if standard input had 27 lines, your command pipeline would show lines 7 (27-20=7) to 12 (27-15=12). If standard input had 127 lines, your command pipeline would show lines 107 to 112. What happens if standard input only has 12 lines? What happens if standard input only has 3 lines?
  6. The "-s" option to ls gives file sizes (in disk blocks, usually either 512 or 1024 bytes).
    Write a command pipeline that shows the five biggest files in the current directory.
  7. Without any options, the "ls" command sorts its output by file name.  You can also give it options to sort by other criteria, such as access or modify time.  Write a shell pipeline that shows the ten most recently accessed files in the current directory.  (This is useful if you can't remember the name of a file you just looked at!)

Hand In: 

On paper, submit the cut-and-paste output of the command lines you used and the output they generated.

Additional material:

UNIX help for new users

A beginner-level introduction to the UNIX operating system

Unix - Frequently Asked Questions

How do I remove a file whose name begins with a "-" ?
How do I remove a file with funny characters in the filename ?
How do I get a recursive directory listing?
...etc...

Unix Horror Stories

From: barrie@calvin.demon.co.uk (Barrie Spence)
Organization: DataCAD Ltd, Hamilton, Scotland

My mistake on SunOS (with OpenWindows) was to try and clean up all the
'.*' directories in /tmp. Obviously "rm -rf /tmp/*" missed these, so I
was very careful and made sure I was in /tmp and then executed
"rm -rf ./.*".

I will never do this again. If I am in any doubt as to how a wildcard
will expand, I will echo it first.