Lab 0: UNIX Instructions

Getting Started with UNIX

Your instructor will have to tell you how to access your account and how to start up the X Window System. There are too many variations to cover here. Eventually, you should be logged in and have a terminal window open to interact with.

The terminal window should have a blinking rectangle sitting to the right of a prompt that might look like this:

or perhaps this:
or something similar.

Question #0.UNIX.1: What does your prompt look like?

UNIX is prompting you to enter a command. Any time that you see this prompt, you can enter in text as a command. Throughout this manual, we will use % as our prompt.

Helpful hint: Never, ever type in the %. That's the prompt from UNIX; you must enter in the command after the prompt.

Interacting with UNIX

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, and it displays the results (if any).
The interaction then starts all over with the first step.

The way in which you interact with a computer depends upon the computing environment of its operating system. For example, an environment that uses the three steps above for its interaction is called a command-line environment or a command-line interface.

The UNIX environment is case-sensitive, and virtually all UNIX commands use lower-case letters, so your caps-lock key should be off. This will cause you a great deal of frustration is you forget it, so keep this in mind.

Most UNIX systems these days provide you with the X Window System (also known as simply X or X11). X provides an environment in which you use the mouse to interact with on-screen windows, menus, and icons. Such environments are called graphical user interfaces (GUIs).

The terminal window that you pop up to interact with UNIX allows for a mixed environment of a command-line interface and a graphical-user interface.

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.


% qwerty

For the sake of your own sanity, remember that % is the system prompt, so you must not type it.

Question #0.UNIX.2: How does the system respond?

If you try other nonsense words, you'll find that the system responds in the same way.

Directories and Files

Throughout this lab manual we'll be entering and running simple C++ programs. When we are done with a program, we'd like to be able to save it somewhere safe so that we don't have to type it in again. To save your program safely, the computer stores it on magnetic media (usually a hard disk) in a container called a file.

We'll create dozens of files, and so some means of organizing them is needed, otherwise they'll get mixed up with each other. 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 (or a folder).

Let's create a new directory. Enter this UNIX command:

% mkdir projects
One last time: do not type in that %. It's the UNIX prompt which undoubtedly looks different in your environment.

After you type in the command, you should get the prompt back. If you get nothing else, then the command worked. If something when wrong, you'll get an error message.

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 using the mkdir command, and make three directories: one named practice, one named labs, and one named myLib.

Viewing the Contents of a Directory

The mkdir gives no feedback unless something goes wrong. So how do we know it did anything?


% ls
The ls command stands for list the contents of a directory. If all is well, you should see (at least) 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. But if you don't see your four directories, then you probably didn't make the directories correctly. Go back and try that again, and then verify the new directories with ls.

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. Enter this command:

% 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 or flag that causes the ls command to generate a long (i.e., detailed) listing. The hyphen indicates to ls that the l has a special meaning; it's not the name of a directory or file. Virtually all UNIX commands have flags like this.

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.

You can use the spacebar to 'page forward' in the manual. Most systems let you use b to 'page back'. The letter q can be used to stop the man command; this may be necessary on some systems even if you reach the end of the manual page.

Reading the UNIX manual pages is an acquired skill that requires much practice, and you will become more accomplished at it over time. Skim over a manual page to find just the information your currently interested in.

Question #0.UNIX.3: What does the man page for ls say about the -l switch?

Removing a Directory

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

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

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

% rm -r DirectoryName
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 your home directory, as mentioned above. You do not have to remain in your home directory; you can change to a different directory whenever you like.

The directory in which you are working at any particular time is called your working directory. To find out the name of the working directory, you can use the pwd command:

% pwd

Question #0.UNIX.4: What is printed by the pwd command for you?

UNIX systems utilize what is called a hierarchical directory system, meaning that directories can contain other directories. There is one root directory denoted by / (a single forward slash) which is the top directory; all other directories are directly or indirectly in this root directory.

Run this command:

% ls /
This lists the contents of the root directory. Within / are a number of subdirectories, including usr, bin, include, home, etc. Each of these contain other directories and files. On many UNIX system, the home directories of the users are stored within the home directory. Here what this might look like on a particular machine:

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 use this abbreviation at the end of the lab.

Changing the Working Directory

If we created all of our files in our home directory, it would soon become cluttered. By grouping related files within a directory, you can organize your files. We created the labs directory to store the files for the lab exercises; similarly, projects can be used to store your programming projects files.

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

% cd labs
The cd command is the change directory command that changes the working directory to the specified directory. Similar to the mkdir command, cd will not display anything if it works successfully (although you might notice a change in your prompt). You will see an error message if something fails.

Use the pwd command.

Question #0.UNIX.5: What does the pwd command display now?

Question #0.UNIX.6: How is the output of pwd different from before?

The effect of the cd command given above 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 this command:

% ls -a
The -a switch tells ls to list all of the contents of a directory.

You will notice two odd items in this output: 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.

We created labs within our home directory. The cd labs command took us from that directory "down" into labs. Once in that directory, . refers to that same directory while .. refers to the directory it's in. So, to go back "up" a directory, we can enter this command:

% cd ..
Since .. is always a synonym for the parent of the working directory, regardless of its actual name, we can always use this command wherever we are.

Remember, though, that it moves (or "changes") us up one directory, relative to the current working directory. It does not always return you to your home directory; you can get to your home directory by entering cd alone without specifying a 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 lab0.
  3. Use ls to verify that lab0 was created successfully.
  4. Change directory from labs "down" into lab0.

Other Commands

You should go back to the main exercise now. Return back here after you've copied, compiled, and executed your first program.

Copying a File

It is sometimes useful to be able to make a copy of a file, so that we can modify a copy without modifying the original. To make a copy of bases.cpp in a different file named bases2.cpp, enter this command:

% cp bases.cpp bases2.cpp
(You didn't forget the significance of the %, did you?)

The cp command makes a copy of the file with the first name (i.e., bases.cpp), and names the copy the second name (i.e., bases2.cpp).

Use the ls command to verify that the cp command worked.

It's more often the case that we need to make a copy of a file in a directory different from the one in which the file currently resides. Both of the names bases.cpp and bases2.cpp are the relative names of these files. To use these names, we must be in the same directory as they are.

However, every file or directory also has an absolute name, a name that starts with the root directory (i.e., /), and lists each directory between there and the file name---just as we saw in the output from pwd.

To use a file's (or directory's) absolute name, you need not be in the same directory as that file. For example, try entering:

% mkdir ~/projects/project0
This uses the mkdir command to make a new directory. Since ~ is another name for our home directory, we just made a new directory inside projects (which we created earlier), called project0, using an absolute path name. Here is the picture:

Almost all of the UNIX file manipulation commands accept either relative or absolute file names. For example, you don't need to be in a directory to see its contents. Enter this command:

% ls ~/projects
You should see the directory you just made.

Similarly, to put a copy of bases.cpp in that new directory, you can enter this command:

% cp bases.cpp ~/projects/project0
A copy of bases.cpp will be stored in that directory.

The cp command thus behaves differently, depending on whether you specify a file or a directory as the second name: if you specify a file as the second name, then the cp command will store the copy in a file with that name. If you specify a directory as the second name, then cp will place a copy in that directory with the name as the original.

Using ls and an absolute path name, verify that there is a copy of bases.cpp in ~/projects/project0.

Question #0.UNIX.7: What is your ls command?

Renaming a File

Sometimes we decide after the fact that a file would be better off with a different name. To change the name of bases2.cpp to bases.bak, enter this command:

% mv bases2.cpp bases.bak
The mv command moves the first file to the second file, effectively renaming it. It can be used with both relative and absolute file names, and can be used to rename directories.

Creating a Sample Execution

In addition to a printout of your program, many labs will ask you for a sample execution. Use the script command. After you enter this command, everything you type and everything any program prints is recorded in a file named typescript.

Here's how you should use it:

% script
% Run your program as many times as needed.
% exit
It's important that you type exit at the end so that the file named typescript is closed up properly.

You can print the typescript file just as you would any text document.

Cleaning Up Your Directory

The various files you have created take up valuable disk space, and so we will remove all of the files from lab0 except for our source program bases.cpp (the others can always be easily re-generated from this source program). To remove bases.bak, enter this command:

% rm bases.bak
The rm command removes whatever file you specify. Earlier we saw that when rm is invoked with the -r switch, it recursively removes a directory and everything within it.

Use the rm command to remove the typescript, bases.cpp~ (a back-up file created by emacs), and bases files. Note that some environments may be configured to display * and / symbols after executables and directories (respectively). Do not type in these characters, especially the *, as part of the file name. Very, very, very bad things will happen if you ignore this advice.

Use the ls command to verify that all of the files except bases.cpp are gone. If there are other files remaining in your lab0 directory, use the rm command to remove them, as well.

Then end your session with the computer, following your instructor's instructions.

Don't Panic

If your head feels ready to explode, don't panic! This first lab covers a great deal of material, that you will use over and over again, and as you do so, you will begin to naturally memorize those commands that you use most frequently. You can speed up the process by reviewing the steps we went through in this exercise.

You will only learn this material by doing it many, many times and if you have this reference handy. Be very, very thankful that you've already bookmarked this page---right?!

Here's a quick reference pages of UNIX commands.


absolute name, command-line environment, command-line interface, computing environment, directory, file, flag, folder, graphical user interface (GUI), hierarchical directory system, home directory, prompt, relative name, root directory, sample execution, switch, terminal window, working directory, X, X11, X Window System

Go back to the main exercise.

© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.