Classwork 2: More Unix Practice


More practice using the basic UNIX/Linux commands

Note: at the end of the lab, you will use submit to turn in a transcript of your Linux session. If you do not finish all the steps, just submit as much as you get done.

Assignment 1

  1. Move to your home directory. Make sure you are there (pwd).

  2. Create a new directory named Personal. Verify that the directory exists.

  3. Move to the Personal directory. Verify that you are there.

  4. Use the nano editor to create a file called things2do.txt.

    linux1[32]% nano things2do.txt

    Once you have opened the file, you should type the following:

    1. Finish today's lab exercise.
    2. Finish homework 1.
    3. Bring my pet tarantula to CMSC104 for Show'n'Tell

    Save the file and exit nano (use Ctrl-X).

  5. Move back to your home directory. Verify that you are there.

  6. Start a transcript of your Unix session with

    linux1[33]% script
    Script started, file is typescript

    It's very important that you don't skip this step!!!

  7. List the contents of the Personal directory (ls Personal). It should contain only the file things2do.txt.

  8. Display the contents of things2do.txt on the monitor (cat Personal/things2do.txt OR more Personal/things2do.txt OR less Personal/things2do.txt). To exit out of less, you should type 'q'.

  9. Make sure you are in your home directory! Create another subdirectory called PersonalBackup in your home directory. Verify that it exists. Both Personal and PersonalBackup should be in your home directory.

  10. linux1[34]% pwd
    linux1[35]% mkdir PersonalBackup
    linux1[36]% ls 
    cs104  Mail  bin  www  Personal  PersonalBackup

    Copy the file things2do.txt from Personal to PersonalBackup.

    linux1[38]% cp Personal/things2do.txt PersonalBackup

    Look at the contents of the PersonalBackup subdirectory. It should now contain the file things2do.txt.

  11. Try to delete the Personal subdirectory. You will get a message that the directory is not empty. You must delete all files and subdirectories from a directory before deleting the directory itself. So,

    • Delete things2do.txt from Personal.
    • Look at the contents of Personal to make sure that it is empty.
    • Delete the Personal subdirectory.
    • Look at the contents of your current (home) directory to make sure that Personal has been deleted.

      linux1[40]% rmdir Personal/
      rmdir: `Personal/': Directory not empty
      linux1[41]% rm Personal/things2do.txt
      rm: remove regular file `Personal/things2do.txt'? y
      linux1[42]% ls Personal/
      linux1[43]% rmdir Personal
      linux1[44]% ls
      cs104  Mail  PersonalBackup  bin  www

  12. Move things2do.txt from the PersonalBackup directory to your current (home) directory.

    linux1[46]% mv PersonalBackup/things2do.txt .
    linux1[47]% ls
    cs104  Mail  PersonalBackup  bin  things2do.txt  www

    • Look at the contents of your home directory to be sure that things2do.txt is there.
    • Look at the contents of PersonalBackup to be sure that things2do.txt is no longer there.
    • Delete the PersonalBackup subdirectory.
    • Delete things2do.txt.

  13. Now you have to stop recording the transcript of your Unix session:

    linux1[49]% exit
    Script done, file is typescript

    • Check that you have a file named typescript.
    • Check that the file is not empty using cat.
    Submit your typescript file:

    linux1[49]% submit cs104_chang cw02 typescript

Assignment 2

Write a C program that prints out "I will not chew gum in class" twenty times.

  1. Use nano to edit a file called gum.c. Note that the filenames of C programs must end with .c.
  2. Use the following "Hello World" program as a template except print out "I will not chew gum in class" instead of "Hello World"

       #include <stdio.h>
       int main() {
          printf("Hello World\n") ;
          return 0 ;

  3. Use the cut-and-paste feature of nano to cut the line that prints out "I will not chew gum in class" and paste it back in. (Use ctrl-K to cut and ctrl-U to paste.)
  4. Save the file and exit nano.
  5. Start a script session:

       linux1% script

    Note: your previous typescript file will be overwritten. Make sure you submitted the previous version already.
  6. Use cat to display your program:

       linux1% cat gum.c

  7. Compile your program using gcc:

       linux1% gcc -Wall gum.c

  8. Use ls to make sure that you have a file called a.out.
  9. Run your program:

       linux1% ./a.out

  10. Quit your script session:

       linux1% exit

  11. Look at your typescript file to make sure it is correct:

       linux1%  cat typescript

  12. Rename your typescript file to typescript2:

       linux1%  mv typescript typescript2

  13. Submit your C program and the typescript file:

        linux1% submit cs104_chang cw02 gum.c typescript2

Be sure to logout completely when you have finished!