Computer Science 010

Lecture Notes 1

Getting Started with Unix

Unix originated in the early 1970s at Bell Labs. It built upon ideas developed in other operating systems to become an extremely popular operating system, dominating the market of multi-user, timesharing operating systems. Since its inception, it has evolved greatly and now comes in many variations. The variations are similar in more ways than they differ and basic functionality and user interface is similar across all variations. The most notable differences occur when doing systems programming or shell programming. The most recent variations of Unix include Linux, FreeBSD and several other free systems that were initially targeted at PC architectures, but which have grown to run on a variety of computer hardware. Commercial Unix versions are provided by vendors such as Sun Microsystems (Solaris), IBM (AIX), Compaq (Digital Unix), SGI (Irix), and Hewlett-Packard (HP/UX). Unix remains the most stable operating system in wide use, however it is still considered one of the least user-friendly operating systems around. Nevertheless, once you become familiar with Unix, it is very powerful and fast to use.

These notes (and this course) assume no prior knowledge of Unix, but they do assume, however, that you are comfortable using computers with a graphical user interface, such as the Macintosh operating system or Microsoft Windows. Upon completion of this lecture and the accompanying practice assignment, you should be able to log in, 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, know where to look for online help, and how to log out.

Logging In

Since Unix is a multi-user operating system, you must login to identify yourself so that Unix gives you appropriate access to your files. You can access the computer by sitting in front of it and logging in directly, or by connecting to it over the network with a program such as telnet or secure shell (ssh).

Console login

When you first sit down at our Unix machines (located in TCL 312) you will see a prompt that looks like this:
FreeBSD/i386 (pineywoods.cs.williams.edu) (ttyv0)
login:

The first line is informational, telling you the type of system this is (FreeBSD/i386), the hostname (pineywoods.cs.williams.edu), and the terminal you are connected to (ttyv0). To login you provide your login id and a password. Williams College has a convention to assign login ids for students. Your login is 2 digits identifying the year you will graduate followed by your initials (usually 3 initials, sometimes 2). If that does not result in a unique id, it will be followed by _# where # is the value of a counter to make your id unique. For example, if George W. Bush was a member of the class of 2002, his login would be 02gwb (or 02gwb_2 if there was already another user 02gwb). Your login id is set by the system administrator and is never changed. Enter your login at the login: prompt, then hit enter.

You will now be prompted for your password:

FreeBSD/i386 (pineywoods.cs.williams.edu) (ttyv0)
login: 02zzz
Password:
Your password will not display as you type for security reasons. Hit enter after your password and you should be logged in. If you typed either your login or your password incorrectly, you will receive an error message and will need to try again.

When you log in, you see the following prompt:

kde, Xwindows, fvwm2 or vanilla? (k|[x]|f|v)
Here, you will choose which window system to use. Unlike most systems, Unix has a window system (the X Window System) which is highly configurable. The KDE is a free variant of the Common Desktop Environment (CDE), Xwindows will choose a window manager known as twm, which is a very simple interface, and fvwm2 will choose a window manager much like twm but with a number of additional features. There are many other window managers available, and we will discuss customization of your X Windows environment later in the course. Finally, vanilla allows you to log in without a window system. Choose "x" to start a generic X Windows environment using twm, which is probably the most simplest of these window managers.

This screen contains 4 windows running programs. We will describe each of these windows shortly. For now, we will use the shell window to change your password. The shell window is the leftmost window.

Remote login

One of the most important strengths of Unix is that you don't need to be sitting in front of a particular computer to login in to it and to run programs. While we will use the FreeBSD systems in TCL 312, most of the time we will access them from another lab (where we all fit) and I expect many of you will access them from your own computers.

Remote access from the Mac Lab with eXodus

Much of our lab work will be done from the Macintosh systems in TCL 217a. Our preferred method to connect will be to start an eXodus session. When you sit down at the Mac, you should have a window called the "Launcher" on the desktop. Click the "Communications" button, then the first icon which appears in the window, labelled "eXodus launcher" (or perhaps abbreviated to "eXodus la"). This will start the eXodus software, which will allow us to display X Window System windows from remote systems onto the screens in the Mac lab.

You can then choose a remote Unix system to connect to from the "Clients" menu. The list includes both PCs running FreeBSD and Sun Workstations running Solaris. We'll use the FreeBSD systems, which are: pester, zebu, toto, faeroes, baggerbont, dulong, epirus, basuto, bearnaise, hinterwald, pineywoods, and watusi. When you choose one of these, you will be prompted for your Unix password, and then you should get a shell window.

Remote access using secure shell

