The Networking Mambo

Chapter 9: Unix

What's so special about Unix? Getting around with files and directories
What is Unix? Manipulating files
Interacting with Unix Other useful commands
The command line interpreters Commands for networking
Some basic Unix guidelines Job control, redirection, and pipes
Unix command summary Email
Logging in and out Editor basics
What happens when you login? Review exercises
Typing commands, command syntax Glossary
Files and directories

This chapter will give an overview of Unix, and an introduction to how to utilize Unix effectively to get things done. In order to become an expert at Unix, you need to spend lots of time with it. You will find that you can't do everything as easily as with a microcomputer using Windows or MacOS. For example, there is usually no graphical interface to help you. But you will also find there are some things that Unix can do that most other operating systems can't do as well, such as linking together a series of commands using Unix pipes.

What's so special about Unix?

Unix was developed in the early 1970s at AT&T's Bell Laboratories. One of the goals for Unix was for it to handle many different programs at the same time (called "multitasking"). This feature, when combined with the strong emphasis that Unix computers place on networking, have helped to make it very widely used today.

Unix will run on almost any type of computer, from the smallest microcomputer (including computers that could otherwise be running Windows or MacOS) to the largest mainframe. There are many different types of Unix, because AT&T licensed the technology to 3rd parties. Unix has a somewhat complicated family tree, because a group at the University of California at Berkeley made their own version of Unix independently of the AT&T version. Today, the Berkeley version is called "BSD" (for Berkeley Standard Distribution), and the AT&T version is called System V (or sometimes just SysV).

In the business world, Unix is often found on the computers that manage inventory, accounts, etc. Unix is also the choice of many researchers, from mathematicians to engineers. In the world of the library, Unix is the basis of vendor systems of medium- to large-scale library card catalogs.

Most Internet tools originated on Unix computers, as well. Lots of the software that keeps the Internet running (such as DNS software and network news servers) started on Unix. Internet servers such as FTP servers, Gopher servers, and WorldWideWeb servers all started on Unix computers.

Today, there is no doubt that Unix will be with us for a long time. Although computer magazines like to speculate about whether other types of operating systems, such as Microsoft's Windows NT, will replace Unix, this doesn't seem likely. Unix has years of head start over NT, and, more importantly, is able to run on virtually any type of computer -- and is the operating system of choice for computers from vendors such as Sun, IBM, HP, SGI, and Digital.

In addition to being sold as part of vendor systems, Unix is also available either free or at a low cost. Free versions of Unix include Linux and FreeBSD, which will run on just about any PC-compatible microcomputer. Many home enthusiasts (and many large businesses, too) have opted to get the power of Unix, but to run Unix on generic PC systems, which are often cheaper than name-brand workstations such as those from Sun and IBM.

What is Unix?

Unix is an operating system. An operating system is a collection of programs that manage system resources, interact with users' programs, and interact with the outside world. Because Unix is multitasking, it can handle running many programs at the same time.

Unix doesn't consist of just one program. But the "kernel" is the part of Unix that is always running -- it gets loaded into the computer's memory as soon as the computer is turned on, and is always active. The kernel can't take care of things on its own, though. It needs the help of dozens or hundreds of other programs accomplish its tasks. For example, a Unix computer that is also an Internet Web server will need to run programs that tell when an incoming Web request arrives, then handle that request, and (most likely) take care of system logs and other tracking for the requests.

Unix also needs to worry about whether disk drives are working properly, whether the network is working properly, and insuring that the various programs that are running are not causing errors on the rest of the system. Unix acts as sort of a referee or taskmaster, but is also responsible for quality control and accounting!

Interacting with Unix

You might be used to a particular type of operating system, such as Windows or MacOS, having a particular interface or way of interacting with the computer. With Unix, though, there are many different ways that people can interact with the computer.

All Unix computers can interact with their users by a command line interface. Using a command line interface, what you type is read by the computer, then any output is sent back to you. All the interaction is by plain text -- no graphics, windows, sounds, etc.

