CSC161 2010F Imperative Problem Solving

Laboratory: GNU/Linux I

Summary: In this laboratory you will explore some basic features of the GNU/Linux system on the MathLAN.

Prerequisites: Familiarity with the MathLAN network

Preparation

Log in to your MathLAN workstation. (Of course, you've probably already done that if you're reading this laboratory.)

Exercises

Exercise 1: The GNOME Window Manager

You have been using the MathLAN for a while now, so I will assume you are familiar with most of the features of the GNOME window manager, including the task bar along the bottom of the screen and the applications menu in the lower-left corner. In case you are not familiar with the name, GNOME is the default GUI interface for GNU/Linux on the MathLAN.

a. Did you know that GNOME provides four separate workspaces to help you organize many simultaneous tasks? To try this out, open a few terminal windows and a web browser. Now find, on the task bar, the icon that is a set of four squares, arranged in a 2x2 grid. This is called the Workspace Switcher. One of the squares (probably the top left one) should have some smaller boxes within it, representing the open windows in your current workspace. The front-most of these should be colored blue. You can drag the icon for any open window from one workspace to another, and you can move yourself to another workspace either by clicking in its square of the 2x2 grid or with the keystrokes Ctrl-Alt-{left,right,up,down}.

If you haven't done this before, go ahead and try it. As you become used to having four times the real-estate to work with, you will find it very handy.

b. Did you know that you can copy-and-paste text from one window to another, even when the two windows are not running the same program? In Linux, you can copy text into a "cut-buffer" simply by highlighting it with the mouse. (After highlighting it, you do not need any additional keystrokes to indicate that the text should be copied.) Then to paste the text, you traditionally click on the middle mouse button -- assuming you have a 3-button mouse designed for use with UNIX. If you have a 2-button mouse, as we do in the MathLAN, you can emulate this by clicking on both the left and right buttons simultaneously.

To try this out, copy the command below into a terminal window and run it. It should start a process that does nothing (sleeps) for 3 seconds. When the 3 seconds are over, another prompt should appear in the terminal window.

sleep 3

Exercise 2: The Terminal Window

You may already be familiar with using the terminal window, but there may also be a few nifty shortcuts that you don't yet know. Please take a few moments to try each of the following items. To open a terminal window, left-click with your mouse on the small monitor icon on the bottom row of the front panel.

a. Suppose you wrote a program that contained an infinite loop, and you ran it from the terminal window. How could you stop the program?

One way is to close the window, but a better way is to type ctrl-c (i.e., hold down the ctrl key while you type c). This should kill the program without making you re-open the window and re-navigate to the directory you were working in.

To give that a try, launch the sleep utility for 10 seconds, and then kill it with ctrl-c.

b. Suppose you want to run a command that you ran a short while ago? If the command is complicated, it would be nice not to have to re-type it, and you don't have to!

One way to retrieve previous commands is using the up-arrow key. If you're not already familiar with it, give it a try: press the up-arrow key two or three times in a terminal window. Note that the down-arrow also works, which can be helpful if you go too far.

But what if the command you want was several commands ago, say 10? Who wants to type that many arrows? To see another option, first use the following command to get a list of the commands you have used most recently.

history

The list you get will be longer, but it may end something like this.

505  sleep 10
506  history

