CSCS Home Page UM Home Page



research > lab > docs > unix



A Very Short Introduction to Unix, emacs, make
and a simple C program.

For an introduction to using the CSCS computers, please be sure
 to first read:

   www.cscs.umich.edu/lab/documentation/introduction.html

That includes basic information on:
  - the computers in the CSCS lab
  - logging in from remote machines
  - changing your passwd
  - Your login shell
  - file backups
  - large volume scratch space for users
  - checking your e-mail on CSCS machines
This document assumes you have read the material on that page!

You also should bookmark this page:
   www.cscs.umich.edu/lab/index.html
the top page for all CSCS lab documentation.

Some Useful Unix Sites:
   www.isu.edu/departments/comcom/unix/workshop/unixindex.html
             (a basic UNIX tutorial)
   www.ee.surrey.ac.uk/Teaching/Unix/
             (a Unix tutorial for Beginners)
Or you can check out some information on Linux at:
   jgo.local.net/LinuxGuide/    (Josh's Linux Guide)

IMPORTANT NOTE

When you are done at the console of a CSCS machine, remember to logoff!! - To do that from a Linux console in the Lab: Click on the Gnome panel main menu button (the RedHat button) and select the log out option. Then select yes to logout. Also, you can just click on the logout button on the Gnome panel (the monitor with stars and a moon button). On any machine, be sure the Login Screen comes back up before you leave...then you can be sure you have logged off.

NEVER TURN THE COMPUTERS OFF

Thus the rest of this introduction assumes you are logged into a linux machine, either directly or via ssh from another computer (in the lab or from outside the lab). For more information about logging in from remote machines, see this page: cscs.umich.edu/lab/documentation/introduction.html

General Note: Don't worry, be Happy!

Its very hard for you to "crash" a unix workstation. If your program "crashes", usually it will just stop and produce a "core dump" file. If this happens, please be sure to remove that file: rm core as they are big and waste space but aren't very useful. One other "bad thing" your program might do is go into an infinite loop. If it does, you can either a) press Control-C to stop it b) use the linux "kill" command to stop it. Each process (eg run of a program) has a unique integer ID assigned to it. You can find out what jobs you have running by entering (in another window, eg): ps -ef | grep yourLogin (substituting your login for "yourLogin"). You will see a series of lines, one for each "process". The second column from the left is the process number and the far right of the line has the command used to start the process. So assume the process you want to stop is 12345. To stop that job, enter: kill 12345 or if that doesn't do it: kill -9 12345

Logging in and out.

From other unix machines, or machines with a ssh client:

  ssh formica.physics.lsa.umich.edu

Note that you CANNOT backspace to make corrections in your
login or password.  You will have to do it right or start over.

To logout from a remote connect, type 
   exit

  NOTE
You must use ssh (SecureShell) to make connections to CSCS machines.
You can get this free program on any computer; its already on
most linux/unix computers.   For information on how to get it, see:
   /lab/security.html
When ssh is available on a unix machine,
you can connect to the CSCS machine "dolce" by
   ssh -l yourLogin dolce.physics.lsa.umich.edu
where yourLogin is your login on the CSCS machines.
(If its the same as the login you are entering ssh from,
then you can skip the [-l yourLogin] .

Once you are logged into a CSCS machine in the Lab, one way to get
"terminal" windows (windows into which you can type commands) is to:
   - move the mouse into the background area on the screen
   - hold down MB3 (usually the right button)
   - select "New Terminal"

Another way to get a terminal, is to poke the red hat in the bottom
left corner or your screen, then poke the menu titled CSCS, then poke 
the "x-term" icon on that menu.

By poke we mean "press the left MouseButton".
Note you have 3-button mice.  The function of the various
buttons depends on the program the poke is directed towards.
In general, the left mouse button (MB1) is the "basic"
button for poking icons, menus, etc.

Note that its useful to have several xterms open
to the same machine, e.g., so you can run a program in
one window, and use "top" or other commands to monitor
the program's execution in another.

Note again: you can use ssh to login to CSCS machines
from any computer that has ssh on it...CAEN computers do,
and you can get a copy for free if need be. See
  www.cscs.umich.edu/lab/security.html
for more information.


Directories

Linux/unix uses a tree/hierarchy/folder structure, just as you find under Windows or the MacOS. A directory is the same as a folder. But Case Matters! The front-slash ("/") separates directory names in linux/unix (as does the backslash in Windows). Each user has his/her own home directory (folder), within which all of his/her subdirectories and files will be storee. Your home directory will be something like: /users/yourLogin where yourLogin is your uniqname/login name. This: ~joe is short for "the home directory of the person with login joe". So for example, ls -la ~yourLogin will list all the files in yourLogin's home directory. In each xterm window, you are entering commands to a "command shell" which processes those inputs and decides what program to run, command to execute, etc. In each xterm window you are "in" a "working directory". To see where you are, use the print working directory command: pwd To see what is in the current working directory, use the list command: ls -l -a where the -l (el) and -a are parameters controlling the behavior of the ls command: -l list in long format -a list all files (including dot-starting files) Another way to see what is in your current working directory: ls -l -a . The . (dot) is optional, and just means "in the current working directory". To see what is in the directory "above" (containing) the current working directory: ls -l .. The .. (dot dot) means "in the directory above the current directory" i.e., the directory in which the current working directory is located. When you enter commands that require an input or output file or directory, if you don't specify one, the commands will often just assume you mean the current working directory. Also, you can refer to files in other directories by specifying their location relative to the current working directory. Thus the example above ls -l -a .. refers to the directory above the current working directory, and ls -l -a ../someOtherDirectory refers to a directory called "someOtherDirectory" which is located in the same directory (folder) as the current working directory. You can also refer to files and directories by specifying "absolute paths"...see the tutorials referenced above for more details on directories, relative paths, absolute paths, and related topics. When you do "ls -la" in your home directory, you will see a lot of files that begin with . (dot). Those dot-files in your home directory contain information to automatically configure various programs when you start them, e.g.: .bashrc --- config commands when you start a new "xterm" (approximately...) using the bash shell -- the default .login --- commands done when you login .emacs --- startup commands for emacs In general, you won't need to change/look at these until you are a more advanced unix user. **** NOTE WELL: There is not TrashCan from which removed files can be **** recovered. When its gone, its gone!! **** Well, that's not exactly true--there are backups made **** at certain times of the day and week. Please read **** www.cscs.umich.edu/lab/documentation/introduction.html#FileBackups Make and remove a directory: mkdir TestC1 rmdir TestC1 Make it again, go there, see there is nothing there: mkdir TestC1 cd TestC1 ls -la To go back "up" to your working directory: cd .. (.. always refers to directory "above" where you are) pwd to see where you are ls to see what is there And then back down to the TestDemo cd TestC1 ls -l .. to see what is in the directory above where you are ls -l to see what is in directory where you are (TestC1) ls -l ../TestC1 hard way to see what is in TestC1

Creating a simple C program

Basically a C program is created as follows: - use an editor to type in the "source" form of the program, i.e., the program written in the human-readable C language. - use a compiler to compile the C program files into "object" files. These are files in a machine readable format, which contain the various parts of the program, but not ready to run. - use a linker to combine the object files, along with standard C library files, to create an "executable" (i.e., run-able) program. In this example, we will first just copy the C source code files into a directory in your file space, to save you having to type it in. Then we will use the "make" command to start the compiler and linker to create the program. Then you will have a program you can run in your own directory. And you will have the source for that program, so you can change the program, re-compile/link it, and get it to do different things when run. So, to start, if you haven't done so (see above), create a directory TestC1 and cd into it. Then copy (cp) the provided source files in to your directory: cp /users/rlr/Courses/IntroProg/C1/*.h . You could also type in the following commands in full, or you can use command recall and editing to make it go faster. After entering the above command, then press the up-arrow key (not the one in the keypad). You should see the above command appear. Then you can use the other arrow keys, the backspace key, etc, to replace *.h with *.c, then just press enter again when your command is ready: cp /users/rlr/Courses/IntroProg/C1/*.c . And repeat for this command. cp /users/rlr/Courses/IntroProg/C1/Makefile . Command recall editing can save a lot of keystrokes, especially if you are a sloppy typist (like me!). See what is there: ls -l See all files that match "any name ending in .c" ls -l *.c To see what is in an (ascii text) file: cat c1.c See the "less" command for a better way to do this. Or use emacs to edit the file to see what's in it. Ask make to use Makefile to compile and link the c1 program: make You may see something like this happen: dolce-TestC1$ make gcc -o c1.o -c c1.c gcc -o c1 c1.o -lm dolce-TestC1$ Note the only thing you type is make The dolce-TestC1$ is the prompt for you to enter a command. Note the part of the prompt before the - is the machine you are logged into, and the part after is the working directory you are in, i.e., the directory displayed if enter the pwd command (pwd = print working directory). Lets see all the *.o files, and the c1 runnable ls -l Note c1 file permissions have "x" (executable). To run your program: ./c1 That means "run the program in the file named c1 in the current working directory". You should see some simple output printed in your xterm window. You can now look in the source files to see what the C program looks like, using any editor you want. However, to edit files in Linux/Unix, I recommand emacs. To edit the file c1.c, enter: emacs c1.c & There is a tutorial in Emacs (from a pull down menu), or get an introduction to Emacs book. (Or see the MediaUnion notes mentioned above...) To edit two files at once, you can use the File->OpenFile menu item, or you can press F7 (top of keyboard on the computers in the lab). In either case, you will be prompted for a filename to open. Enter c1.h and press return. To flip back and forth between the files, you can use the Buffers menu item, or you can press (at same time) -Mouse1 (usually left button) A list of open files will appear: slide pointer to the one you want and release. There are also some special functions defined for some of the "Function" keys on the keyboard. These are defined in your .emacs file (see above) on the CSCS computers. A couple of other useful keys: F1 - open new file in new window F2 - open buffer in new window F7 - open new file in current buffer Again, see various emacs docs for zillions of other commands and shortcuts. Next, make a copy of that c1.c file: cp c1.c c1-orig.c Then change in c1.c aLocalDouble = 40.01; to aLocalDoble = 40.01; Save the file, via the menu File->Save, or ^X^S To "make" (compile and link) the program again: make You will see some errors. To see what has changed: diff c1.c c1-orig.c Fix the errors in the emacs window, type make You will still see the errors (unless you saved the c1.c file)! Save it, do a make, and it should be aok. Do another make...it won't do anything...its "up to date". Exit emacs. To leave emacs, File->ExitEmacs or ^X^C Now start emacs again: emacs c1.h & Put an error in the c1.h file, by changing it to: double aGlobalDouble; x Now in emacs window, -x compile On the CSCS keyboards, is the key next to the spacebar. So the above involves pressing the and x key at the same time, then in the Emacs command buffer (at the bottom of the window with the file you are editing) typing "compile" and pressing the while in that command buffer. It will ask you for a compile command, the default being "make" so just press return-key again. It will ask you if you want to save the c1.h file...type "yes". It will then put up a new window, in which the make command, and so the gcc compile command, is executed. You will get a strange error, not obviously related to the error you put in the c1.h file!! Press the middle mouse button on the line in the *compilation* window: c1.c:10: syntax error before `void' A new window will come up, for the c1.c file, and the pointer will be right on the line with the (alleged) error. On the CSCS machines, a short way to start the Make process in emacs is to press SHIFT and the "9" key in the keypad area (on the right of keyboard). Now we know there is no error here, as we didn't change it. Typically if you get a really off-the-wall error message like this, start looking backwards in the file. In this case the next line is #include "c1.h" so just go to that file and "look" for the error. Correct it, press the "9" key in the keypad area. That should do the make in emacs again. To leave emacs, File->ExitEmacs or ^X^C

I (rlr) don't recommend it, but Josh does:

A simple editor to try  is the text editor "gedit", which is
accessible via "RedHat Menu -> Accessories" -> "Text Editor" .
It has anti-aliased fonts, and other nice features.
But it does NOT have syntax color highlighting, or the ability
to make and debug programs within the editor (as you can in emacs).

To rename files, use the mv (move) command:
   touch xxx       <- this just creates an empty file named "xxx"
   ls -l
   mv xxx yyy
   ls -l 

To remove files, enter "rm filename"
   rm yyy

To remove a directory and all the files in
directories in it:
   rm -r someDirectoryName

BUT BE CAREFUL:  as noted above---there is no trashcan!! 
It takes extra work to recover files, and that can
only be done if the file is saved to our weekly filesave tapes.

The "man" command can give you details about any command:
   man ps
   man ls
   man less            "less" is a good way to read text files
   man who             
   man top
   man grep

See who else is logged it:      who
How busy is the machine:        top    (Control-C to quit top)
How busy are all the machines:  qhost
  (rough estimates...)

Sometimes its useful to see what command is being executed
when you type in a command/program, e.g., when there are multiple
versions with the same name, in different directories in your PATH
(the list of directories unix looks through to find commands you enter).
   which make
will show you are using the version of make in /usr/bin .
Sometimes when you run a command (or a program of your own)
and it doesn't do what you expect, it can be because its really
running some other version!

grep (general regular expression parser, I think) is very useful
for searching files for strings.  For example, suppose you want
to find all occurences of some variable name in your
*.c and *.h files:
    grep  -n totalHits *.h *.c
The -n tells it to print the filename and line number along with
each line that has "totalHits" in it.
You can search for various "regular expression" patterns:
    grep -n "person*Total" *.h *.c
will find "personGrandTotal, personSubTotal, and so on.

In unix there is a concept called a "pipe", for piping data
from the output of one program directly into the input of another.
An example of this was shown above:
   ps -ef | grep yourLogin
(Again, subsitute your login for "yourLogin".)
The veritcal-bar character (|) is the pipe from the "ps -ef"
command to the "grep yourLogin" command.
To see the output of the ps command, just enter
   ps -ef
All that output goes into the "grep" command, which in
effect acts like a filter, printing out only those input lines
that have the string "yourLogin" in them.
Many unix commands are written so they can be strung together
in pipelines.  See any introduction to unix for examples.

We have three postscript printers in the CSCS lab, but by
default all printing is done on the duplex (two-sided)
black and white printer named 'calamaro'.  To print postscript 
files:
  lpr afile.ps

To list program files, or other plain text, I find 
   enscript -2rG  c2.c
useful, as it puts info at the top, and prints two-pages side
by side (and so saves a few trees).
Enter "man enscript" for more information about its parameters.

See the page:
   www.cscs.umich.edu/lab/documentation/printing.html
for more information about printing in the CSCS lab.

Remember, if you login from some remote site, and you enter
a print command (lpr or enscript or whatever) in a session
running on a CSCS computer, it will print the the CSCS lab,
NOT on the printer at your remote site!!
You have to transfer the file from the CSCS computer to
the local computer to print on that local printer.
(For unix-unix, use the ftp command. On other computers there
are similar methods, eg, Fetch on macs.)

How to save versions of your programs now and again: 
   the tar (tape archive) command.

Go to the TestC1 directory
   cd TestC1
Save the Makefile and *.c and *.h files in named/dated file: 
   tar cf c1-980108.tar Makefile *.h *.c     
See what is in a tar file:
   tar vf c11-980108.tar
Recover all the files from a tar file:
   tar xf c1-980108.tar
Recover one file from a tar file:
   tar xf c1-980108.tar demo1.c

To compress and uncompress a tar (or any) file:
   ls -l
   gzip c1-980108.tar 
   ls -l
   gunzip c1-980108.tar 
   ls -l

You can also do the tar and compress/decompress in one command:
   tar zcf c1-980108.tar Makefile *.h *.c  
or to uncompress and untar:
   tar zxf c1-980108.tar.gz

Sometimes a Makefile will have commands built in
to do a create a dated tar file for you.
To use use the commands in your Makefile to do the
saves, with date automatically provided:
   make save
   ls -l Saves

NOTE: Its a good idea to save your programs source files, notes, etc,
      regularly (eg after you work hard all day!).
      Even safer is to then copy (eg via ftp) those files to some
      other computer system.

To copy an entire directory:
   cd          (cd to your home dir)
   cp -r TestC1 TestC1-x
   ls -l
To rename a directory
   mv TestC1-x TestC1-y
   ls -l
To remove it (***be careful***)
   rm -r TestC1-y

Unix, X11 and Swarm GUI's (Graphical User Interfaces)

If you are accessing the CSCS machines from a remote machine
using ssh, you may also need to set the DISPLAY enviroment variable to
point to your local machine, so that programs that use X11 GUI displays
will know to put displays on that remote machine, not on the console
of the machine you log into.  (Programs that use the X11 protocol include
mathematica, emacs (it can go without X11, too), Swarm, and many others.)
Assume your other machine is yourmachine.umich.edu,
and you are logged into the CSCS machine formica.physics.lsa.umich.edu .
Then do this:

1. On a session at yourmachine.umich.edu :
     xhost +formica.physics.lsa.umich.edu
2. On the session running on the CSCS machine (formica):
   If your command shell is bash (the default), use:                       
       export DISPLAY=yourmachine.umich.edu:0.0
   If your command shell is tcsh, use:
       setenv DISPLAY "yourmachine.umich.edu:0.0"

Note that you may not need to do this if you use ssh to access the 
CSCS computers.  (However, depending on the version of ssh,
you may still need to do these commands to set the DISPLAY variable.)

You can tell that your DISPLAY is not set properly if you run
some program that tries to create a new window (eg netscape,
or emacs in the background), and after nothing happens, if you press
the "Enter" (Carriage Retunr) key again, you get a message like this:

  pulcina-rlr)emacs xxx &
  [1] 18221
  pulcina-rlr)
  [1]  + Suspended (tty output)        emacs xxx

