COSC 065

UNIX Tutorial


The UNIX operating system was developed in the 1960's and 1970's at AT&T's Bell Laboratories, one of the leading centers for Computer Science research at the time. UNIX was designed to be an efficient, multi-tasking, multi-user operating system. The elegance of its design, along with its highly portable and widely available source code, gave UNIX an early foothold in academia, as well as many commercial arenas.

Today, many descendants of UNIX (Linux, Solaris, MacOS X, *BSD, etc.) continue to play a vital role in the global Internet, financial infrastructure, Universities, and even on people's desktops.

As Computer Scientists, we continue to use and study UNIX-like systems for many reasons. Chief among them is the elegance of the UNIX design, although it may take some time and more courses for novices to see this. Servers in the MSCS department allow us to perform tasks that would not be practical or affordable with other, more commercially popular operating systems. More importantly, (and unlike Microsoft Windows and MacOS X specifically,) UNIX was not designed to hide what is going on from the average user. The UNIX philosophy allows for greater power and flexability for advanced users, but also presents more of a learning curve for the uninitiated.

This tutorial is designed to give you a head start on the journey.

Terminals and Shells

In the olden days, computers were massive, centralized machines that occupied much or all of a single room. Multiple users could interact with the single computer via terminals, essentially a teletype monitor and a keyboard connected to the computer via serial cable. Users typed text commands at a command prompt, and the computer responded in kind with plain text answers. Despite the advent of powerful graphical user interfaces and windowing systems, the power, portability and simplicity of the terminal interface survives. It is important to be a competent UNIX terminal user, because you can always fall back to this when other, more modern interfaces won't do what you need.

The simplest way to open a terminal on an MSCS machine is to use a Secure Shell client (ssh) to connect remotely to a UNIX host.

Alternatively, if you are logged into a graphical user interface (either at the console of a UNIX workstation, or remotely using the X-Win32 client on a Windows machine,) you can start up a terminal window from the File Manager through the File/Open Terminal menu option.

A command shell is a program that allows a user on a terminal to interact with the operating system. The default shell in most UNIX environments is called "csh" (pronounced C shell). More user friendly shells that allow command line editing and command line completion are "tcsh" (pronounced, "T C shell") or "bash" (pronounced, "bash"). The default shell for MSCS Solaris systems (like Pascal) is bash, which should be fine. If you find yourself logging into a system for which the default shell is not one you are comfortable with, you can always switch to bash by executing the command "bash", or "/bin/bash", depending on your default path.

Simple UNIX commands

The most common UNIX command is "ls".  Type:

> ls
The command "ls" with no arguments lists the contents of the current directory. If you want to see the details of the files type:
> ls -al
The "-a" tells the ls command to display also the files that are normally hidden, those that start with a dot ("."). The "-l" option tells the ls command to display file attributes like who owns the file, the size of the file, and the permissions associated with it.

The command "pwd" prints the directory path of the current directory. Type:

> pwd
The directory path printed is a list of the directories necessary to reach the current directory from the root directory.

To create a new directory (for example the cosc065 directory) type:

> mkdir cosc065
> ls
To change the current directory to the cosc065/ directory type:
> cd cosc065
> ls
You will notice there are no files in there. Now type:
> ls -al
You will see two files: "." and ".." One dot means the current directory and ".." means the parent directory.

To change the current directory to the parent directory type:

> cd ..
The home-directory is the directory where all your files are stored and is where the current-directory points to when you first login. To change the current directory to your home directory type "cd" with no arguments. Type:
> cd
> pwd
> ls
In many departments like MSCS, multiple UNIX servers work together to ensure that the same directory appears as your home directory no matter which machine you login to.

It is strongly recommended that you create and use subdirectories in your home directory to organize your work. It becomes hard to find things once the listing of files in a directory fills more than a screen of your terminal window.

With a modern shell like tcsh or bash, you should be able to use the arrow keys to recall previously entered commands, or to edit the line you are on.

Also, modern shells feature command completion, so that you don't need to type all the characters for a file or command. You may use <tab> and <ctrl-d> to complete the name of a file.

For example, type:

> ls /users/ma<tab>

This will complete to "ls /users/majors/"

If the prefix matches several files, you may see a list of files matched by typing:

