Name: _____________________ Class: Comp 217
SSN/ID:   _____________________ Section & Group: ____________
Introduction to Linux


This experiment introduces the student to the Linux operating system. It will cover how to start Linux, log in and log out, open files, examine the GNOME and (if installed) KDE Graphic User Interfaces (GUI), and how to create and manage simple text files. To prepare for this lab, read chapter 1 and be prepared to refer back to this chapter as you work this lab. You will be responsible for this information in future labs.

Objectives:

  1. To introduce the laboratory computing environment.
  2. To introduce the editing, compiling, and execution of a program.
  3. To introduce simple file manipulation commands.

What is Linux?

Linux is a free Unix-like operating system originally created by Linus Torvalds with the assistance of developers around the world. It is an alternative to other operating systems, like Windows and Mac OS. Linux is not a program like a word processor and is not a set of programs like an office suite.

Starting LINUX

To start a Linux session, you must reboot the lab computer. Perform a normal shut down, except you should select "restart" rather than shut down. When the computer starts, you will see the usual BIOS information, then a blue screen showing the choices of operating system to load. Use the cursor keys, as the mouse isn't loaded yet, to select Linux (or RedHat or Fedora). You only have a few seconds so act fast. Press ENTER to start the Linux boot process.

Logging in and logging out

After some time passes and Linux displays all the operations it must perform in order to boot, you will see a blue screen asking for your username. The username for everyone is u1. It is case sensitive. Press enter after entering the username. The next screen asks for your password. Everyone has the same password, passu1, which is also case sensitive. Press Enter. The system will boot to the GNOME GUI.

Once you log in, you will see something similar to the following image.



When you are ready to leave Linux, left click on the Red Hat symbol , which is the main menu. Select Log Out from the menu, then choose Restart the Computer. Press Enter. Linux will shut down, the computer will start again, and you get the boot loader screen again. If you do nothing, it will default to Windows and it will then start the Win2K operating system.

Getting a Shell and editing a file

The C++ language requires a clean text format for its source code. Word processors should not normally be used unless you can save your job as plain text. For that reason, it is highly recommended that you use "Emacs", the standard Linux full screen text editor. The older VI editor is not recommended due to its complexity. For old timers, VI is similar to DOS EDLIN, and just as complicated (or worse).

To get started, we need to first start a terminal session.  In order to do this, simply right click in the work space (the big blue empty screen) and click on New Terminal (alternatively, there might be a "Terminal" icon on the menubar that you can click to launch the Terminal; in addition, you can press CTRL-F10 and use the arrow keys to go to the Terminal entry and press Enter to launch it). This brings up a kernel shell (or console window or terminal window) which allows you to enter commands from the keyboard, just like the good old days BW (before windows).

Getting Started with C++

The main purpose of this lab is to introduce you to the computing environment of your laboratory. You will use the ideas in this lab again and again throughout this course, so you should make every effort to understand not only what, but why you are doing what you are doing at each step.

About this Manual

In this (and every other) exercise, instructions will be printed in this default font (the one you are reading). To help you distinguish the instructions from what appears on your screen, text that you should see displayed on your screen will be shown in this font. Text that you are to type or enter will be shown in the this font.

The distinction between typing something and entering something is as follows:

Getting Started with UNIX

When you see a blinking rectangle (in the terminal or console window) sitting to the right of something like this:

   %
or perhaps this:
   <1>~:

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. The terminal or console window is an example of an X-Term.  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 C++ 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".

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 print working directory).

   % pwd
   /home/adams

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 a sample UNIX system, the users' home directories are stored within one of these subdirectories whose name is home. Within home, the name of the home directory is adams. Here is a picture showing a bit of the hierarchy on this sample machine:

Now, if you compare this to the 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 the current working directory (which is called the path or full path or absolute path), with slashes between the directory names. Since the working directory is currently the home directory, the last name that pwd displayed happens to be the home directory (in this 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.

Copying a File to somewhere else

The Linux copy command is "cp_<source>_<destination>", where source is the name of the file you wish to copy and destination is where you want it to go (the _ represent spaces). If you created a file named test.txt and you want to copy it to the ZIP drive, you would type: "cp test.txt /mnt/zip" and press ENTER.

Dealing with GNOME and KDE

Now, we will examine GNOME a little bit. Close any open files and shells. The Red Hat symbol is the main menu. Feel free to examine the contents. It's similar to the Windows START function. Examine the games and play one if you wish. We'll relax the no game playing rule this time. It's an educational experience.

You would probably like to see if the file did get copied. You can examine the contents of the zip drive by bringing up the workspace menu again (right click in the empty screen area) then selecting DISKS from the menu. A list of your installed disks appears. Left click on ZIP and a new icon appears on your workspace for the ZIP drive. Double click the ZIP icon, and a window showing the contents of the ZIP disk appears. To examine the contents of the file you copied, double left click on it and a screen appears asking you what to do with it. Since it is a text file, select display, and you can view the file contents. Note the navigation icons at the top of the window. If you push back, then you'll go back to the listing screen. If you should have Microsoft Word or Excel files on the disk, and you select them, Linux will open its Open Office equivalent and attempt to import the files. It may not be 100% successful, particularly with highly formatted files, but it'll do the best it can.

When you decide to leave Gnome, you have several options. If you just select log out, you will get a new log in screen. At the bottom are several possibilities. Choose session. If the KDE GUI is loaded, click on it and then log in using KDE. It's similar to GNOME, but also different. You should examine the features of KDE if it's available. After you finish playing with KDE, log out of KDE and log back in to GNOME.

Examine the various features of GNOME until you think you've had enough. You might try to install the weather icon to provide you with local weather. Google has basic information on using GNOME, KDE and the shell. Refer to these pages and attempt as many possibilities as you can in the time you have available. The weather report is in here somewhere, so I leave it to you to look it up.

This completes lab 1. You can see it is basically a self exploration exercise, which you should take the time to complete and be as thorough as possible. We will look at more LINUX in the next lab.

Just for Fun

If you want to try a C++ program, open GEdit (by typing gedit & at the command prompt), and type in the following:

#include <iostream> using namespace std; int main(void) { int age; cout << "How old do you think you are? "; cin >> age; cout << "So you claim to be " << age << ". Sure you are!\n\n"; return 0; } Save the file with any name you like, but be sure it ends with .cpp Example: test.cpp

To compile it, enter the command g++ -o test test.cpp substituting the filename you chose for test.

If there are no other errors reported, run it by typing "./test"

It should work.

(The reason you need to put "./" in front of the program is that Linux thinks all executables are in another directory. "./" tells Linux to run the program from your current directory. Picky, isn't it?)

Questions:

1. The X-window system provides what kind of computer interface?




2. A program can be stored on a disk in a container called a _________?




3. A container in which related files can be kept is called a _________?




4. A ____________ is a program that translates a high-level program into a functionally equivalent machine-language program.




5. Suppose you are currently in your home ($HOME) directory and there is a file called science.txt in the /usr/ directory that you want to copy to the labs directory within your home directory. Please give the full cp command that will allow you to do this copying.



Ricky J. Sethi <rickys at sethi.org>
Last modified: Sat Mar 25 17:45:40 PST 2006