Introduction to Unix

Highlights of this lab:

The purpose of this lab is to help you develop a working knowledge of the Unix operating system and the vi editor.  Unix is the operating system that you will use to compile and run your C++ programs, and vi is the recommended Unix text editor for CS110 lab.  In this lab,  you will learn:

Lab Exercise:

Use the Unix, vi, and other skills described in these notes to create and run a simple C++ program.
Click the little computer above for a detailed description.

NOTE: There are NO lab assignments for CS110 labs, but (there's always a "but"), there are three good reasons (probably more) for you to do the lab exercises.

  1. You need to develop technical skills and acquire new programming techniques.
  2. There will be questions on the class quizzes and exams based on the lab material.
  3. You will find it much easier to tackle your class assignments if you first perform the lab exercises.
  4. If you need help during the week, you are welcome to go to CL119 during Office Hours posted for lab instructors. They will also be able to help you there if you have a problem with your Hercules account.
    There will also be a sign by the CL119 door indicating the location and times of the Programming Help Sessions put on by the Computer Science Department.

Getting started -- logging on to your Hercules account

In order to use the PC's in CL135, you must logon with your HYPERION user name and password.  For detailed instructions of logging on lab PC's, please refer to this page:

Double click on the Telnet icon  on the Win95 desktop, then choose HERCULES from the Host window and click on Ok.  Enter your Hercules username and password when prompted to do so.

Your username and password are on the account sheet you got from your lab instructor.

Unix Commands

General Format

Unix commands are cryptic, often only two or three characters in length. However, they are representative of the function they perform. For example, ls is the command to get a directory listing.

The general format for Unix commands is:

        command -options other arguments

where "command" is the name of some command followed by a number of options or other arguments.
Options are generally single characters that represent specific options. The minus sign "-" should be entered before an option.

        Type in ls 
        and then type in ls -l

Notice that the "-l" option results in a 'long' listing showing file permissions.

List of Commands

A Unix command summary is stored in HTML form on the CS Department's WWW site at:

Please be aware that you are responsible for learning all of these basic commands.

Finding Unix Commands

Unix commands descriptions are all kept on-line in the form of "manual" entries. If you do not know the specific command to perform a function, you can enter the "man " command, followed by the "-k" option, and then a keyword that relates to the command you are looking for.  For example, if you wanted to find the command to change your password, you could use "password" as a keyword.

Type in the following:

man -k password 

There will be several related Unix manual entries cited. Look for the one most relevant to what you want to do. i.e.

 change login password and password attributes 

What is the 5 letter Unix command to change your password?

This document will present other Unix commands in the context of tasks that you will need to perform for your CS110 class. It is assumed that you have no prior experience with Unix and are using your Hercules account for the first time.

The vi Editor

A standard editor in Unix operating systems is "vi", pronounced vee-eye. Like Unix, vi is cryptic, designed to minimize typing. You can use other editors if you wish, but you will be tested on vi in your class quizzes and exams.

Simply enter       vi filename
to call up vi. If the file does not exist, it will be created. If the file does exist, it will be read into the edit buffer.

A few notes on vi before we get into the commands:

To enter insert mode, position your cursor at the desired location and enter one of the following characters. The letter you type will not be displayed because you are not yet in insert mode.

i insert before cursor position 
a insert after cursor position 
O insert before current line 
o insert after current line 

To end any insertion, type the Esc key.
If you are not sure, type the Esc key anyway, it won't hurt.

To move around in the file:

Arrow keys. You can use the keyboard arrow keys.
Ctrl f moves you forward one 'screen' at a time 
Ctrl b moves you backward one 'screen' at a time 
G moves you to the end of the file 
/search_string/ If you want to go to the location in a file where a specific word or phrase is located, you can put that word inside slashes "/" and vi will take you to that spot. Note that it will take you to the first occurrence of that search string. If that's not the one you want, enter n and vi will take you to the next occurence of the string.

To replace character(s):

r replace the current character 
R replace text starting from the cursor position 

Replacement initiated by R must be terminated by typing the Esc key.

To delete character(s):

x delete the current character 
[n]dd delete the current line(s) - the line(s) are moved into the edit buffer.
e.g. dd deletes the current line
e.g. 3dd deletes 3 lines starting at the current line.
In other words, precede dd with a number to specify the number of lines to delete.

To "cut/copy & paste" within the vi editor:
Use vi commands to cut or copy lines into the edit buffer and then move to the desired location in your file and enter the vi paste command.

[n]dd delete (cut) the current line(s) (See the description just given.)
[n]yy yank (copy) the current line(s) - a copy of the lines is placed into the edit buffer.
e.g. yy yanks the current line
e.g. 3yy yanks 3 lines starting at the current line.
In other words, precede yy with a number to specify the number of lines to yank.
p paste the line(s) from the edit buffer after the current cursor position.
P paste the line(s) from the edit buffer before the current cursor position.

To "cut/copy & paste" from outside the vi editor:

Setup your PC
When you are in a browser like Netscape or another PC application, use your mouse to highlight the text that you want to copy into the clipboard.
Select Edit -> Copy from the application's menu.
Now activate your Telnet window, with vi active.
:set noai
This vi command translates to "set no autoindent".
You'll find that if you don't set this, when you do your paste, the text will become very messy as vi attempts to automatically indent lines.
Get into insert mode. (First make sure you are at the desired location in your file.)
Edit -> Paste
Select this option from the Telnet menu. It takes the contents of the Windows clipboard and pastes it into your file.

To exit the VI editor:

:q! exits vi without saving changes 
:wq saves the file and then exits 
ZZ saves the file and then exits 

Example of Creating a File with vi.

Here is an example of how to create a file called "header.txt" using vi.

Start by entering:   vi header.txt

In vi, you are either in command mode or insert mode. You are now in command mode so that every letter you type is interpreted as a command.

To start entering text, type the letter "i" to get into insert mode. You will remain in insert mode until you type the ESC key; otherwise, everything you type is entered into your file.
NOTE: do not use the arrow keys while you are in insert mode.

While in insert mode, enter the following lines. Do not worry about mistakes, you can correct them later.

        Student Number:
        Assignment Number:
        Program Creation Date:
        Purpose of the Program:

Type the Esc key to get out of insert mode. You are now in command mode and can make corrections/additions.

For example, you could use the arrow keys to move the cursor to the colon ":" following "Name". Then type the "a" key to get into append mode so that you can add your name to that line.
Remember to type the Esc key to get out of insert mode.

When you are finished making all changes, be sure to write the contents of the edit buffer and quit vi by entering:


You will notice that as soon as you type the colon, the cursor will move down to the bottom left corner of the screen. If this does not happen, perhaps you are still in insert mode. Just type the Esc key again to get back to command mode.

For more information on the vi editor, refer to the 5.2. VI EDITOR section of the CS Dept Users Manual.

The Users Manual can also be accessed from the CS Dept Home page, following the links: Computing Information - User's Manuals.

Transferring files using FTP

Mercury to Hercules

In some sections of your labs, you will be asked to transfer some documents, for example, sample C++ applications from Mercury -- the CS Department instructor's machine, to Hercules -- the machine where you have your accounts.  To do this you use the command-line FTP program located on Hercules.

The following is a sample session showing how to transfer hello.cpp, from Mercury's pub/class/110/ftp/cpp directory to your Hercules account.
First, from Hercules, change to the directory where you want to store this file, and then follow the following procedures.
Note: The commands in red are those that you are supposed to enter.  The instructions for responding to particular prompts are in bold.

hercules[5]% ftp ftp
220-Access to this system ...
220 mercury FTP server (Version wu-2.4(3) Tue Jan 16 10:25:00 CST 1996) ready.
Name (ftp:pvh): anonymous
331 Guest login ok, send your complete e-mail address as password.
Password: (JUST PRESS THE "Enter" KEY HERE)
230- Welcome to ...

ftp> ascii       (NOTICE YOU MUST ENTER THE ascii MODE)
ftp> cd pub/class/110/ftp/cpp
250 CWD command successful.

ftp> get hello.cpp
ftp> quit

Hercules to a PC

Now why would you want to do this?

The answer is that in order to use the printer in CL135, you must print directly from the PC, not from a Telnet to Hercules session. Let's suppose you have the file hello.cpp in your Hercules account space and have made some changes to it. You want to get a printout of it.

To do this you use the graphical FTP program located on the PC. Go to the PC desktop and double click the FTP icon.

You will then see a "connect" screen in which you need to select "Hercules" and enter your username and password. Click on the OK button when you have this set up.

When you have established connection, set up the "Remote" side of the window, (the right side), to the desired location in your Hercules account. On the "Local" side, (the left half of the window), you should be located in C:\Workarea. This is fine, you just want a temporary location to hold the file. Make sure you have selected Ascii for a text file, and then click on the left arrow in the center of the screen.

When you have completed your transfer you can go to Notepad in the Tools folder to read in your file and print it out.

How to Compile and Run C++ Programs on Hercules

Let's suppose you just followed the previous example and had successfully FTP'd the file called hello.cpp to your current working directory. The commands to compile and run this program are as follows:
hercules[115]%  CC hello.cpp -LANG:std -o hello

hercules[116]%  hello 

Welcome to the world of C++
What is your name?  Ada 

Hi Ada! Welcome to CS110 class.

Actually, when you enter the compile command, there will be a delay as Hercules processes the file, then you should see something like this on your screen:

hercules[4]% CC hello.cpp -LANG:std -o hello

--- C++ prelinker: hello.o ---
 CC -c -DEFAULT:abi=n32:isa=mips4:proc=r10k -LANG:std -n32 hello.cpp

--- C++ prelinker: hello.o ---
 CC -DEFAULT:abi=n32:isa=mips4:proc=r10k -c -DEFAULT:abi=n32:isa=mips4:proc=r
10k -LANG:std -n32 hello.cpp
Let's have a look at those commands in a little more detail.

Compile Command

CC hello.cpp -LANG:std -o hello

If we break this down, we can see what each part of the command does.

CC This is the command that says "Compile my C++ program."
Notice that this is capital CC not lower case. Unix is very "case sensitive" i.e. there can be a huge difference in meaning between characters in upper case and those same characters in lower case.
hello.cpp This is the name of the C++ program.
Notice that you need to use the extension .cpp
-LANG:std This option is needed to is needed so that the compiler will recognize the using namespace std line in your C++ program. namespace std is the area where identifiers such as cout and endl are defined. Actually there can be compatibility issues with some C++ compilers. Refer to your text or any C++ reference; look up namespace or header files in the index.
-o hello This is called the "minus-oh" option.
It tells the linker to create an executable file as specified by the name following -o. It is a convention to name your executable file the same name as the source file without the .cpp extension.
In this example: source file is hello.cpp and executable will be hello.
What happens if you don't use the minus-oh option?
Then your executable file will, by default, be called a.out which is ok, but everytime you compile another C++ file, you would overwrite your a.out file.

Run Command


Is that all??   Yes, actually, that is all there is to it. Just type in the name of the executable file. Following the example just given, the name of the executable file is hello. If we had not specified the -o hello option, then the executable would have been named a.out and that is what we would have had to type to run the program.

Two Unix commands that are useful in association with running a program are the more command and the script command. The more command is handy if your display is longer than a single screen. The script command allows you to save the contents of a program run into a file, which you can then print out. (Useful for putting together an assignment that needs to be handed in for class.)

Refer to the Unix Command Summary for details on the more and the script commands.

This page last modified:
Tuesday, 17-Apr-2012 08:57:04 CST
Accessed     times.

CS Dept Home Page
CS Dept Class Files
CS110 Class Files

Copyright: Department of Computer Science, University of Regina.