LAB #1: Introducing UNIX



The Math lab PC's use the LINUX operating system, a dialect of UNIX. UNIX is the language we'll need to speak when we want the computer to create places for us to work, to store information in files, or to run programs. This lab will practice the basic skills we will need.


To use the machine, you need a username and password. Once the machine gives you access, you begin your session with a work area known as your home directory. You will be assigned a limited amount of space that you can arrange as you like, and which will be available to you the next time you log in. Your sessions always start in this place.

Exercise: Your home directory has a "name". To find out what this name is, use the present working directory command:


Unix organizes all the work areas of all the users into a hierarchical system. At the very top is a single directory called "/" or slash. Beneath this are subdirectories, including /bin for certain executable programs, /lib for compiled program libraries, and /usr, where the users (like us!) are put. Each subdirectory can have further subdirectories beneath it. For instance, under the /usr directory there is probably a subdirectory called /local, which is referred to as /usr/local; the /man directory under that is /usr/local/man and so on.

Directory Listings

There may already be a few files in your home directory. To see a listing of the files in your current directory, type:

For a more detailed listing that includes information such as file size, try:
ls -l

Exercise: Any file whose name begins with a period is a "hidden file" which will not show up under the usual ls command. However, the command ls -a will force such files to be included in the directory listing. Try a detailed listing of all your files using the command:

ls -la
(The a and the l are optional arguments to the ls command. You can include either one, in either order. There are many other such options, which you can find out about by typing man ls.)

Deleting Files

There may not be any files in your directory. One way to make a file quickly is to use the touch command:

touch file_name
If a file of this name already exists, then the touch command doesn't hurt it. But if no file of this name exists, touch creates an "empty" file (which you are free to fill up with information later, if you like).

Once we make a few files, we'll probably want to get rid of some of them. We can do this with the rm command:

rm file_name
You can use wildcards, especially the "*", so that a command like
rm math*
gets rid of all the files whose names begin with "math", while
rm *.f
removes files with the ".f" extension. The deadly command
rm *
deletes all files in the current directory.

Exercise: Use the touch command to create the files einstein.f, euler.f, and euclid.c. Now use a single command to get rid of euler.f, and euclid.c but not einstein.f.

Moving Up and Home

You can move around the directory system using the change directory command, which is cd. Note that every directory except for "/" is a subdirectory of some other directory. The symbolic name of the current parent directory is ".." or dot-dot. There's a special version of the cd command that allows you to go up to the current parent directory:

cd ..
Just typing this command enough times will take you to the top of the directory system!

Your home directory has the special name "~" or tilde or twiddle. No matter where you are, you can always move back to your home directory by typing

cd ~
and this is such a common thing to do that on some systems, you can even leave off the destination and get home:

Your current directory, whatever it is, has the special name of "." or dot. There's no reason to use this name with the cd command, (what would the command cd . do?) but we'll see its uses later.

Exercise: Starting from your home directory, use the cd command to move up, one subdirectory at a time, until you reach the "/" directory. Each time you move up one level, use the pwd command to verify where you are. Now issue the ls command and see how the top level directory is organized. Now go home.

Making Directories

We saw lots of subdirectories on our trip up the file system. We can make our own new work areas, but only as subdirectories of our home directory. Once you have a subdirectory, you can make a sub-subdirectory of that, and so on. To make a subdirectory of your current directory, you use a command like

mkdir name
If you want to delete a subdirectory, the command is
rmdir name

Exercise: For our later work, we will need some subdirectories. Make three subdirectories of your home directory, called c, fortran, and matlab. Now, in your home directory, get a directory listing. Your new directories should show up, along with any files you have.

Moving Down or Anywhere

We already saw simple uses of the cd command to go up or home. Now we need to see how to go "down", or, in fact, anywhere we want. To do so, we need to specify where we want to go. There are two ways to do this, the global and local directory addresses. Usually, the local address is simpler to use.

The global or absolute address points to a directory or file's location by specifying the full path name, that long string printed out by the pwd command. A global address always begins with a "/". For instance,

cd /usr/local/bin
It's best to use the global address when the place you want to go is "far away", or not a subdirectory of yours.

The local or relative address points to a directory or file's location relative to the present working directory. If we are in our home directory, and we want to go "down" to the matlab subdirectory that we just created, then we simply type

cd matlab
If the matlab subdirectory had a subdirectory beneath it, called, say data, then we could get there from our home directory in one step, using the local address, by typing:
cd matlab/data

