Intro to UNIX for Programming Courses


This tutorial discusses:

  1. What do you need for this lab?
  2. How our UNIX system stores files
  3. Making and going to directories (organizing your work)
  4. UNIX Concepts
  5. Looking up UNIX commands (and C library functions)
  6. Returning to your home directory


  1. What do you need for this lab?

    Students in this course must get an account on our department network, which consists of machines that run the UNIX operating system. You should get an account which is not the one that you would have used for your CS1100 lab sessions. If you do not have an account, contact Mr. S. Ravichandran in the Software lab for getting your accounts created.

  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 department network form a file hierarchy, something like:

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

    So that those that have accounts for this course will have an account in the location:

    /home/username
    

    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 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:

    First, let us move to the Desktop. The UNIX command cd shall help us in changing the directory.

    cd Desktop
    
    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 cs2110
    

    which will add another directory to our picture:

    Bottom of File Hierarchy
    ------------------------
    
       / |  \
     ... ... username <-- your home directory
              / | \
                Desktop <-- the Desktop directory 
                /X
            cs2110 <-- 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/username are called absolute (or full) paths.


    Now go to the directory by typing:

    cd cs2110
    

    which will change your present location in the file hierarchy:

    Bottom of File Hierarchy
    ------------------------
    
       / |  \
     ... ... username <-- your home directory
               / | \
                Desktop <-- the Desktop directory 
                    /
                cs2110
                X
    

    To make sure you are there, you can type:

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

  4. UNIX Concepts

    Directory
    The UNIX concept of a directory is just like the Mac/Windows concept of a folder. Directories hold files and other directories. The UNIX system contains lots of directories in a nested structure.
    Subdirectory
    A subdirectory is simply a directory that is inside another directory. Since UNIX only has one top level directory, called the root directory, every other directory can technically be called a subdirectory.
    Root directory
    The top of the directory hierarchy on any UNIX system. It is represented by the directory named with just a slash, as in /.
    Current or Present working directory
    The current or present working directory is the default directory for UNIX commands. For example, using ls by itself lists files in the current directory. If you want to access files other than in the current directory, you have to use either a relative pathname or a full (absolute) pathname.

    When you log in, your current directory is your home directory.

    Home directory
    Your home directory is the directory allotted for all your stuff. It, and the files and subdirectories under it, are all yours.

    If you are in a CS course, your home directory will be:

    /home/username
    

    This just means that your home directory is under the home directory, which is under the root directory, i.e., /. This, of course, is an example of a full pathname.

    Path or Pathname
    A path or pathname is simply the set of directories that have to be traversed to get to a file. In other words, it is a way of giving an address for the location of a file, like a mailing address for a letter.

    An example path would be:

    cs2110/lab_0/p1.c
    

    which refers to a file called p1.c in the directory lab_0, under the directory cs2110 under whatever your current directory is. Note the use of the slash (/) to separate the pieces.

    Typically you'd use a path to tell a command what file to operate on. For example,

    ls -l cs2110/lab_0/p1.c
    

    uses ls to list information about the file p1.c. Since, in this case, p1.c is not in your current directory. you must give a path with the file name.

    Full or Absolute pathname
    A full pathname is just a path that starts at the root directory, i.e., begins with the slash (/) character.

    Any example full path is:

    /usr/bin/man
    

    which is the location of the man command.

    Relative pathname
    A relative pathname is just a path that starts from your current directory, and thus, doesn't start with a slash (/) character.

    Any example relative path is:

    cs2110/test1
    

    which refers to a file in the subdirectory cs2110 under your current directory.

    Command Argument
    Command arguments are those things that are typed at the prompt after the command name. Arguments are usually separated from the command and from one another by space. An example would be:

    ls -l cs2110
    

    where the command name is ls and the arguments are "-l" (a special kind of argument called a flag) and "cs2110".

    Command Flag or Option or Switch
    Command flags are typed at the prompt as part of a command. An example would be:

    ls -l
    

    in which case the flag -l (minus ell, not one) gives a longer set of information. Flags alter the default behavior of commands. For example, the command ls, by default, just lists the names of files. With the -l flag, it gives you more information about each file (or directory).

    Flags normally start with a minus sign (-) and consist of a single letter, like l (i.e., ell) or a whole word, as in -debug. For commands that take only single letter flags, more than one flags can often, but not always, be combined. For example, -l and -F could be typed as -l<SPACE>-F or -lF. In some cases, flags will start with a plus sign (+) instead. It is more typically for flags with pluses to be part of a pair of flags, e.g., -a to turn some feature on and +a to turn it off.

    Because some flags turn features on/off, flags are sometimes called switches. In addition, they may be called options.

    Text file/Binary file
    A text file is just like any other file, but it mainly contains letter, digits and punctuation characters. So, a textual e-mail message or the source code for a program is often stored in a text file. Since the coding we use for letters, etc. (in most computers) is the ASCII (pronounced ask-key) code, text files are also sometimes called ASCII files.

    Types of files that are not text files are executables and word processor documents and are called binary files. When you try to view binary files with text file commands, they look like garbage. Again, a text file only differs from a binary file in that information is stored in it using the typical symbols that you'd see on a typewriter.

    Source Code
    A source code file is a file that contains program code in C (or some other language). For C, these files usually have names ending in .c (and sometimes in .h). These files cannot be run on the computer. In order to get an executable, which can be run, you must compile and link the source code.
    Executable
    An executable is a file that contains a program that is ready to run. When using ls to view files, these executables might have an asterisk (*) after their names (though the * is not part of the file's name).

    To run an executable, you just type its name.

    Compiling and Linking
    Compiling and linking means to turn source code into an executable that can be run on the computer.

    Compiling and linking are actually separate procedures, though often you'll use one program to both compile and link. Technically, compiling puts all the source code into an intermediate form and linking links all the intermediate pieces and special libraries into a single executable.

    Often we'll use the term compiling for both compiling and linking.

    We'll compile and link our C programs by using the gcc compiler or by using the make utility.

    Debugger
    A debugger is a program that allows you to examine what an executable is doing while it is running. It is useful in finding errors in programs that compile fine, but that don't do what you want them to.

    Our debugger is gdb.

    Running a program in the Background
    Running a program in the background just means that while you are running the program you can do other stuff on the machine. This is exactly what we do when we use the ampersand (&) at the end of a UNIX command, so that we get the prompt back right away.

    If a program is not running in the background, we say it is in the foreground.

    Redirecting input into a program
    Many programs require you to give them some input. Usually the program expects input to be typed at the keyboard. Sometimes, however, you want to put that input in a file instead (for testing, etc.).

    UNIX allows you to use the less than (<) character to make the program take input from a file instead of the keyboard.

    Redirecting output from a program
    You can capture the output of a program and send it into a file.

    UNIX allows you to use the greater than (>) character to send a program's output to a file instead of to the screen.

    Piping output from one program into another
    You can capture the output of a program and send it directly as the input to another program.

    UNIX allows you to use the pipe symbol (|) to send a program's output into another program.

    Command History
    When you type UNIX commands on our system, a record (or history) of these commands is kept for your login session.

    Using the up and down arrow keys, you can scroll to a previous command, edit it (or leave it as is) and then press <RETURN> to perform it again.

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

    Since you have limited disk space, 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 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:

    cd
    
    by itself, which also goes to your home directory.

    You can confirm where you are by using pwd.

This tutorial has been adapted from here