Getting to Know the Unix Operating System

When you see a blinking rectangle sitting to the right of something like this:


or perhaps this:


or something similar, then your computer is ready for your session to begin. The text being displayed is called the system prompt -- a message from your computer's operating system (UNIX) that it is ready for you to enter your first command. (Your prompt is easy for a user to customize; as a result, the same UNIX system may display different prompts for different users.) Any time that you see this prompt, you can enter a command following it. Throughout this manual, we will use % to refer to the prompt.

Interacting with the Computer

Next, we will learn to interact with your computer. A UNIX interaction consists of three steps:

  1. The computer displays a prompt and waits for you to enter a command.
  2. You enter a command.
  3. The computer executes (performs) that command (if it is able to), and when it is finished, displays another prompt.

At that point you're at step 1 again, ready for another interaction.

The commands we'll ask the computer to perform take different amounts of time to complete. Some will take a fraction of a second. Others may take a several minutes. Others will continue to execute indefinitely, until we specifically ask them to stop.

The way in which you interact with a computer depends upon the computing environment of its operating system. For example, an environment in which you repeatedly:

  1. are prompted for a command,
  2. enter a command, and
  3. the machine displays the result of the command

is called a command-line environment -- an environment in which you interact with the machine by entering commands following the system prompt. When you first turn on (or boot) a UNIX workstation, it typically comes up as a command-line environment, though they can be configured otherwise (see below). The UNIX environment is case-sensitive, and virtually all UNIX commands use lower-case letters, so your caps-lock key should be off.

Another common UNIX environment created at the Massachusettes Institute of Technology (MIT) is the X-window system. The X-window system provides an environment in which you use the mouse to interact with on-screen windows, menus and icons. Such environments are called graphical user interface, or GUI (pronounced gooey) environments.

One of the kinds of windows available in the X-window environment (called an x-term) provides a separate command-line environment. The X-window environment is thus a combination environment, combining the convenience of a GUI with the power of a command-line, making it a favorite of computer scientists around the world. Since both plain UNIX and X-windows require knowledge of command-line commands, we will concentrate on them in this exercise.

In order to use a command-line environment, you must learn those commands that the environment "understands." Let's first see what happens if you type something that the environment does not recognize. Enter

   % qwerty

How does the system respond ?

If you try other nonsense words, you'll find that the system responds in the same way. It's thus safe to make mistakes and mistype commands -- you won't harm the computer.

Directories and Files

Very soon, we'll be entering and running a simple Java program. But when we are done, we'd like to be able to save your program somewhere "safe" -- somewhere that, if the power goes off, your program can still be retrieved. To save your program safely, the computer stores it on magnetic media (usually a hard disk) in a container called a file.

During this course, we'll create dozens of files, and so some means of organizing them is needed. Just as the documents in a filing cabinet are often kept in manila folders, we can create a container in which to keep related files, called a directory. Let's do it! Enter

   % mkdir projects

The mkdir command is the UNIX command to make-a-directory. By placing the word projects after the mkdir command, we tell the system that we want to make-a-directory named "projects." Practice by using the mkdir command to make three directories: one named "practice", one named "labs" and one named "myLib".

In most GUI based operating systems, a directory is called a folder. You should be familiar with both terms. In the general parts of the lab manual, you may be instructed to create a folder.

Viewing the Contents of a Directory

Next, let's learn how to look at what's inside of a directory. Enter

   % ls

The ls command stands for list the contents of a directory. If all is well, you should see (at least) four things listed: the four directories you just created!

   labs     mylib    practice      projects

(You may see more than these four directories, depending on how things are set up at your institution.) Many people like to visualize the relationships of these directories like this:

where $HOME represents our home directory -- the directory where UNIX places us at the beginning of a session.

The ls command can also be used to find out what is in a particular directory. Try entering:

   % ls labs

If you follow the ls command with the name of a directory, it displays that contents of that directory. Since each of your directories are new (i.e., empty), there is nothing for ls to display.

To get a more detailed listing of the current directory, enter

   % ls -l

The -l is called a switch that causes the ls command to generate a long (i.e., detailed) listing.

The UNIX On-line Manual

Detailed information about all of the switches that can be used with a given UNIX command (such as ls) is available in the UNIX on-line manual. The name of the command to search this manual is man. For example, if you enter

   % man ls

