Mount Holyoke Computer Science Lab - Linux/Unix Hints and Tutorial

This document is provided to help to familiarize you with Unix and Linux in general, and specifically the Mount Holyoke Linux systems that we will be using in some of our labs this semester. It assumes no Unix background, but it does assume that you are comfortable using computers with a graphical user interface, such as Mac OS or Microsoft Windows.

Linux is one of many "Unix-like" operating systems. Unix was originally developed for time sharing systems but has become a standard on desktop PCs and workstations as that hardware has become capable of multiprocessing as well. Many Unix variants have been developed over the years, with Linux and FreeBSD the most common today. Mac OS X is also built on top of a Unix framework.

To get started, sit down at a PC in the lab and go through this tutorial, trying things out as you go. Upon completion, you should be able to understand the basics of entering a command line, move around in the file system and view existing files, do basic manipulations of windows, do basic editing with the Emacs text editor, and know where to find  help.

Logging In

The PCs in Clapp 202 boot both Windows and Linux, so first be sure to boot your PC into Linux. If it is at the Windows login screen, hit Crtl-Alt-Del then choose "Shut Down" and "Restart". When the boot menu comes up, choose the "Ubuntu" option to boot into Linux. Booting Linux should take about a minute.

To start, log in by entering the user name and password for your computer science account in the login window. Your user name for this account will be the same as that used for your Windows account. If you don't have an account from a previous semester, fill out a form and turn it in to Wendy or Adam.

Using the Gnome Interface

Linux and other Unix variants usually provide a graphical interface through the X Window System. The current major version is 11 (and has been for many years), so this is often referred to as "X11".

Unlike the Windows or Macintosh interfaces, X11 does not enforce a particular "look and feel" to the graphical user interface. This task is taken care of by a component called a window manager. Window managers range from the minimalist twm to complete desktop environments such as KDE or Gnome. Your account is set up to use Gnome. In addition to having a choice of window managers, most X11 window managers are highly customizable.

When you log in, your Gnome desktop will look something like this:

Since you're all familiar with the Windows or the Macintosh interface, it shouldn't be too hard to get used to Gnome. Many of the applications you will need can be selected from the menu bar at the top of the page. The familiar "point and click" techniques will help you get around, at least until you get comfortable at the command line.

The Unix command line is what separates true Unix users from GUI-dependent Windows and Mac users. Once you get comfortable with the command line, you will wonder how you ever got anything done spending all that time navigating menus and clicking buttons. To access the command line, you need to open a terminal window. This can be found in Gnome under the "Applications" menu under "Accessories". Your desktop with a terminal window will look something like this:

You may use this window to issue commands to the Linux system. These commands may launch other applications, navigate the file system, or connect to other computers on the network.

When you log in, you will have access to collection of files called your home directory. The files you create in your home directory are not stored on the computer on which you are working. Instead, they will be stored on our department's Linux file server, babyred. No matter which computer you log into, you can access your single home directory.

All windows managed by Gnome have some common features. Much like those in Windows or Mac OS, you can close, iconify, and resize windows. Many come equipped with their own menu bars.

Using Unix Commands

Much of your time in the Unix environment will be spent interacting using the command line. You can open multiple terminal windows if you'd like and switch among them with the mouse.

To get started on the command line, type the command ls in the shell window and press return. This command will list the files in your home directory. A command consists of the name of a program, zero or more options preceded by -, and zero or more arguments, which are themselves words. The command line is terminated with a carriage return.

If you make mistakes in typing on the command line, use the Delete or Backspace key to erase the previous character.

Here are some other useful commands related to files:

ls List the files in the current directory
cp file1 file2 Copy the file named file1 to a new file named file2
mv file1 file2 Rename the file named file1 to have the name file2. (mv is short for "move", since the same command is used to move files among your directories)
rm file1 Remove the file named file1 (Warning: you may be used to your computers asking "Are you sure?" and moving things to the trash so you can recover things you deleted by mistake. This is not the case with the Unix rm command. You ask for it to be deleted and it will be deleted and it's not coming back!)
more file1 Display the contents of the file file1
lpr file1 Print the file named file1

One thing you will notice with Unix is that "no news is good news". Often if a Unix command is successful, it will produce no output! If it fails, you will receive an error message. Of the list of commands above, only ls and more produce output when they succeed.

The more command deserves some more comment because it is an interactive command. It displays your file one screenful at a time. After each screenful, it displays a prompt either consisting of the name of the file being displayed in reverse video (for the first screenful), (END) in reverse video (for the last screenful), or : (for all other screenfuls). The prompt appears at the bottom of the screen. At this prompt, you have several options, the most useful being:

space bar Display the next screenful
b Display the previous screenful
q Quit more, returning to a shell prompt

You can try out more by typing

more /home/jteresco/shared/cs324/examples/late/late.c
This will show you a file containing the late penalty calculator. (One useful tidbit: hitting the tab key will autocomplete directory and file names. Try typing /home/jter and then hitting the tab key.)

When you use ls -F to list your files, a directory will appear with / at the end of its name. At all times you have a current directory. When you first log in, your home directory is your current directory. Your commands are interpreted with respect to your current directory by default. So, for example, ls lists the files in your current directory if you give it no arguments. Here are some useful commands to manipulate directories:

mkdir dir1 Create a new directory named dir1
rmdir dir1 Remove the directory named dir1 (You can only do this if the directory is empty)
cd dir1 Make dir1 be the current directory
pwd Display the name of the current working directory

Filenames in Unix can contain any characters, but they are often easier to deal with if you restrict yourself to filenames that consist only of letters, numbers, and '.'. A filename is typically divided into a descriptive name and an extension, separated by '.'. Unlike Windows and Mac OS, Unix will make no attempt to hide these extensions from you. Extensions are purely by convention but typically indicate the type of file. For example, myfile.txt would be a an appropriate name for a text file, while MyJavaClass.java would likely contain a Java program. Extensions are generally optional although some programs, such as the Java or C compiler, expect them to be there.

Filenames can be given as names that are relative to the current working directory or as absolute names from the special root directory. Thus far, we have used mostly relative names that refer to files in the current directory. We can also use relative names to identify files in subdirectories. For example, mydir/myfile is the file named myfile in the directory named mydir, where mydir is located in the current working directory.

No matter what the absolute pathname is to your home directory, you can always refer to it with the special relative pathname ~. For example, no matter what your current directory is, if you type ls ~, you will see a listing of the files in your home directory rehardless of your current directory.

An absolute pathname always begins with / while a relative pathname never does. To find out the absolute pathname that corresponds to your current directory, type pwd. Use pwd to find the absolute path for your home directory.

One last useful feature of the command line: Pressing the up arrow will give the last command you executed. Pressing return will run that command again. Try this. You can access older commands in your history by hitting the up arrow multiple times.

To practice these commands, copy the late.c file to your home directory. The command

cp /home/jteresco/shared/cs324/examples/late/late.c ~
will do this. For practice, rename the file, move it into a newly created subdirectory, and view it there.

Basic Emacs

Emacs is a widely used text editor for Unix. It relies heavily upon control-characters as a means of entering editing commands. Many commands are also available via pulldown menus, but experienced users tend to use control commands almost exclusively. To start Emacs, type emacs & in a shell window. (Useful tip: the & at the end of any command executes that command in the "background", meaning that the program starts to run and you get your command prompt back.)

This will open a new window on your display. The window will look similar to that shown below:

Across the top row are the titles of pulldown menus that are available. Initially, we will work with a few of these menus and a few keyboard commands. As you gain more experience, your repertoire of commands will grow and will move away from menus and toward keyboard commands.

First, we will create a new file. To do this, point at "File" and click the mouse button. Then select "Open...". At the bottom of the window, you will see "Find file: ~/" with a small black box following it. The box indicates where characters will go when you type. Type in the name of the file that you want to create and hit carriage return.

The last line of the Emacs window is where command prompts and messages appear. The reverse video line immediately preceding it is a status line. It shows you the name of the file you are editing. Before that you should see -----. This means that the file is up-to-date. If it says ---**, it means the buffer you are editing has been changed since it was last saved. If it says ---%%, it means that you cannot modify the file. After the filename is the "editing mode" being used by Emacs. This might say (Fundamental) or (Text), if you are editing a text file, but will say (C) when editing a C program or (Java) when editing a Java file. Next is the current line number (L1 indicates the first line). Following that is an indication of how far the current line is in the file in terms of percentage. All indicates that the entire file is showing. Top indicates that you are on the first screenful. Bottom indicates that you are on the last screenful.

The area between the menu titles and the status line is the buffer that displays the contents of the file. Most of your typing goes directly into this buffer. Type some text. Notice how status line changes. You can move the cursor around using the arrow keys. Use the scrollbar to move through the file. Use the "Save" command from the Files menu to save the buffer to a file. Use "Exit Emacs" from the Files menu to exit Emacs. Notice the characters that appear in parentheses at the ends of command names in the menus. These tell you the keyboard characters to type to execute the commands without using the commands. For example, saving the buffer can be executed by typing Control-x Control-s. In the menu, control characters appear as C-. Type these by holding the control key down at the same time as typing the second character. Another common way to enter commands is using the escape key. This is abbreviated M-. In this case, the escape key and the following key are typed separately. For example M-x represents typing the escape key followed by the x key. Here are the keyboard sequences for some of the most common Emacs commands:

C-v Next screenful
M-v Previous screenful
C-d Delete next character
Backspace Delete previous character
C-x C-s Save buffer to a file
C-x C-f Open a file
C-x C-c Exit Emacs

You can find more Emacs commands in the brief introductory guide linked from the course home page.

Using keyboard commands is generally faster than moving one's hands back and forth between the mouse and keyboard as one must do to use the menus and scrollbar. While it takes extra effort to learn, the payoff is worth it in the not-too-long run in terms of speed of using the editor.

Getting Help

The Unix command man provides information about all Unix commands and programs. For example, to learn about the ls command, type man ls. You can then page through the manual information as you did with more. To learn more about the manual, you can read the manual about the manual by entering man man at the command prompt. A great way to learn Unix is to watch experienced users and ask questions.

Creating "tar files"

For some labs, you will need to submit multiple files. To make this easier to deal with, you will be asked to submit them as "tar files". A tar file is kind of like a ZIP or Stuffit file from the Windows or Mac worlds, but is created on the command line and is portable across all Unix versions.

To create a tar file lab1.tar that contains files late.txt, prime.c, and access.c, you can use this command:

tar cvf lab1.tar late.txt prime.c access.c
The cvf are three flags meaning "create" "verbose" and "file". The v tells tar to give you some output describing what files it's operating on, and the f means use a specified filename rather than the default, which is actually a tape device for historical reasons. (Hence the command's name: "tape archive"). You can list the contents of a tar file with
tar tvf lab1.tar
And you can extract the contents of a tar file with
tar xvf lab1.tar
But beware! This will extract the files with no concern for whether it is overwriting files in your directory. For this reason, it's good practice to create a brand new empty directory to use when extracting tar files.

Logging Out

To log out, select "Log Out..." from the "System" menu in the top left-hand corner of the screen. Always close all Emacs and shell windows prior to logging out to prevent you from losing any work.

Acknowledgement

This document is based on and borrows heavily from a similar one developed by Steve Freund at Williams College.