The emacs job is "suspended" because it can't start a new window
on your screen, but it must do that to exit the file "xxx".
To unsuspend the job, type the "%" key and press Enter:

  pulcina-rlr)%
  emacs xxx

In this case, emacs will start editing that file in that eterm window,
rather than in a new one.   Other programs (e.g., netscape) will
print an error message and not start, e.g.

  pulcina-rlr)netscape 
  Error: Can't open display: 
  pulcina-rlr)

If you get any messages like these, use the export/xhost commands
as described above, then retry the command that failed.

Console logins in the CSCS Lab

On a Linux machine:

After you enter your login and password in the basic login screen, 
an xterm window may or may not come up.  If one doesn't,
to get one do the following (where Click => press left mouse button):

     At the bottom of the screen, a bit to the left of 
     center, is an icon that looks like a monitor.
     Click on that.

One way or another, an "xterm" windown should come
up, with a prompt something like

  formica-yourLogin)

Click in that window.  Then you can type in that window.
You can use the left-mouse button to grab and edge
and change its size and shape.
You can use the left-mouse to grab the top bar in
the window and move it around.  (Or press the
ALT-key at the same time as you press the left-mouse button
and drag the window from any point in the window.)

To read your mail, you must telnet to the machine
that has your mail reader.  So at the prompt enter

   ssh  -l yourMailLogin yourmachine.yourdomain.edu

if your mail reading machine supports ssh ("secure shell").
In any case, log in there as normal.

IMPORTANT NOTE

When you are done at the CSCS machine, remember to logoff!!
***\                                      /***
**** BUT DO NOT TURN THE COMPUTER OFF ****
***/                             



Updated September 1, 2005