Intro to UNIX for Programming Courses

This tutorial discusses:

  1. What computer accounts you need
  2. How our UNIX system stores files
  3. Making and going to directories (organizing your work)
  4. Downloading a file from the web
  5. Viewing/printing text files
  6. Compiling a program
  7. Editing text files
  8. Copying/moving/renaming files
  9. Running a program
  10. Using a data file
  11. Looking up UNIX commands (and C++ library functions)
  12. Returning to your home directory

  1. What computer accounts you need:

    Students in this course must get an account on our CSA network, which consists of machines that run the UNIX operating system. Students will also need to get an NT account to use the machines in our teaching lab, although we will often log into the CSA network from those NT machines.

  2. How our UNIX system stores files:

    Like other common operating systems (the Mac operating system or MS Windows) information on our UNIX systems is stored in files. Besides files, UNIX allows the creation of directories, in which other files and directories can be placed (a directory is the same as a folder in Mac-speak).

    Note: UNIX is case-sensitive, i.e., upper- vs. lowercase usually matters for file, directory and command names.

    All the files and directories on the CSA network form a file hierarchy, something like:

    File Hierarchy (or File System)
              /  <-- root directory (a slash)
            /   \
          home  ...
          /  \
      ugrad  course
             /    \
           ... ... 
                   / |  \
                 ... ... username <-- your home directory

    So that those that have accounts for this course (except CS majors) will have an account in the location:


    Note: Declared CS majors should have their accounts in:

    This is called the path of your home directory, the directory you (X) are placed in when you log in.

    A path just represents the location of a file or directory in the file hierarchy. For example, the first path above says that under the root (/) directory, there is a home directory, under it, a course directory, under it, a directory, and under that is where your username directory is.

    Since this path starts at the root (/), it is called an absolute (or full) path.

    Aside: There are two uses of the slash (/). It represents the root directory when at the beginning of a path. And, it is used to separate parts of the path.

  3. Making and going to directories:

    You should use separate directories to store files associated with a tutorial, lab or particular homework. Make a directory for this UNIX material by typing:

    mkdir unixlab

    which will add another directory to our picture:

    Bottom of File Hierarchy
       / |  \
     ... ... username <-- your home directory
              / X
            unixlab <-- new directory

    Aside: When making this new directory, note that we gave its location relative to the current directory. Paths like this, which start from the current directory, are called relative paths.

    Remember, paths that start from the root (/) directory, like /home/course are call absolute (or full) paths.

    Now go to the directory by typing:

    cd unixlab

    which will change your present location in the file hierarchy:

    Bottom of File Hierarchy
       / |  \
     ... ... username <-- your home directory

    To make sure you are there, you can type:

    and it will report your present working directory, which should end in unixlab.

  4. Downloading a file from the web:

    Let's look at a program. We've written a program that sums a series of numbers. The program's source code is in sum.cpp.

    To download this program into your unixlab directory, do the following in :

    1. Click on the sum.cpp link above to see the program.
    2. Choose Save As... from the browser's File menu.

    To make sure the program got saved correctly, go to your UNIX window and type the ls command to list the files in your current directory. See that it is there.

  5. Viewing/printing text files:

    One way to display a text file is to use the cat utility followed by the name of the file you want to view. In our case, you'd do:

    cat sum.cpp

    It will display the contents of the file on the screen. However, if the program is too long to fit in the window, you'll only see the end of the file on the screen.

    Since long files won't fit in one window, you can use less to view such files instead of cat. Do...

    less sum.cpp

    Less will allow you to page forward through the file using the <Space> bar and page backward using the b key. In that sense, it is a bit more useful than a similar utility called more.

    When you are done looking at the program, type q to quit less.

    If you needed a printout of this code, you could use lpr to send a copy to the printer, as in:

    lpr sum.cpp


  6. Compiling a program:

    The source code sum.cpp is not ready to be run on the computer. First, we must compile the program (and link it) into what is called an executable. The executable then can be run.

    Now, let's compile and link the program with our C++ compiler g++. All you need to type is "g++" followed by all the .cpp files that make up the program, as in:

    g++ sum.cpp

    If something is wrong with the source code, g++ will display error messages...If so, pay attention to the first error messages and the lines that they occur at:

    sum.cpp:41: unterminated string or character constant
    sum.cpp:39: possible real start of unterminated constant

  7. Editing text files:

    Since there is an error in the program, we'll want to edit the source code and fix the error. To edit or even create a text file, we can use the program emacs. To edit the source code, type the following at the UNIX prompt:

    emacs sum.cpp &

    Note: We always put the ampersand (&) at the end of commands that bring up their own window, since doing so gives us another prompt back right away where we can type more UNIX commands.

    Once Emacs is up and has loaded the source file, we can go directly to the line where the error is. To do so, type the command <Esc> x goto-line and hit <Return>. Then, enter the line number where the error occurs (here, line 39) when prompted to do so at the bottom of the Emacs screen.

    What is wrong with the line? Fix it.

    Now to save the file back to disk, choose Save Buffer from the Emacs Files menu.

    When you are done with Emacs, choose Exit Emacs from the Files menu.

    The source code should now be correct, so go back to the UNIX window and compile the program again.

    Aside: You can use the up arrow to get back to the previous g++ command and re-do it by pressing <Return>.

    This time you should have an executable named a.out (which is the default name for an executable).

    You can use ls to see that a.out is there. If there is an asterisk (*) at the end of a.out, it just means that file is executable, it is not part of the filename.

    Aside: If there are any other files there you don't recognize, where did they come from?

  8. Copying/moving/renaming files:

    If we wanted to change the name of a.out to something else, we could do it in 2 ways:

    1. Make another copy of the file, as in:
      cp a.out exec1
    2. Or, rename the file, as in:
      mv a.out exec2

      There is also an easy way to tell g++ to give the executable a name other than a.out. You can do so using the -o (minus oh, not zero) option to g++, as in:

      g++ -o total sum.cpp

      Note that the desired name of the executable follows the -o option. In other words, we have asked g++ to store the executable in a file named total.

    3. Running a program:

      To run the executable, you just type its name, total. Since this program scans in values, it will sit there waiting for you to type input. Type the following bold values when the program prompts you for them:

      Enter how many numbers I will sum: 2
      Enter number: 3.4
      Enter number: 4.5
      The sum is: 7.9
      Thank you for using the summer!

      The program should then tell you that the sum is 7.9.

    4. Using a data file:

      The program in sum.cpp uses the C++ object cin to get input. By default, input is typed at the keyboard but it can also be taken from a file.

      There is a file named data1 that can be used as input. Download it into your unixlab directory right now.

      To run the program again, this time taking input from the file, we will using input redirection. Instead of just typing total, we use:

      total < data1
      This will make the program read values from data1 instead of expecting us to type them from the keyboard.

      Note that the output is:

      Enter how many numbers I will sum: Enter number: Enter number:
      Enter number: Enter number: The sum is: 10
      Thank you for using the summer!

      All the prompts get printed, but you won't see the input since it comes from the file rather than the keyboard.

      Note that the program should say that the sum is 10.

    5. Looking up UNIX commands (and C++ library functions):

      Since you have limited disk space for this course, you may want to remove unnecessary files. If you do not know what command is used to remove files, you can look it up on our system.

      The UNIX Manual Pages let you look up information on UNIX commands (also C and C++ library functions), including how to use them. To view Manual Pages, we use the man command.

      Let's first look for a command to remove files:

      man -k 'remove files'
      rm              rm (1)          - remove files

      The -k tells it to look up the key phrase 'remove files', which has to be in quotes because of the space between the 2 words. (In practice, you might have to try different key phrases or words before you find what you are looking for.)

      It tells us that we want the rm command in Section 1 of the Manual Pages. Now, we can look at the documentation for rm by using:

      man rm

      Man displays the info just like the program less. Reading the documentation given by man, you can see that all we need to type to remove a file is "rm" followed by the names of files to be removed.

      Remove the executables exec1 and exec2 by using:

      rm exec1 exec2

    6. Returning to your home directory:

      Finally, since we are done with the files in the unixlab directory for now, let's go back up to our home directory. Since you are one directory below your home directory, you can just type:

      cd ..

      to go up one level. Dot dot (..) is a way to refer to the directory above.

      We could also use:

      cd ~
      since tilde (~) is shorthand for your home directory at the UNIX prompt (and in some applications, like Emacs). This works even if our home directory is not right above where we are.

      Finally, we could also just use:

      by itself, which also goes to your home directory.

      You can confirm where you are by using pwd.

    BU CAS CS - Intro to UNIX for Programming Courses
    Copyright © 1993-2000 by Robert I. Pitts <rip at bu dot edu>. All Rights Reserved.