To access the Unix lab from other systems, you should use a secure shell client. These are becoming more widely available, and are now available for just about any Unix (several implementations, including OpenSSH), Windows (putty, ttssh, among others), or Mac (NiftyTelnet and others) system. After making a connection using one of these programs and logging in to the system, you will have a Unix command line, as you had in the above examples.

Changing your password

Your initial password is provided by the system administrator and should be changed the first time you log in. A good password should be difficult for others to guess (your birthday or phone number or username are BAD choices) and difficult to find by trying every word in the dictionary. Guidelines for a good password include:

If you forget your password, the system administrator can give you a new password.

To change your password, use the passwd command at a command prompt (by default, your prompt looks like: ->). At the prompt, type passwd. Here is what you will see:

-> passwd
passwd: Changing password for your-login-id
Enter login(NIS) password:

Here you should enter your current password and press enter. For security reasons, your password will not be displayed on the screen when you type it. Next you will see:

New password:

Type in the new password that you want. Again, it will not appear on the screen. Now you will see:

Re-enter new password:

Type the same new password again. This ensures that you typed correctly since you could not see the characters you typed. If you type the password the same both times, you will see the message:

NIS passwd/attributes changed on bull

If the two attempts at typing your new password differ, you will see:

passwd(SYSTEM): They don't match; try again.
New password:

Command Lines

The most obvious difference between Unix and Macintosh/Windows is the user interface. Whereas Macintosh/Windows have primarily graphical user interfaces, Unix has a primarily textual user interface. Thus, to accomplish most things in Unix, one types commands to a "shell" program. The passwd command you used above was one example of this. 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. Once you get to know emacs, you will be able to use many of the emacs commands to help edit your command line. You can also use the up arrow key to recall previous commands typed in the same command shell.

Many Unix commands may also be performed using the X Window Interface, but we will often concentrate on the command line interface.

File System

The file system is organized into directories and files. A directory is analogous to a Macintosh or Windows folder. A directory may contain files and/or other directories. Here are some common 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

rm file1

Remove the file named file1

more file1

Display the contents of a file

lpr file1

Print the file named file1

One thing you will notice with Unix is that "no news is good news". Most Unix commands produce no output when they are successful. If a command fails, you will receive an error message.

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

Display the next screenful

b

Display the previous screenful

q

Quit more, returning to a shell prompt.

Instead of more you may wish to use less, which is essentially a better more. Yes, that's right, less is a better more.

Directories are containers for files. When you use ls to list your files, directories will be listed with / at the end of their names. By default, your commands are interpreted with respect to your "current" directory. 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

cd dir1

Make dir1 be the current directory

pwd

Display the name of the current working directory

Filenames usually consist only of letters, numbers, and '.', but Unix filenames can contain any character. A filename is typically divided into a descriptive name and an extension, separated by '.'. Extensions are purely by convention but typically indicate the type of file. For example, "myfile.txt" would be a text file, while "myfile.c" would contain a C program. Extensions are generally optional although some programs, such as the 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 assumed relative names that refer simply 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.

Each user also has a home directory. This is the directory that is the user's current directory when the user initially logs in. 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.

Basic X Windows

This section applies primarily to the environment you will get when logged in on the console of the FreeBSD systems in TCL 312.

The user interface to Unix is the X Window system. This allows you to have multiple windows each running a separate program. Each typically has a title bar with some common functionality. When you login, you will see 4 X windows, 3 of which are typical X windows while the 4th is simply a picture. The main window that you interact with is the largest and has the name of the current directory in the title bar. The other windows are:

To use a window, the window must have the user's focus. To get focus, you simply slide the mouse into the window (no need to click on it). If the mouse slides out of the window, the window loses focus. All keyboard and mouse input goes to the window in focus.

Let's examine the title bar of a window. The leftmost symbol is a circle. If you click on it, it closes the window. The window remains in the twm icon list. Next to its name in the list is a box with an X in it, indicating that the window is closed. Click on the entry to redisplay the window.

The next symbol will display a window operations menu if you click on it. This menu contains a number of commands, the most useful being:

Refresh

Redraws the window

Full zoom

Makes the window the size of the entire screen

Delete Window

Removes the window and its icon from the twm icon list

Kill Client

Kills the application running in the window, deleting all windows used by the application.

Vertical zoom

Makes the window the full height of the screen

Resize

Allows you to drag the window borders to change the window size

Lower

Put this window to the bottom, revealing the windows behind it

Raise

Bring this window to the top

Iconify

Same as the circle at the left of the title bar

Move

Allows you to drag the window

