Unix

Introduction to Unix

This lab will familiarize you with some basic Unix commands.

Preparation

You should  read the following before working through this exercise.

A few more things you need to know

  • Many unix commands expect one or more filenames as input (e.g. grep word file, looks for word in file). Whenever a list of files is expected you can instead use "wildcards" to specify a group of files. The simplest wildcard is "*" which can stand for anything. E.g. more *.java will list the contents of all files ending with .java in the current directory. You can even use just "*" which will match all files in the current directory.
  • To move down into a subdirectory type cd subdirName. To move up to the parent directory type cd .. (that's cd followed by a space followed by two dots with no intervening space).

What to do

    1. Log on to one of the Windows lab machines or make sure you have some ssh client on your own computer.
    2. From the lab: Connect to the unix machine by going to the Start menu then Internet Tools, then _Unix_Telnet&SSH, then unix.

From your own machine: use your ssh program to connect to unix.ucsc.edu. On Macs you can simply type "ssh username@unix.ucsc.edu" at a command line (Terminal application in Utilities).

  1. Put your username (the part BEFORE @ucsc.edu) in the User Name field. The password is the same one you used to login to the lab machine.
  2. After typing several commands using tcsh, try using the arrow keys to see what happens. You can edit previous commands and then execute the modified command. You do not need to move the cursor to the end of an edited command, just hit the return key when ready.
  3. Type cd /afs/cats/courses/cmps012a-cm/b but DO NOT hit the return key, instead hit the tab key. Notice how tcsh is able to complete the filename for you (there is only one way to finish). If it doesn't work, check your typing carefully.
  4. Now hit the return key to actually cd into that directory. Type cd c and the tab key. Notice that this time your computer will beep after partially completing the filename. There are multiple ways to continue. You can type Control-d to see a list of completions or type some more characters if you know what you want next.
  5. Filename completion and command editing can save a lot of time and typing.
  6. Get back to your home directory by typing "cd" (then hit return/enter which you always need to finish a command).
  7. Make a class directory called "cs11" (use mkdir).
  8. Change to the new directory.
  9. Make a subdirectory for each of the 3 remaining programming assignments. Call them prog2, prog3, prog4
  10. Change back to your home directory (just type "cd").
  11. Use grep to find out which file(s) in /afs/cats/courses/cmps012a-cm/book-examples/chap2 contain the word "height".
    1. Use cd to first get into the directory /afs/cats/courses/cmps012a-cm/book-examples/chap2.
    2. Type: grep height *
    3. Look at the resulting output. Each line gives the name of the file and the line containing the word.
  12. Use grep and wc to find out how many lines in that same directory (.../chap2) contain the string "int".
    1. If you aren't still in the directory use cd to get there.
    2. Type: grep int *
    3. You could try counting the lines of output yourself but why not let the computer count them for you?
    4. You can use "pipes" to connect two or more unix commands, "piping" the output of one command into the input of the next.
    5. Type: grep int * | wc. This sends the output of the grep command to the input of the wc (word count) command.
    6. The output is the number of lines, the number of "words", and the number of characters that were found in the input to wc.
  13. You can move files between your computer and the unix.ucsc.edu machine using an sftp client. You can find some at: http://its.ucsc.edu/software/sftp/
  14. Try writing, compiling and executing the HelloWorld.java program on unix.ucsc.edu.
    1. Create a subdirectory in your home directory called HelloWorld
      %mkdir HelloWorld
    2. Move into the HelloWorld directory.
      %cd HelloWorld
    3. Copy the HelloWorld.java program into your directory. Don't miss the DOT at the end of the "cp" command below. That means the destination for the copy is the "current directory".
      %cp /afs/cats/courses/cmps012a-cm/book-examples/chap2/HelloWorld.java .
    4. After you "get" the HelloWorld.java file you can print it to the display with:
      %cat HelloWorld.java
    5. Then compile the program with:
      %javac HelloWorld.java
    6. Now look at what files you have. There should be both HelloWorld.java and HelloWorld.class
      %ls
    7. Now you can execute your program with:
      %java HelloWorld
    8. You should see ``Hello world!'' printed on the display.