> ls /usr/bin/g<ctrl-d>

This will show all the commands in /usr/bin/ that start with 'g'.

You can refer to a list of files by using the wildcard character, "*". The "*" character in a file name represents one or more characters. For example:

> ls /usr/lib/*.a

Lists all the files that end in ".a" in the directory /usr/lib. With power comes responsibility. Be careful with wildcard characters when you are performing potentially dangerous tasks like moving or deleting files.

Here is a summary of the commands you have learned and some more:

Summary of UNIX commands

ls Lists the contents of the current directory
ls -al Lists the contents of the current directory in detail
pwd Prints the path of the current directory
mkdir new-directory Create a new directory
cd directory-path Change to a directory
cd Change to home directory
cp old-file new-file Copy a file from old-file to new-file
mv old-file new-file Rename a file from old-file to new-file
rm file Remove a file
echo "message" Outputs the string "message" 
<up-arrow> Get previous command
<down-arrow> Get next command
<left-arrow>, <right-arrow> Move cursor to the left or right.
<tab> Path-completion

Manual Pages

UNIX systems typically come with extensive documentation for all of their commands. Historically, this documentation was called the manual pages, or man pages for short. Access the man page for a command by typing:

> man <command>
as, for example,
> man ls
describes all of the options and features for the ls command. You can use the space bar to advance the text, and the 'q' key to quit back to the command prompt.

The man command itself has a man page, which can be accessed using

> man man

You can use the "-k" option to the man command to search for man pages containing a given keyword if you don't know the name of the command you are looking for.

Text Editors

There are two mutually-exclusive and hostile schools of thought on text editors in the UNIX environment. Rather than take sides on this most contentious of UNIX religious wars, I will point out that both emacs and vi have advantages and disadvantages. A good computer scientist should be proficient in both, and know which one to pick for a given task. For programming in assembly language, either will suffice.

Emacs is a powerful, extensible text editor based upon the Lisp programming language. To launch emacs,

> emacs <filename>
Emacs often recognizes the type of file you wish to edit by its extension, and customizes itself appropriately.

Beginners can find their way around modern emacs implementations using the graphical menus that appear in the bar at the top of the window. However, emacs's real power derives from its nearly endless list of keystroke shortcuts. Type "emacs tutorial" into Google, and you will get over a million hits. I suggest you try some as you learn emacs. With practice, advanced users find that emacs allows them to program with greater productivity and speed than just about any other text editor on any operating system.

The downside of emacs is that it is a real resource hog. I do NOT recommend that you run emacs when connected from a remote machine; performance can be quite poor. Nor is it a good idea for three dozen users to all run emacs on the same machine; if all of us are doing it at once near the end of an assignment deadline, performance will suck for one and all.

In contrast, the vi text editor is a light-weight, simple tool that is pretty much guaranteed to be available on any modern UNIX system, no matter how primitive. Vi was designed in the days of slow terminal and modem connections, and emphasizes frugal resource usage and austere command interactions. Some find this to be arcane and cryptic; in the hands of an expert, vi can be as powerful a tool as any editor, and with much less bloat than emacs or other alternatives.

The key to using vi is understanding that it has several modes. Primarily of interest to beginners will be command mode and insert mode. When vi first starts up, it is in command mode. In command mode, users can navigate a document using the arrow keys, and execute commands using single character keys. A few examples of commands:

  • 'i' : puts vi into insert mode.
  • 'x' : deletes the character under the cursor.
  • 'a' : moves the cursor to the end of the current word ("append") and enters insert mode.
  • 'A' : moves the cursor to the end of the line, and enters insert mode.
  • 'r' : causes the next character typed to replace the character under the cursor.
  • 'dd' : deletes a line.
  • ':w' : saves file.
  • ':q' : quits vi.
  • ':r' : reads in a file.

    and many more.

    Hitting the 'i' key in command mode enters insert mode. Once in insert mode, characters typed are inserted directly into the file at the cursor location. Hit the 'escape' key to escape from insert mode back to command mode.

    Type "vi tutorial" into Google, and you will get over two million hits.

    On pascal, gvim, an improved vi with a graphical user interface, is also available.

    [ DWB 2005 Oct 12 ]