CMSC 10200 Lab — Winter 2006
Unix Tutorial

To follow this page you must be able to open and use

See KDE for further help with these tasks.

You must have a CS (Computer Science) account to access the Linux Cluster of machines. See Request a CS account under Getting Started.

Goal of Unix Tutorial

You will need moderate proficiency using the commandline in Linux. The first five lessons in the basic tutorial is enough for this course. This page will guide you through some of the tasks you will need to perform for the first lab. You can then work through the tutorial at your own leisure.

  1. Unix Terminal: Description of terminal window
  2. Unix Manpages: Using the Unix Help Desk, the manual pages (called manpages)
  3. Navigating on Unix: Basic commands for moving around Unix. Create a new directory, cs102 in your home directory.
  4. File and Directory Management: Basic commands for copying, moving, removing and displaying files and directories.
  5. Bash Shell: It is very important that you are using the bash shell.
  6. Advanced Shell Commands A brief discussion of redirecting I/O and shell wildcards.
  7. Creating an alias: You will create an alias for rm, the Unix command for removing files. This may one day save your life.
  8. Locating files: Use the command locate to find the location of files in the file system.

The Unix Terminal

Linux is a command based operating system, based on the proprietary Unix Operating System. When you first enter the operating system, you will be given a window with a prompt. Here is what my terminal looks like:

Here is what you see

The shell is a program which interprets and executes the commands you place after the command prompt (which ends with the $.) More on the shell.

Unix Manpage

Unix comes with an online manual page (called manpages). The command man gives access to the manpages. Lets give it a try. Enter the following command after the command prompt, then press return:

man pwd

Here is what it looks like on the terminal:

You recieve the appropriate manpage after you hit return:

pwd is the name of a command to print the working directory. You can page down the manpage by hitting return, and leave the manpage by entering q (for quit.)

Lets try this command. We know from the prompt we are in the directory ~, which the shell interprets as /home/kaharris, for me. The command pwd prints the working directory, and we send our command to the shell by typing a newline:

The program pwd returned the current directory, found on the second line. Notice that the shell returns the prompt, ready for another command. (Unix commands are a legacy from the early seventies, when program names had to be short!)

You should try the manpages as you go through this tutorial, looking-up each command. You will find a fuller discussion of how to use the Unix manpages.

Navigating the Unix File System

You have seen one useful command for navigating the file system, pwd for finding-out where you are in the file system. You should read the first tutorial to learn more about

The rest of the lab expects you have read this tutorial. After you are finished with the tutorial you will need to perform the following task The next paragraph walks you through the steps, using what you learned in the tutorial.

You will make a directory called cs102 for your class assignments. The command is mkdir for make directory, and takes the new directory name as its argument, followed by return:

When you press return the prompt returns, but no output.

Now, we will verify that you created a directory by listing the contents of the current directory with the command ls. This command will be given no arguments, and produces a listing of all directories and files in the current directory:

I have many files and directories in my home directory, but you will notice the newly created cs102 in the last row of the first column.

Our next task will be to move one level in the directory tree, and enter the directory cs102. We will change directories from our current directory using the command cd with a single argument giving the new directory we want to enter:

The prompt now displays the path to reach our current location, the directory, ~/cs102. It says: Start in our home directory, ~, then proceed to the directory cs102 located inside our home directory. The forward slash, /, separates directories along the path.

  1. Verify that you are now in the directory ~/cs102 using the appropriate command.
  2. List the contents of the directory ~/cs102. You should not have any files or directories in ~/cs102, so the command ls you used for part 2 returns no response:

    It is easy to return to your home directory. Remember that when we previously used cd, we gave it one argument, the directory we wanted to move to. If we call the command with no arguments we are automatically returned to the home directory:

    How can you tell that you are in the home directory?

    File and Directory Management

    You will need to read sections 2.1 to 2.4 of Tutorial two for basic commands to copy, move, remove and display files and directories. The key commands that will be used later in this tutorial are

    • mv: move files and directories
    • rm: remove files
    • tail: display the last 10 lines (the tail) of a file.
    Additionally, the following command is handy for practicing
    touch file

    which creates the file if it does not already exist.

    The Bash Shell

    A shell is a program which interprets and executes the commandline. The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt.

    There are many shells available to you, but you are probably using the bash (Bourne again shell-- its a long story!) You can test this by entering the following command

    echo $SHELL

    You should see a file terminating in bash, as in the screen shot. If you do not see this, you will need to see me--it is very important for some of the tasks you will perform later. What is returned is the location of the program bash. The command echo prints what follows on the terminal, and the argument $SHELL is a shell variable, which the shell interprets as /usr/local/bin/bash. All shell variables begin with a dollar sign $, and are conventionally capitalized.

    Advanced Shell Programming

    The shell interprets what it reads on the commandline, before it executes a command. You saw one example above with shell variables, like $SHELL. The shell also recognizes wildcards. One example is * which matches any sequence of characters. For example, the following command lists all files in the current directory which end in .java

    ls *.java

    See section 4.1 of Tutorial four.

    Many unix programs return their output to the terminal. We say one example, echo, which displays what follows (interpreted by the shell) to the terminal. It is possible to redirect this output to write or append to another file. The symbols >> tell the shell to redirect output to append to the file which follows. Try the following experiment

    The symbol alone > tells the shell to redirect output to overwrite the file. Try the previous experiment replacing >> with > and compare the output. You can read more on redirection in sections 3.1 and 3.2 of Tutorial three.

    Creating an alias

    The command rm (discussed in tutorial two, section 2.3) removes files. It is very dangerous. We will create an alias for removing files, so that whenever you enter rm, you will be prompted if you really want to remove the file. One option to rm is -i:

    rm -i file_name

    which first verifies that you really want to remove file_name:

    To avoid accidently typing rm when we really want rm -i, we will turn rm into an alias which really calls rm -i. You will need to be in your home directory where the .bashrc file is located. Enter:
    echo 'alias rm="rm -i"' >> .bashrc

    You can verify that the lines

    alias rm="rm -i"

    have been added to the file .bashrc by entering the command

    tail .bashrc

    (see section 2.4 of Tutorial two.)

    Re-start your shell by creating a new terminal, so that bash reads the file .bashrc. Now try your new alias rm:

    Here is a brief explanation. The file .bashrc is a special file read by the bash shell at the beginning of a session (when you first open a terminal.) The command alias is interpreted by the shell to treat the term on the left of the equal sign as an alias for the term on the right:

    alias rm="rm -i"

    This treats rm as an alias for rm -i (the double quotes are necessary because of the space.)

    Locating Files on the File System

    I want to find the location of the Eclipse program (which will be needed later.) The command I will use is locate (look this up on the manpage.)

    locate *eclipse

    The program is located at /opt/eclipse/eclipse-3.1/eclipse. The directory /opt is for optional programs that are not standardly found in the Linux installation. In the Eclipse tutorial we will create an alias
    alias eclipse=/opt/eclipse/eclipse-3.1/eclipse

    to avoid having to type /opt/eclipse/eclipse-3.1/eclipse everytime we want to execute Eclipse.


Kenneth Harris
kaharris@cs.uchicago.edu