LAB #1: Introducing UNIX


TABLE OF CONTENTS

Introduction

Although the PC's in the lab use a Microsoft Windows operating system, we will be working on the University's UNIX server. 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. In this lab you will learn:

Logging In to the campus server

When you sit down at a PC in the lab, you are faced with a Microsoft Windows screen. You will need to log in to the campus server (unixs). To do this, you first need to start the "X Server" program, XWin32 by double-clicking on its icon. When this program starts up, a window will cover your whole screen with a grey hatch pattern. Find the tiny box in the far upper left and click on it to get a menu. Choose sessions to get a second level and then choose unixs. This choice should bring up a login screen. Log in using your id and password. You will end up with a window with a "command line" into which you can type commands. The rest of this lab covers what commands you might want to type.

Directories

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:

pwd

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 /afs, where users have files. Each subdirectory can have further subdirectories beneath it. For instance, under the /afs directory there is a subdirectory called /pitt.edu, and under that there are many directories of the form /afs/pitt.edu/usr#/yourid, one for each student or user of the computer. You can, and are encouraged to, organize your work by creating further subdirectories below your home directory.

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:

ls

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.)

Adding and 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). File names in UNIX consist of one or more numbers and letters, and a few special characters like the underscore "_". Uppercase and lowercase letters are different, so the two names myfile.txt and MyFile.txt are different files. It is customary in UNIX, as in Windows, to use the characters after the dot (the extension) to indicate the use of the file. Unlike Windows, however, use of extensions and use of particular extensions is not always required. Common file extensions include: .txt for text files, .c for C language files, .f for Fortran 77 files, .f90 for Fortran 90 files, and .m for Matlab files.

Once we make a few files, we'll probably want to get rid of some of them. We can do this with the rm (remove) 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:

cd

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 afs

and your second command will probably be

cd afs

Now go to the directory /afs/pitt.edu/common/motd/ (this is the directory containing the welcoming message you see when you log in) 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

or

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.

Editors

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 a simpler one, pico.

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

The PICO Editor

pico 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. The pico command without a file name opens an empty window that you can type into and give a name to when you save it, and the pico command with a file name will open up the window with the file in it.

To enter and alter text is very simple. You simply position the cursor using the arrow keys to the desired location and type new text in or use the backspace key to erase text that is there.

Menu options are listed on the bottom of the window and allow you to search, to save the file, to read in a different file, to quit, etc.

Exercise Create the file my_file.txt using pico.

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 (move) 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

or

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

Printing

Unfortunately, you cannot print a file to the printers in the lab. You can print a file to the printer in the Benedum lab using the lp command:

lp -d beh filename

or you can print a file to the printer in the Cathedral with the command

lp -d cl filename

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. If you did not get an error message, your file will show up on the indicated printer. Please drop by and pick it up.

File Transfer

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

        ftp euler
        
          (Euler will ask you to log in:)
        
        
          my_name
          my_password
        
        
          Now you might want to move to a subdirectory on Euler.
        
        cd euler_directory 

        get my_file.txt 

        quit 

      

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 netlib.org. 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.

NETSCAPE

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:

netscape

Warning: DO NOT use the netscape icon on the PC for this task. You are actually running netscape on unixs, but displaying it on your PC screen. If you want to save files, you will have to use unixs. If you only want to surf, you should use the Netscape icon on the PC.

Inside of the program, specify my home page:

http://www.math.pitt.edu/~sussmanm

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 pico.

You can do much the same process to retrieve files from your area on unixs to your PC in your room. Start up Netscape or Internet Explorer and specify the page:

ftp://your_id@unixs.cis.pitt.edu/~your_id

Note that it starts with ftp and not http. Then click on directories until you end up at the file you want to download. Highlight it and choose save link as to get a dialog box asking you what the file name should be on your PC. After you fill in the PC file name, the file will show up.

Compiling

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. Before you can use the compilers on unixs, you must issue the command

setup sunpro

Sometimes you will get an error message from setup:

setup: "setup" is only available in login shells.
setup: plea>

Transfer interrupted!

shell.
setup: type "man setup" for more information.

If this happens, type the command bash --login (that is two dashes before the word "login" and no space between the dashes and the word) and then try the setup command again.

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:

./sample

(That's "dot-slash-sample", all one word!)

Compiling Fortran

It is horrifying but true that you are not provided with a Fortran 90 compiler on unixs. The Fortran 90 language (now ten years old) is much more modern than the Fortran 77 language. How many of you were born when Fortran 77 was new? Sorry.

Here is a sample FORTRAN program. Move to your fortran subdirectory and create a file containing this information. WARNING! Every line of the following file must begin with 6 blank spaces! Call the file sample.f:

        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
        stop
        end
      

Exercise: Compile and run the program. (If you have already used the setup sunpro command, you do not need to repeat it. If you have not done so, see the discussion on compiling and running C above.) Our one-step version of the compile-and-load command is:

f77 sample.f

This creates a program called a.out which we should rename:

mv a.out sample

which can then be run by typing

./sample

Quitting

To end your unixs session, type exit at the command prompt. You may have to type it more than once. If necessary, you can also click on the X box in the far upper right corner of the screen.


Back to the MATH2070 page.

Last revised on August 26, 2000.