the manual entry for ls will be displayed, providing exhaustive information about that command, including all of its switches, the meaning of the long listing information, and other information. Don't be discouraged if much of the manual does not yet make sense, reading the UNIX manual is an acquired skill that requires much practice, and you will become more accomplished at it over time.

Removing a Directory

Since we made practice just for practice, let's get rid of it. Enter

   % rmdir practice

The rmdir command is the remove directory command, so this changes our picture as follows:

Whatever directory you specify following the command will be removed from the current directory, provided that the directory being removed is empty. To remove a non-empty directory named DirectoryName, the command

   % rm -r DirectoryName

can be used (but this should only be used when DirectoryName contains nothing important).

Using the ls command, verify that practice no longer exists.

Identifying the Working Directory

When you begin a computing session, UNIX always starts you out in the same place -- a special directory called your home directory. As we shall see shortly, you need not remain in your home directory, but can "change location" to a different directory.

The directory in which you are working at any given time is called your working directory. (Your home directory never changes, but your working directory can.) To find out the name of the working directory, you can use the pwd command (which stands for path to the working directory).

   % pwd

Try it for yourself and note what is displayed.

UNIX systems utilize what is called a hierarchical directory system, meaning that directories can contain other directories, and that there is one root directory named / that contains all of the other directories in the system. Type

   % ls /

to list the contents of your system's root directory. Within / are a number of subdirectories, including usr, sys, include, and so on, each of which contains other directories and/or files. On my UNIX system, the users' home directories are stored within one of these subdirectories whose name is home. Within home, the name of my home directory is adams. Here is a picture showing a bit of the hierarchy on my machine:

Now, if you compare this to my output from pwd, you can see that pwd begins by displaying / (the root directory) followed by home, followed by a slash (UNIX unfortunately uses slash both for the name of the root directory and to separate directory names) followed by adams. That is, pwd lists the sequence of directories between the root directory and my working directory (which is called the path), with slashes between the directory names. Since my working directory is currently my home directory, the last name that pwd displayed happens to be my home directory (in my case adams). If we use shading to indicate the working directory, then we can visualize the situation this way:

Apply this to the output from your pwd command. What is the name of your home directory?

A last word on the home directory: UNIX treats the tilde character (~) as an abbreviation for your home directory, which can save you a lot of typing if the path to your home directory has lots of characters. We will make use of this abbreviation at the end of the lab.

Changing to a Different Directory

If we created all of our files in our home directory, it would soon become cluttered. By grouping related files within a directory, one's files can be kept more organized. Thus, we created labs in which we'll store the files for our lab exercises, and projects in which you can store your programming projects files (we'll discuss mylib in a later lab exercise.)

Since we are working on a lab exercise, let's change the working directory to directory labs. Enter

   % cd labs

The cd command is the change directory command that changes the working directory to the directory whose name follows the command.

Next, use the pwd command and note its output: how is it different than it was before?

The cd command changes our location within the directory hierarchy, effectively changing the working directory. The effect is to change the preceding picture as follows:

Since pwd outputs the path (from the root) to the working directory, the output of pwd changes each time you cd to a different directory.

To see everything in a directory, enter

   % ls -a 

The -a switch tells ls to list all of the contents of a directory. You will notice two odd items in the output of ls -a: a period (.) and a double-period (..).

In the UNIX file system, . is another name for the working directory (whatever directory you are in); and .. is another name for the parent of the working directory.

For example: We created labs within our home directory. The cd labs command took us from that directory "down" into labs. How do we get from labs back "up" to the directory where we began? Enter the command:

   % cd ..

This command moves us "up" one directory because .. is always a synonym for the parent of the working directory, regardless of its actual name. We've thus changed the working directory back to what it was previously:

(Note that since that directory is our home directory, we could also have used cd to accomplish the same thing, because cd without an argument always returns you to your home directory.) Use pwd to verify that you are currently "in" your home directory.

To wrap up this section, do the following:

  1. Change directory back "down" into labs;
  2. Make a new directory (inside labs) named 0;
  3. Use ls to verify that 0 was created successfully; and
  4. Change directory from labs "down" into 0.


Quick Unix Reference

Back to the Lab

Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.