Now if you wanted to re-issue the sleep command, you could do so by typing "!505" to repeat command number 505. Better yet (since you don't need to know the command number), you can repeat the command with "!s". This will re-issue the most recent command that begins with the prefix "s". Note that the prefix can be of any length: it is not limited to one character.

Give it a try! You may find it very useful when you have a sequence of commands that you want to cycle through repeatedly; for example, to edit, compile, and run a program from the terminal window.

c. Another very useful keyboard shortcut is called "autocompletion." Suppose you want to do something with a file called .emacs in your home directory. Although that particular file name isn't very long, it can still be handy not to have to type the whole name every time.

Try out file name autocompletion by typing "cat ~/.em" followed by a TAB key. You should find that when you press TAB the system completes the (unique) file name for you. Note that on the MathLAN, when you type the tilde character "~" in the terminal window, the character will LOOK like a hyphen "-" instead. Even so, the system will interpret it as a tilde. (The command name cat stands for "concatenate" for reasons we will see later. It can be used to display the contents of a text file, so you should see the file .emacs scroll by when you use this command.)

What if you press TAB too early, such that there isn't a unique completion? Try this by typing "cat ~/.ba", followed by TAB. You should find that the system adds a couple letters and beeps. This tells you that there are multiple files in your home directory that begin with .bash, but no files that begin with .ba followed by something different. The system has completed as much as it can.

Now type "cat ~/.bash" followed by two TAB characters. This should give you a beep, plus a list of the files that begin with the given prefix. Thus, you could finish the command yourself, or type a few characters and use autocompletion again.

Next, try typing "cat ~/.bing". This time you will probably get only beeps, no matter how many TABS you add. What do you suppose the system is telling you now? Can you think of an experiment that will confirm or refute your idea?

Finally, note that autocompletion also works with commands. For example, try typing his followed by TAB.

What is the fewest characters you can type to produce the history command?

What other commands begin with the letter h?

Are there any commands that begin with her?

d. Here is another simple thing you can do to make your life, when working in the terminal window, easier.

First, the hard way. Suppose you want to read a pdf file. We'll use a program called evince. Type

evince ~rebelsky/Web/Courses/CSC161/2010F/Labs/linux-lab-1.pdf

You can use cut and paste or autocompletion to reduce your typing. This command will open the pdf reader just fine. But trying switching back to your terminal window. You will notice that it is unavailable for further use (i.e., you won't get another command prompt) until evince is closed.

Now close evince, and then re-open it from the terminal window, but this time add an ampersand character to the end of the command:

evince ~rebelsky/Web/Courses/CSC161/2010F/Labs/linux-lab-1.pdf &

Now, when you return to your terminal window, a prompt is waiting for you, making it easy to do multiple tasks at once. Adding the ampersand character to a command causes the command to be launched as a background process, allowing you to continue working with your terminal window.

e. Finally, let's talk about a really easy task: closing the terminal window. You are probably used to reaching for the mouse when you want to close the window, and of course that works just fine. But the more you get used to typing commands, the more you may like them, because they are faster. The command exit will close a terminal window.

Even faster, you can use the keyboard shortcut ctrl-d. As we will soon see, ctrl-d is used in multiple contexts in the Linux system to indicate the "end-of-input" character. Thus, pressing ctrl-d at a command prompt indicates that there will be no more commands forthcoming, and the terminal window should close.

Go ahead and give it a try.

Exercise 3: The Linux File System

Once again, I'm sure you know that Linux maintains directories and files in a tree structure. Do you know what kinds of files are stored in the directory /bin? Are you familiar with files named ".", "..", or "~"? Do you know how to set permissions such that others can (or can not) modify your files? We will explore questions like these in this section.

a. First, let's talk about absolute and relative pathnames for files. You can determine your current "working directory" (i.e., your position within the directory tree) with the command "pwd", which stands for "print working directory".

A relative pathname for a file is a name that is given "relative" to your current working directory. For example, if your current working directory is csc201, then hw/hw2.c could be a relative pathname for a file named hw2.c located in a directory named hw that was itself inside csc201.

An absolute pathname, such as /home/username/csc201/hw/hw2.c, includes the file's complete path starting with the system's "root" directory, which is always named "/" on a Linux system. Just like it sounds, the root directory is the topmost directory in the file system tree.

Each directory in a Linux system contains two special files "." and ".." that can be useful when constructing relative pathnames. The file named "." means "the current directory," and the file named ".." means "the parent directory". For example, ../csc152 could be a directory that is a sibling of your current working directory (i.e., csc152 could be a directory that has the same parent directory your current working directory does). Then ../csc152/hw/hw2.java could refer to a file farther down that branch of the tree. Similarly, ../../pix/grinnell could be a directory that is a cousin of your current directory in the file system tree.

The tilde character is also useful for specifying pathnames, but it works a little differently. Used alone, it specifies your home directory, so ~/csc201/hw is a short name for /home/username/csc201/hw. If you haven't used this before, try it now: print a "long" listing of the files in your home directory with the command

ls -l ~

Note that the command above contains two letter l's and no numbers.

Finally, ~username refers to the home directory belonging to username. Thus, you can print a listing of the files in my public_html directory with

ls -l ~coahranm/public_html

b. Next, let's take a look at the file attributes shown in the "long" listing. At one point, this command

ls -lF ~coahranm/public_html

would generate this listing.

total 40
drwxr-xr-x 4 coahranm mathfac 4096 2007-04-25 16:32 csc105/
drwxr-xr-x 6 coahranm mathfac 4096 2007-01-19 16:04 csc152/
drwxr-xr-x 4 coahranm mathfac 4096 2007-01-19 19:10 csc201/
drwxr-xr-x 4 coahranm mathfac 4096 2007-09-01 15:43 csc211/
drwxr-xr-x 4 coahranm mathfac 4096 2007-08-31 17:56 csc301/
-rw-r--r-- 1 coahranm mathfac 5808 2007-09-01 20:38 index.html
-rw-r--r-- 1 coahranm mathfac 5808 2007-08-31 19:02 index.html~
drwxr-xr-x 2 coahranm mathfac 4096 2008-01-02 15:55 mmc_files/

The listing specifies each file's type and permissions setting (in a string like drwxr-xr-x), as well as the username and groupname of the file's owner (coahranm and mathfac), and the file's size, last modified date and time, and file name.

Now produce a long listing of the files in your own home directory, and take a look at their attributes.

What is the groupname assigned to files that you have created?

You may belong to more than one group on the MathLAN. To see a list of all of your groups, use the command groups.

If you get really confused, you can ask for your own username with the command whoami. Try this out, just to be sure.

c. While we are poking around the Linux file system, take a look at the files in the root directory /. You should see directories with names like /bin, /home, /lib, and /usr.

Again, list the files in each of these directories. They contain many, many files, organized as follows.

/bin: These are the executable programs that comprise the GNU/Linux utilities. For example, there is an executable file here named ls that is run when you issue the command ls.

/home: You won't be surprised to hear that user accounts are stored in this directory.

/lib: This directory is the home of many libraries that can be used by programmers. For example, you should be able to find a file named libc-2.3.6.so here, that contains the "standard c library functions" we will use later in the course.

/usr: The name of this directory is pronounced "user", and it generally contains application programs, libraries, and other files that are not part of the GNU/Linux system (i.e., optional resources intended for and requested by users). For example, evince is located here under /usr/bin/evince.

We could also have learned where the acroread program is located with the command

which evince

Give that a try now.

We can also learn from which that (at least at present) an alternative pdf reader called xpdf is not available on the MathLAN system. How does which tell us this?

Obviously, which is a very useful command once you are familiar with the Linux tools you like to use. Next time you migrate to a new Linux network, you can use which to find the tools you like or ask the system administrator to install ones that are not currently available.

Exercise 4: The Manual

Linux includes an on-line help system, called the manual. There is a "man page" describing each Linux command and each C library function. Don't worry if you don't understand everything you see in a man page. They are often long and cryptic, but you can learn a lot without having to understand everything.

For example, try typing "man cat" to read about cat. You should see the command name and a quick synopsis of how to use the command. For example, the synopsis "cat [OPTION] [FILE] ..." tells you that cat (optionally) takes a file as it's input, and it also can take (optional) options as parameters. A list of the options follows the synopsis.

Find -n in the list of options for cat. What does it do? Try it out by issuing the command "cat -n ~/.emacs". Note that to close a man page, you type "q" for "quit."

Now check out the man page for the command less. You may notice that the man pages often have a certain geeky sense of humor about them.

You could also look up the man page for the C function sqrt. It contains a lot of useful information about the parameters, return type, and "include files" needed for a set of functions related to sqrt. You will find all this information very helpful very soon.

Another handy use for the man pages is finding commands or C functons when you don't remember, or never knew, their names. Suppose you wanted to find a function for computing the cube root: you could guess that such a function might exist, but you might not know its name. To print a list of man pages that include the word "cube" in the name or description fields, you could use "man -k cube". You could then follow that up by looking at specific man pages that appear in the list.

Of course, keyword searches are more useful for some commands than others: "man -k print" may reap you more results than you want to deal with!

 

History

January 2008 [Marge M. Coahran]

  • Created

August 2008 [Marge M. Coahran]

  • Revised

27 August 2010 [Samuel A. Rebelsky]

  • Reformatted
  • Added Preparation section.
  • Other minor changes.

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Aug 27 06:36:19 2010.
The source to the document was last modified on Fri Aug 27 06:36:17 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/linux-lab-1.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/linux-lab-1.pdf

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2010 Marge Coahran and Samuel A. Rebelsky. Please contact us for permission to reuse materials.