Next in the title bar is the window's title. The following symbol allows you to resize the window in the same manner as the resize command in the window menu. The final symbol lets you do a vertical zoom, the same as the command in the window menu. If you click in the title bar outside any of the symbols, the buttons have the following behavior:

Left

Raise if not on top; Lower if already on top. Drag to move the window.

Middle

Resize

Right

Brings up the window operations menu

The area that is outside all windows is called the root. The mouse buttons behave differently in the root menu. In this case, each button brings up a menu. The left button brings up the basic menu with the following commands being most useful:

Emacs

Creates a new window running emacs

X terminal

Creates a new window running a Unix shell

Shutdown X

Use this to logout

Clicking the middle mouse button in the root gives you another menu that creates windows running specific applications. The right button gives you the same window operations menu that we have seen previously.

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, use the commands "emacs &". This will open a new window on your display. Initially, you will just see the outline for the window. When you move your mouse, this outline will move around. Pick a location for the window and hit the left mouse button to place the window. The window will then give you a complete display and will be running the Emacs text editor which will look similar to that shown below:

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. Over the month, your repertoire of commands will grow and will move away from menus and toward keyboard commands. For now, let's create a new file. To do this, point at "Files" and click the left mouse button. Then select "Open file...". 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 has been changed since it was last saved. If it says --:%%, it means that you cannot modify the file. After the filename is the "mode" that you are editing in. This should say "(Fundamental)". We will talk about modes later this month. 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 the status line changes. You can move the cursor around using the arrow keys. Use the scrollbar to move through the file. Use the "Save Buffer" 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. Here are the keyboard sequences for the most common Emacs commands:

C-f

Forward one character

C-b

Backward one character

C-n

Next line

C-p

Previous line

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

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 long run in terms of speed of using the editor.

 

Getting Help

One of the most important things to learn this semester is how to get help online so that you can find things yourself. There are two tools of particular interest: xman and the info package within Emacs.

xman is already started when you log in on the console of the FreeBSD systems in TCL 312, but will need to be started with the command xman & at your command prompt if you are logged in elsewhere. xman provides a graphical user interface to online Unix manuals. The manual is divided into several sections. The most useful section for users is section 1 on user commands. C programmers will be interested in sections 2 and 3 which describe library routines provided by Unix that can be called from C programs. If you click on the Sections menu, a list of sections will appear. Selection Section 1, user commands, and you will see a complete table of contents of all Unix commands. If you click on one entry in the window, you will now see the manual page for that command. This is very useful when you want to know exactly how to use a command or exactly what arguments it takes. You will be reading a lot of man pages over the month so you will become more familiar with doing this as the month progresses.

Using xman in this way is only useful if you know exactly what command you are looking for. xman also supplies a keyword-based search. To use it, pulldown the Options menu. Select Search. Type in a keyword and then click on the apropos button. You will now see a list of every entry in the manual with that keyword in its title. Remember that you will find section 1 most interesting so it is safe to ignore those entries that do not have (1) after the command name since those are not in section 1 and thus are not user commands.

xman is most useful for the simplest Unix commands. More complex tools are usually better explained with the info package within Emacs. To start info, enter Emacs and then use "C-h i" to begin info. The first screen shows a list of programs for which help is available. Move the cursor to the program you want more help on using the arrows and then use the carriage return to select the entry. This will now give you help in a hypertext style for the program. You may find underlined links that you can move the cursor to and then hit carriage return to select them. You can also navigate the hypertext selection using the following keys:

l

Last item visited (like Back in Netscape)

u

Up to parent

n

Next sibling

p

Previous sibling

Many people prefer the text-based manual system, called man. You can get text-based information about any of the commands or system calls by typing man followed by the name of the command or system call. You can then page through the manual information. To learn more about the manual, you can read the manual about the manual by entering man man at the command prompt.

Mail

You have a mail account on your Unix machine, but it would make the most sense for you to read all of your mail from one email account. I assume that you each have an account elsewhere that you have been using to send and received mail. You should continue to use that account for mail and simply have your mail forwarded to that machine. To forward mail, you will create a file called .forward. The only thing that this file contains is the address to which you want your email forwarded. For example, the file might contain:

jcool@williams.edu

After creating this file, send mail to your Unix account to verify that the forwarding is set up properly. It should be sent directly to your Williams account even though you use your CS address. Your CS email address is your login name followed by @cs.williams.edu, such as jcool@cs.williams.edu.

Logging Out

To log out from an indivudual command shell, type exit or logout at the prompt.

To log out from an X Window console session, point the cursor at the root (outside all X windows) and click the mouse button. Select Shutdown X and then slide the mouse right until you see "select to confirm". Let go of the mouse and you will be logged out.