Minimalist UNIX

MINIMALIST UNIX 2.2:
A Survival Guide for UNIX Users at Virginia Tech

These notes are intended to provide sufficient information for a student to produce programs under ULTRIX on the DECstation and OSF/1 on the DEC Alpha. They are primarily designed for non-CS majors who will be using the lab machines run by the CS department. It is assumed that the user has available a UNIX machine that is booted and ready to log onto. Note that a couple of the commands (such as sash and pc) are not standard UNIX commands.

Logging On and Off

A UNIX machine that is ready to accept a user will have the prompt ``login:'' on the screen. To log on, simply type the name of the account that you wish to use. For lab machines, the proper account name is ``guest''. Some accounts will then ask for a password. If you are using an account with a password, type the password when the prompt is given - for security, your password will not appear on the screen as you type it. The ``guest'' account on the lab machines has no password. (On the DECstation, you must hit ``Return'' at the password prompt.) At this point, UNIX will go through its login routine, and probably type some messages to the screen. Eventually it will provide a prompt that may look something like:

usrusersguest>

Whenever you see the prompt, UNIX is ready for a new command.

To log out of a DECstation or Alpha, hold down the right mouse button when the cursor is in the grey crosshatched part of the screen. Select the ``QUIT'' option. When using a lab machine, it is polite to log off so that the next user will realize that the machine is free. NEVER TURN OFF A LAB MACHINE THAT IS RUNNING UNIX unless instructed to do so by the lab staff!

Directories Under Unix

Every file in a UNIX system is located in a hierarchical directory system. Directories may contain both files and subdirectories. The highest level of the hierarchy is referred to as ``/''. The things in directory ``/'' have names like ``/usr'' and ``/CS''. ``/usr'' contains subdirectories with names such as ``/usr/bin''. Every time you see the symbol ``/'' in a file or directory name, it indicates that the file is one more directory lower in the file system hierarchy. A name which starts with ``/'' is called a path name since it describes the complete path from the top of the file structure ``/'' (which is called the root).

At any time you will be ``in'' some directory, called the working directory. On the DECstation, you will usually be working in either the subdirectory called ``/usr/users/guest'' (which is the home directory of the account named ``guest'' and the directory that you will be in when you log on) or the subdirectory called ``/sony'' (which is where you will access the contents of both DECstation and Alpha diskettes). You can find the name of the directory that you are currently in with the command pwd (which you can remember as ``present working directory'').

If you wish to refer to a file or directory name, it is not always necessary to give the complete path name. You can also give a name relative to the directory that you are currently in. Whenever you give the name for a file or directory, and the name that you give does not start with ``/'', it is assumed that what you are looking for is relative to the current directory. Thus, if your current directory is ``/usr/users/guest'' and you type ``foo'' for a name, UNIX will assume that you mean ``/usr/users/guest/foo''. Naturally, if the file ``foo'' actually resides on ``/sony'', then UNIX will not be able to find it in this example. You can change the working directory with the command cd (which you can remember as ``change directory''). So if you log on as ``guest'' with working directory ``/usr/users/guest'' and then mount a diskette, you can type ``cd /sony'' to make the working directory be the one containing the files on the diskette. If you type ``cd'' with no directory listed, then you will return to the home directory that you log into.

There are various shorthand ways to give names relative to the current directory. ``.'' means the current directory. ``..'' means the parent directory. Thus, if the current directory is ``/usr/users/guest'', then ``./foo'' is the same as ``/usr/users/guest/foo'' and ``../foo'' is the same as ``/usr/users/foo''.

The command mkdir will make a subdirectory under the current directory. This may be useful if you wish to organize your diskettes. Type ``mkdir foo'' to create a subdirectory (in the present working directory) named ``foo''. ``rmdir foo'' will remove a subdirectory, but all files must first have been removed from it.

The ls command will list the names of the files and subdirectories contained in the current directory. ls, like many other commands, can take a list of file or directory names as parameters. In this case, ls will return information only on the names listed. To make typing easier, UNIX will use the symbol ``*'' to mean ``match anything''. Thus, if you type ``ls a*.c'', this will be the same as having typed the names of all files or subdirectories in the directory that start with ``a'' and end with ``.c''. ls, like many other commands, can also take options that modify how the program works. Typing ``ls -l foo'' will give you more information about the file ``foo'' than typing ``ls foo''.