Many Unix computers can also interact graphically, using a windowing environment. The most common windowing environment is called "X." X, or X-Windows, was developed to give Unix users a way of using their mouse and multiple windows to point-and-click through a Unix session. Of course, one of the windows can also have a command line interpreter! Many variations on X exist, since most vendors have chosen to give their computers a unique look at feel with a customized interface.

This chapter will focus on the command line interpreters, since those are always present. Once you have mastered command line interpreters for Unix, you will have no problem adapting to the graphical interface when you encounter one.

The command line interpreters

Command line interpreters are Unix programs that interact with users through a pseudo-terminal. "Pseudo-terminal" is just fancy language to point out that, to the Unix computer, it's as though you were connected to the computer using a direct hardwire connection. In fact, though, you are much more likely to be connected using a telnet program.
They're called interpreters because they need to interpret what you type. As you will see below, there is plenty of opportunity for ambiguity. They also handle sending output from the programs you run back to your terminal (that is, back to you over your telnet connection).

Unix command line interpreters are usually called "shells." A Unix shell is what we've been talking about: a program that acts as an interface between the Unix operating system and users in the real world. As you might have guessed by now, there is a choice of which shell to use to interact with Unix. Your specific choice is dependent on whoever runs the computer -- not all choices are going to be available, and you might be blocked from choosing a shell yourself.

Some of the most frequently found shells are:

There are others as well, including BASH (the Bourne-Again Shell) and zsh. Basically, a new Unix user should choose csh or a variant to learn Unix. People writing programs using the shell language might want to switch to sh or a variant. These shell language programs are also called "shell scripts." Most operating systems have something like shell scripts available. In DOS, there are batch files; in Digital's VMS, there are command files; on IBM VM mainframes, there is REXX. Shell scripts are very useful for putting together a series of shell commands that can be stored in a file and run as a program. We're not going to talk about shell scripts here, though, as that's a more advanced topic.

Some basic Unix guidelines

Here are three things to remember about Unix:

Unix command summary

1. Logging in and out

When first connecting to a Unix computer, you will be prompted for your username and password. The username is case sensitive, and is usually 8 characters or less. This is how you are identified to the computer.

Your password is also case sensitive, and will not show up when you type it. Most Unix systems only support password up to 8 characters, but newer systems allow for passwords up to 128 characters.

Choose a good password! It should:

A sample Unix login screen. A welcome message is followed by a prompt for a username ("login:") and password. The password is not echoed to the screen. After the password is accepted, a prompt is given for the type of network terminal in use. Finally, the shell prompt is displayed.

Welcome to GAIA, in the Graduate School of Library and Information Science at the University of Illinois at Urbana-Champaign.

login: gbnewby
Password:

TERM = (vt100)
%

To logout, you can type "exit" or (usually), "logout." You can also use the Unix shorthand for "end-of-input," which is control-D. For control-D, hold down the control key, then type "D."

2. What happens when you login

Different shells have different prompts. sh uses "$," while csh uses "%." But a particular computer's administrator might have set the prompt to be something else.

When you login, some special initialization files are run. (This is just like the autoexec.bat and config.sys files used in DOS.) The exact files and their order varies somewhat with different brands of Unix, but usually goes something like this:

Notice there are two groups: the login initialization, and the shell initialization. Remember that in Unix, you can choose to use different shells. Your own personal login initialization file is called ".login" (that's a period, followed by the word login. Unix filenames can start with periods).

Your personal shell initialization file is named according to the shell. For csh and variants, it's ".cshrc." For sh and variants, it's ".profile."

Your .login and .cshrc or .profile files are in your home directory when you login. You can edit them, if you would like to customize how the shell works for you. These files are, in fact, Unix shell scripts (as mentioned earlier).

3. Typing commands, command syntax

The things you type at the shell prompt will be interpreted to the shell, then (in most cases) passed on to the Unix operating system to be run. These commands follow a pattern that you should get used to:

for example, the ls command is used to get a file listing. One use of the ls command to get information about a file called "asdf" might be:

in that case, ls is the command, -l is the option, and asdf is the argument. Commands don't all need to have options and arguments, of course. And, the list of allowed options and arguments is different for each command.

In our ls example, the -l asks for a "long" directory output, with details such as the file's size and creation date. And, if we were to leave off the asdf argument, we would get information about all files, not just the one called asdf.

What happens when a command is successful? You get your prompt back. With the ls command, it's obvious that things worked well because you will get a listing of files before your prompt. But with other commands, you might not get any information back, just your prompt. If we wanted to print the asdf file, we might use a command like this:

lpr asdf

Rather than getting some informative message, such as "your file is being printed," we'll just get our prompt back!

There are some typing tricks for getting around in the shell. Most of these will work in csh and variants:

Other options for command-line editing are available, but might not work for all shells. In tcsh, for example, you can use Emacs-style keys to return to previous lines. Emacs is a popular text processing system for Unix, and utilizes commands like ^p (to the previous line), ^a (to the start of the current line), ^e (to the end of the current line), and ^b (back one character).

In sh, you might be able to use command based on the vi editor. The ESCAPE key gets you into edit mode, then you can use the k key to back up a line, w to go forward one word, x to delete a character, and i to get into input mode (to type something new). These ways of interacting with the shell take a little while to learn, but can save you lots of time with typing and re-typing commands.

Every username on a Unix system has a home directory. Your username has its own private space where you can create, rename, delete, and otherwise manipulate files. You can look at files in other areas of the system, but usually you can't change or delete any files except those you own. Other usernames have their own directories, too. Most likely, the system security will be set so that you can't see other people's files, and they won't be able to see yours.

The system manager's username (typically, "root") is exempt from these limitations. Luckily, most Unix system managers are way too busy keeping things running to be concerned much with your own files and directories, and many organizations have strict rules about keeping your files private.

You are probably already familiar with files and directories from other types of computer systems. A file is simply a designated storage area on a system containing data. File are kept on non-volatile storage devices, such as disk drives or CD-ROMs. (Non-volatile means that they don't go away when the computer is turned off.) A file can contain any type of data -- a program, some text that you typed in, an email message, a database, etc.

In Unix, files have a variety of characteristics, including:

A directory is actually a special type of file, used to keep track of other files. Commands such as ls are used to find out what files are associated with a particular directory. Directories have the same set of characteristics that files have.

Every Unix system has its own directory structure. A forward slash, /, is used to identify sub-directories (remember that directories are just special files, and directories are used to store file information -- so, a directory can contain other directories!).

Your own directory might have a name like this:

where "username" is your own username. My directory might /usr/people/gbnewby. Different Unix systems have different directory structures, and the system administrator can make decisions about where to put different usernames as well -- so don't expect your directory will always be in the same place as mine!

Most Unix shells (including all those compatible with csh) have a shorthand way to refer to your home directory. They use a tilde, or a tilde followed by your username. So, if my username is gbnewby, the following all refer to my home directory:

The tilde is shorthand for my directory, but I can also use it as shorthand for another username's home directory: ~greg would be shorthand for the home directory of the person with username greg.

Appending a forward slash is optional, unless you want to refer to a specific file. For example, to refer to a file called .login in my home directory, I could use any of the following:

In addition, if my current working directory is my home directory, I could just specify:

where the period-slash (./) is shorthand for my current directory. (Actually, just the period is shorthand for my current directory....the slash is used to separate the period from the filename that follows.)

If I wanted to see who else has a username in /usr/people, I could type "ls .." where the double-period refers to the parent directory of my current directory. That is, one level up.

At the top of all the directories on every Unix system is a directory called the root directory. This is called "/" -- it's the top directory; every other directory on the Unix system is a sub-directory of /.

In practice, Unix systems might have more than one disk drive attached. And, the disk drives might be divided into logical filesystems (this makes it easier to divide the files on the Unix system so that different groups of files can have different disk quotas or different purposes). But they all fall under /.

5. Getting around with files and directories

Try using these commands to get information about directories. These are just examples. Mix and match the options and arguments.

6. Manipulating files

A set of commands are available for looking at files. Another set is used for copying, renaming, and removing files.

You probably have a file called .login or .cshrc that you can use these commands for. Also, every Unix system has a file called /etc/passwd. Beyond that, it's hard to know which files will exist on your particular system, so you will need to use "ls" to look around.

Commands to change or manipulate filenames include:

On Unix systems, there is no good way to "recover" a file as you do on a PC system. This is because the disk space that you file took will be re-allocated for other files. So, you need to be careful about removing files! Most Unix administrators will make backup tapes to take a "snapshot" of all the files on the system, but you shouldn't rely on such tapes. Apart from the difficulty of convincing your administrator that your file is worth looking for on a tape, the tapes might not be made all that frequently.

You can make your own directories. Like all the file creation or renaming commands, these will only work in your own home directory (or another directory you have specifically been given access to).

7. Other useful commands

man: The Unix online manual. This is typically not very well-written, but is useful if you want to know the syntax or details of a command.

grep: search a file for a string of text. egrep, agrep, and fgrep are variants.

grep gbnewby /etc/passwd: will look for my username's entry in the /etc/passwd file

w, also who: See who is logged on.

ps: process status, gets a listing of the programs you are running, including your Unix shell. "ps -aux" will get detailed information about all processes on a BSD system; "ps -elf" will do the same on a SysV system.

kill: Used to stop/kill a program. For example, if you got hung up from your terminal session, you might need kill to stop your email program from the hung-up session. Used with ps to get the process number to kill.

finger: Get information about a particular user on the system. "finger gbnewby" would give information about the gbnewby username. This also works over the network: finger gbnewby@uiuc.edu will give information from a remote system (uiuc.edu in this case).

8. Commands for networking

Unix clients for telnet, FTP, and other Internet programs are available. If you are familiar with a point-and-click client for FTP, or typically access FTP through your Web browser, you should experiment with the Unix FTP client.

Many other commands may be available. These are discussed in other places in The Networking Mambo. Commands include a text-based Web browser ("lynx"), Gopher, and various network news readers.

9. Job control, redirection, and pipes

One of the greatest features of Unix is job control. Through job control, you will be able to have one program running, while interacting with another program. One obvious application of this for the Internet expert is to have use email, while reading network news at the same time.

This is not the same as having multiple windows, each with their own program, as you would with Windows or MacOS. It's more like having multiple programs running, but only one is really getting input from you, while the others are paused or sleeping.

Redirection is the powerful capacity of Unix to send the output of a command to a file, or to specify a file that the command should take input from.

A Unix pipe is a way of sending output of one command to another command. Here are some examples of all of these features:

This is all a little complicated. Job control, redirection, and pipes are all related, but can also occur separately. A concrete example might help:

The example shows that it's not necessary to exit from one program to start another one, and also lets us see how, in an average work session, you might want to use several different commands (in this case, pine and finger) to help with the main work (editing an assignment).

10. Email

Unix systems typically come with a "mail" command for reading and sending electronic mail. It is not fancy, but can do all the basics: send, receive, refile (to new mail files), extract (to a new file), forward, and edit email messages.

These days, many Unix system administrators add more fully-functional or easier-to-use email systems. These include elm, pine (pine was based on elm), MH (the Message Handler), and others.

You can choose whichever system you would like. In all cases, the mail program you select will be able to find your new incoming mail, and will know how to send messages out.

10. Editor basics

Unix systems all come with an editor called "vi." vi is short for "visual," because it's the visual (that is, screen-oriented) interface to the "ex" editor. These days, we are used to having standard terminal types, such as vt100, to help us to have screen-oriented interaction with our computers. This is what enables us to move our cursor around the screen, get highlighted or underlined text, and other "fancy" stuff.

vi is always there, but it's not very easy to learn. I'll present the very basics of two common editors, vi and emacs. It's likely, though, that your friendly Unix administrator has chosen an easier editor, such as pico. Unlike vi and emacs, pico is very easy to learn (it's actually part of the pine email system, which is also very easy to learn). Just type "pico filename" to start editing a file, and you will see most of the commands you need to know right on your screen! For newcomers to Unix, pine and pico (or some of the other easy-to-learn email systems and editors) can make life much simpler.

Fundamentals of vi

First, understand that vi has two modes, input mode and edit mode. You use input mode to type in new text. The new text will go wherever your cursor was when you started input mode. To get to input mode, type an "i" (lower case). You can also use an "a" which get into append mode, where what you type goes in after the cursor, not at the cursor. This is the way to add to the end of a line with vi!

Edit mode is for moving around in a file, replacing text, giving vi commands, and everything else. To get from input to edit mode, hit the ESCAPE key once. (If you're already in edit mode, you'll get a beep from your terminal. This is OK -- it's actually a great way to confirm you're in edit mode.)

Moving around in vi (in edit mode) is based on these keys:

Basic editing includes keys such as these:

By using the colon, you can get a prompt at the bottom of the screen for more extended commands:

Except for the commands with the colon, the commands you type will not echo to the screen. This can be hard to understand at first...if you want to delete 10 lines, you would type 10dd, but none of the characters would appear on your screen. Typical of Unix, though, the ten lines will obediently disappear, unless you made a typo!

Stuck in vi-land? There are many times on a Unix system when you can suddenly find yourself in vi. Two common examples are the chfn command, that lets you edit your personal information, and typing "e" while in "more." If you get into vi and want out, just type ESCAPE, then :q! and you'll be free!

Fundamentals of Emacs

Emacs is much more than a text editor -- it's a complex environment that can do everything from automatically indenting your program code to being a front-end to an online help system. It's not included as part of most Unix systems, but enough people like Emacs that it's a likely component for the system administrator to add.

Emacs lets you use your arrow keys to get around, or command sequences. Emacs commands come in two forms: control sequences (holding down the control key, then pressing one or more other keys) and meta sequences ("meta" is Emacs lingo for the ESCAPE key). Meta sequences consist of hitting the ESCAPE key, then hitting one or more characters.

Remember that with control, you hold the control key while you type other keys (this is a bit like using the SHIFT key on your keyboard). But with the ESCAPE key, you hit ESCAPE, and then press any other keys.

To move around in Emacs:

Since there is no "edit" mode, as with vi, you can always make changes (or do anything else) in Emacs.

Your backspace key might not work correctly with Emacs. Emacs is picky about ^h being used for help, not for backspace, yet many Unix systems make use of ^h for backspace. So, make sure you use ^b to move back, and ^d to delete a character.


Review Exercises

1. On a Unix system you have access to, determine the following:

2. Utilize the "man" command to understand the syntax for commands such as "ps" and "finger."  Experiment with these and other commands.

3. If you are comfortable with Unix, try "man csh" or "man sh" to get more detailed information about the Unix shell you use. What new things did you learn about?


GLOSSARY OF TERMS

Directory: A special file used to keep track of a listing of files. Also, every Unix username has a home directory, which is usually set to the current directory when you login.

Emacs: A sophisticated text editor often found on Unix systems. It is also available for microcomputers running DOS, Windows, or MacOS.

File: A group of data stored on a non-volatiile device, such as a disk drive. Files can have characteristics such as size, location on the disk (on Unix, by using an inode, or file number), owner, and dates of last change, access, and original creation.

Mail: mail is the standard Unix email program.

pipe: Also |. Used to send the output of one command as the input to another.

Pico: A simple editor associated with the Pine email system.

Pine: An easy to use email system developed at the University of Washington.

Program: Also application, job, or process. A set of commands that a computer is able to follow.

Server: A computer that is willing to provide data to another computer using a particular protocol, such as HTTP (the HyperText Transport Protocol, used for the WorldWideWeb). Any Unix computer can act as a server.

Shell: Also command line interpreter. A program that acts as the interface between a user and the operating system. It usually communicates via a terminal.

Unix: A multitasking operating system that is available from many vendors for many types of computers.

Vi: A standard Unix text editor. Difficult to learn.