Exercise: Go up to the "/" directory in one step. (it's easy). Now step down to your home directory, going down just one level at a time. Your first command will probably be

cd usr
Now go to the directory /usr/local/bin and, moving up or down a single level at a time, get back home.

More About Addresses

Directory information can also be used with the ls command, or indeed any other commands, such as rm, touch and so on. You can use the ls command with an absolute or relative address to get listings of other directories, without having to move there:

ls /usr/local/bin
ls fortran

Exercise: It will be convenient if we have a file called startup.m in our matlab subdirectory. For now, we don't actually need anything to be in this file, we just want it to be there. Without moving to the matlab directory, use the appropriate touch to make this file there. Then, from your home directory, use the command

ls matlab
If you did everything right, the new file should show up.


We will need to create our own text files. To do so, we need an editor program. Unfortunately, the editors on most UNIX systems are pretty bad. You can always edit on your favorite home system, and ftp files back and forth, but it helps to have some way to do editing locally. You might already know how to use the fancy emacs or vi editors. I will discuss two simpler ones, ed and xedit.

If you already have a favorite editor, and it's available on the lab machines, you do not need to learn the ed or xedit editors. Use your editor to create the file my_file.txt.

The ED Editor

ed is a "line editor" - that is, it edits and displays your file a line at a time, without using a graphical interface. ed is easy to hate, but you will often find that your favorite fancy editor isn't available on some system, and if you don't have a backup, you're dead.

On some systems, ed might not be available, but edit may work almost the same. Try typing

which ed
to see if the operating system can find the program for you.

You start the program by typing something like

ed my_file.txt
and then type a meaning append to indicate that you want to add stuff to the file. What you type now will go into the file my_file.txt, until you type a single period in column 1. Then type w to save the changes, and q to exit. Here, with some helpful indentation, is a suggestion of how this works:
        ed my_file.txt
            Here is an example of
            a file being created with
            the ED editor.
Once you've made the file, you want to go back and make changes in it. Here are some commands you can give:
        17              goes to line 17
        $               goes to the last line
        .=              print the number of the current line

        r FRED          copies the text of the file FRED into this file

        a               append text after the current line
        i               insert text before the current line
        d               delete the current line
        m 23            move the current line to just after line 23
        p               print the current line
        s/the/one       change the first "the" to "one" on the current line
        /tag            find the next line containing "tag"
Many commands allow you to specify a range of lines. For instance
10,20p prints lines 10 to 20.

Exercise Create the file my_file.txt using ed.

The XEDIT Editor

xedit is a simple screen editor, which shows you a chunk of lines from your file, and allows you to scroll smoothly up and down through the file, as though it were printed on a very long sheet of paper.

Thus, to enter and alter text is very simple. You simply position the cursor in the desired location and click. You can highlight text to be cut and then paste it elsewhere or discard it.

Search is done using Control-S, and replace with Control-R. Menu options allow you to save the file, read in a different file, or quit.

Exercise Create the file my_file.txt using xedit.

Doing Things With Files

A file is placed in a directory, usually your present working directory. If you're in your home directory, and you run the editor and create the file bob, then when you leave the editor, bob will be sitting in that directory. You might not like the name bob. In that case, the mv command can be used to rename it:

mv bob dave
More often, the mv command is used to actually move the file. For instance, to move bob into the subdirectory notes:
mv bob notes
mv bob notes/bob
The first command is a little risky. If we don't actually have a notes subdirectory, then the command changes the name of the file bob to notes, which is not what we wanted. The second command is safer. If the notes directory doesn't exist, the command won't be carried out.

The cp command will let us make a copy of the file:

cp bob dave
In the common case where you want to copy a file from some other subdirectory to your current directory, you can specify the second name as simply ".", and the copied file will keep its original name:
cp /usr/local/bin/bob .

The ls command can be used to get a directory lising of just a particular file instead of the whole directory:

ls bob
To examine the contents of a file, type
more bob
To delete a file, type
rm bob

File Transfer to Floppies

You can copy files to or from a floppy disk inserted into the computer. This can be a handy way to save your work from one lab session to the next.

To use the floppy drive, you need to mount it, so that LINUX knows it's there. Try issuing the xmount command. You should see a window open, with a few devices listed, one of them being the floppy drive. Next to the floppy there should be an option labeled Mount, and you want to click on this.

Now you just need the cp command, and the correct path name of the floppy drive:

cp my_file.txt /mnt/floppy
to copy a file to the floppy, or
cp /mnt/floppy/my_file.txt .
to copy from the floppy. (Here, "." stands for your current directory, which is where the copy will be placed.)

(When you no longer need the floppy drive, or you are ready to log off, you should use the xmount command again, this time to unmount the floppy drive.).

Exercise:: try to copy your file my_file.txt to a floppy. If you don't have a floppy disk, you can borrow one from the instructor. After you copy the file to the floppy, verify that it got there. First, issue an ls command, while still in your home directory. Then, move to the work area associated with the floppy drive, and to an ls right there.


You print a file using the lpr command:

lpr -Pprinter file_name
For our work, the printer will usually be hp707, but you might also want to print to hp625 some times.

Exercise:: modify your file my_file.txt, by using an editor. Insert your name as the first line of text. Then try to print the file.

Internet File Transfer

You can copy files to or from another computer, using the ftp program. For example, if you're logged in on a lab computer, and want to get a file from your account on Euler, you might type:

        ftp euler
          (Euler will ask you to log in:)
          Now you might want to move to a subdirectory on Euler.
        cd euler_directory 
get my_file.txt
To copy a file from the lab to Euler, use the put command instead of get instead.

Exercise: We will use an anonymous ftp server, that is, a computer system that allows anyone to log in. This server has the address Instead of a user name, type in anonymous. The server will ask you to type in your email address as the password. Typically, such a server will stick you in a directory containing a subdirectory called pub, and the first thing you should do is change to the pub subdirectory, using the cd command. Now use the ls to see what's available at this level. We want to move from here to the sminpack subdirectory, which should show up on your listing. Once you're there, use the get command to retrieve the file fdjac1.f, and then quit from ftp.


Some of our classwork will require that we use Netscape in order to see certain web pages, or to copy files. Let's just make sure we can do that on these machines.

Exercise: start the Netscape program by typing:

Inside of the program, specify my home page:
and on the home page, select the highlighted item Math 2070. Now save a copy of this file by going up to the File menu, and selecting Save or Save As.... Then exit the program. Use the ls command to verify that you have a new file in your current directory. Try examining it with xedit.


A compiler converts text files of C or FORTRAN source code into new executable programs for finding roots of polynomials or solving differential equations or whatever you want. The text files, of course, are created with an editor. Once you have the necessary files written, there are two steps to creating an executable program: compiling, and linking.

Compiling is the process of checking a program for language errors, and then translating the source code file into an object library format. Typically, a new file is created, with an extension of ".o".

Linking or loading is the process of taking one or more object files, and putting together a complete machine language executable program. This requires, among other things, that there be a "main program" or starting point, and that any built in functions such as abs or cos be copied into the program. Unless you specify otherwise, the resulting executable program is called a.out

Once you have your a.out file, you should rename it to something more appropriate, such as zero_finder. Then you can run the program simply by typing its name.

Compiling C

Here is a tiny C program. Move to your c subdirectory and create a file called sample.c containing this information.

        #include <stdlib.h>
        #include <stdio.h>
        int main ( void ) {
          int i;
          float w;
          float x;
          w = 10.0;
          x = 1.0;
          for ( i = 0; i < 6; i++ ) {
            printf ( "X = %f  X^2 = %f\n", x, x * x );
            x = 0.5 * ( x + w / x );
          return 0;

Exercise: Compile and run the program. We will use a one-step version of the compile-and-load operation, because our program is so simple:

cc sample.c
(On some machines, the compiler might be called gcc instead.) This creates a program called a.out which we should rename:
mv a.out sample
and now we can run it by typing:
(That's "dot-slash-sample", all one word!)

Compiling FORTRAN

To find out which FORTRAN compiler we have, we need to use the which command. Type

which f77
which f90
If which can't find a program, it says command not found. I presume it will find one of these two compilers, and that's what we'll have to use for the rest of the exercise.

WARNING! If you will be using f77, then every line of the following file must begin with 6 blank spaces!.

Here is a sample FORTRAN program. Move to your fortran subdirectory and create a file containing this information. If we are using the f77 compiler, call the file sample.f; if we are using f90, call it sample.f90:

        program main
        integer i
        real w
        real x
        w = 10.0
        x = 1.0
        do i = 0, 5
          write ( *, * ) 'X = ', x, '  X^2 = ', x * x
          x = 0.5 * ( x + w / x )
        end do

Exercise: Compile and run the program. Our one-step version of the compile-and-load command is either:

f77 sample.f
f90 sample.f90
This creates a program called a.out which we should rename:
mv a.out sample
which can then be run by typing

Back to the MATH2070 page.

Last revised on 31 August 1999.