Executing Commands and Programs

Under UNIX, commands are executed by typing their name along with any parameters, followed by the ``return'' key. Most system commands are actually a special type of file. However, when your account is set up properly, you do not need to type the complete path name even though these commands are not in your working directory. In the same way, to execute one of your programs, you simply need to type its name. Note that names under UNIX are case sensitive, i.e., ``foo'' is not the same as ``Foo''.

Very often when writing programs for class, your instructor will require that you turn in a listing of the program along with the results of running the program. Any program that writes to the screen is normally writing to something called standard output. If you have a program (or any system command) that sends output to the screen, you can redirect this output to a file by using the ``>'' symbol. For example, if your program is named ``foo'', then typing ``foo > file'' will send the output from ``foo'' to the file named ``file''. In the same way, you can store the file list given by the ls command into a new file named ``outfile'' by typing ``ls > outfile''.

Many programs read data from the standard input. Normally, this means that they are requesting input from the keyboard. To make a program read from a file instead, use the ``<'' command. For example, to make the program ``foo'' read from file ``infile'' and write to file ``outfile'', type ``foo <infile >outfile''. It does not matter what order you put ``foo'' ``<infile'' and ``>outfile''.

If you place ``&'' at the end of a command line, then that command will be executed in background mode. In other words, it will be executed simultaneously with any other work that you wish to do. After typing a line ending in ``&'', you can go about your business and ignore the command until it is complete. UNIX will let you know when the command is finished. If you type the command fg, then your command will be returned to the foreground, which means that it will execute as though you had originally typed it without ``&'' at the end. Note that commands running in the background must have their input read from a file, and you probably also want to redirect the output to a file.

When typing and executing commands, certain keys will have a special effect. While typing a command, the ``delete'' key (which looks something like ``<X|'' on the DECstation keyboard) will back up one character, allowing you to make corrections. Typing ``cntl-c'' will let you start the command line over. If you type ``cntl-c'' while a command is executing, that command will be aborted. Alternatively, if you type ``cntl-z'', then the command will be frozen. At this point you should get a prompt, and you can type commands to UNIX. While the command is frozen, you can send it to the background by typing the command bg, or let it re-start by typing the command fg. Typing ``cntl-d'' tells most programs reading from the keyboard that they have reached the end of input.

Diskettes

The command sash allows you to manipulate diskettes (along with a few other housekeeping chores that on standard UNIX systems are privileged to the root account). Simply type ``sash'' and several options will be described. Then type the name of the desired option. If your diskette has not been formatted, there is a menu option to format it. Diskettes are normally only formatted once. Note that DECstations and Alphas use the same type of 3 1/2 inch diskette. A diskette formatted for one machine will work on the other.

Once a diskette is formatted, it must be mounted each time it is to be used. This is done with another option on the sash command. After mounting, the files on the diskette will be available under the ``/sony'' directory. When you are finished using the diskette, you may also unmount it with sash. Note that you must eject the diskette by pushing the button near the diskette drive. Never eject a mounted diskette without unmounting it first - otherwise you may lose your diskette files!

When using a lab machine, you should do all of your class work in the ``/sony'' directory rather than in the ``guest'' home directory. In other words, always work directly on your diskette. This will minimize the chances of your forgetting to copy the updated version of your work to your diskette when you are finished, and will also keep other students from copying your classwork. One special point to remember is that you cannot unmount the diskette if your present working directory is on ``/sony''. If you try to unmount the diskette and get a response like ``mount device busy'', then you should use cd to move back to your home directory.

If - oh no! - you do eject your diskette without unmounting it first, you are usually OK if you put it back into the drive immediately. You are also OK if you made no changes to the diskette (i.e., did not write to, edit, create, or delete any files on the diskette).

Someday you probably will end up ejecting a diskette without unmounting it first - either through accident or because the machine crashed with your diskette still mounted. In this case it is quite possible that the file system on the diskette is damaged so that it is not ever again reliable until it is fixed. The file system is fixed by doing the following. First, if there are important files on the diskette, try to mount the diskette and copy the files somewhere else for safekeeping. Be warned that these files may be corrupted if the file system has been damaged. Next, use the sash ``check'' option to fix the file system. Answer ``y'' to any questions that the program asks. After this procedure, your diskette should be OK to use (you can then mount it with the sash command). It is possible that you will lose files from the diskette during this procedure (but only if the file system was damaged in the first place). That is the reason to try and copy files off the diskette before running sash's ``check'' option.

Editing Files

There are two screen editors available on the lab machines. The standard UNIX screen editor is called vi. To start it editing file ``foo'', simply type ``vi foo''. Another editor that you can use is called emacs. There are short handouts available from the lab giving brief command summaries for each of these editors. In addition, the emacs editor provides extensive on-line information to help users learn it, including an on-line tutorial. To start emacs editing file ``foo'', just type ``emacs foo'', or if you want to reach the on-line information just type ``emacs''.

Manipulating Files

Files may be copied with the cp command. Type ``cp name1 name2'' to copy the file ``name1'' to file ``name2''. Type ``cp name1 dirname'' to copy the file ``name1'' to the same name within directory ``dirname''.

Files may be renamed or ``moved'' with the mv command. File and directory names are used just like for cp. Note that you cannot mv a file between your login directory and the directory ``/sony'' - you must cp the file instead.

The standard UNIX command to delete a file is called rm. To delete file ``foo'', type ``rm foo''. As with ls, you can use the ``*'' filename construct to delete all files matching a certain pattern. For example, ``rm a*'' will delete all files whose names begin with ``a''. rm will list the names of each such file, giving you the option to delete it or not. Be very careful how you use ``*'' with rm. For example, there is a big difference between ``rm a *'' and ``rm a*''. The first example will delete all of your files in the current directory!

You can print a file to the screen with the cat command. This is often unsatisfactory since the file may be longer than one screen in size. The more command will let you see the file one screenfull at a time. Type the space bar to see the next screen, or the return key to see the next line.

To make a hard-copy listing of file ``foo'' from a lab machine, just type ``lpr foo''. The file should then appear at the attached printer.

Compiling Programs

The name of the ``C'' compiler under UNIX is cc. It is good practice to name all ``C'' programs with ``.c'' at the end. To compile a ``C'' program in a file named ``foo.c'', the simplest thing that you can do is type ``cc foo.c''. This will create the compiled version of the program in a file called ``a.out''. You probably want to give your own name to the compiled version of the program. To do so, type ``cc foo.c -o foo'' to name the result ``foo''. Before you compile a ``C'' program, you might want to perform a preliminary error check with a program named lint. Lint will give you a better description of your syntax errors than does cc, and also catches some simple logic errors. Type ``lint foo.c'' to run lint on file ``foo.c''.

The name of the Pascal compiler is pc. All Pascal program files should end in either ``.p'' or ``.pas''. pc has command-line options very similar to those for cc described above. See the lab handouts on pc for more command options.

Getting More Information

UNIX contains extensive on-line documentation about its commands. The primary way of obtaining information about a command is by the man command. For example, type ``man cp'' for more information about the cp command. Most of the commands described in these notes can do more things than described. It is a good idea for you to read the manual sections for these commands by using man.

If you think that UNIX ought to have a command to do something not describe here, there is a good chance that the command really does exist. The command apropos will take a keyword and search an online database to suggest commands that pertain to that keyword. You can then use man to look at the relevant manual sections.

Note that some of the commands described in these notes are not separate programs at all, but are part of a bigger program called the shell. The shell is the user interface and command interpreter that you really communicate with whenever you are logged on. These commands (for example: cd, fg, bg) do not have separate manual sections because they are part of the shell. For more information on the shell, type ``man csh''. The ``csh'' manual will also provide more information about redirecting files and a host of other useful features.

Complete UNIX manuals are also available in the lab.

Miscellaneous Information

Class data files will appear in ``/CS/<classname>'' where <classname> is the name of your CS class. If you are familiar with anonymous FTP, you may also access class data files from the machine named ``cslab.cs.vt.edu''.

These notes are still evolving, and will be updated from time to time. If you have any suggestions for how they may be improved, please tell the lab staff or bring it to the attention of Dr. Shaffer.