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.
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.
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!
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.
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:
csh (or C-Shell): This is the most common shell for people to use. It has some nice features that makes it easier to use than other shells, but is also not as good for writing Unix programs as sh.
sh (the Bourne Shell): sh is always available on Unix computers, and it's usually the case that some of the system programs that help keep the computer running are written using sh. sh is not great as a user interface since it doesn't have some of the features that csh has, but it has some better features for writing programs.
ksh (the K-Shell): An enhanced version of sh.
tcsh: An enhanced version of csh.
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.
Here are three things to remember about Unix:
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:
|
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."
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:
a. A system-wide login initialization file is run, if one exists
b. Your own personal login initialization file is run, if one exists
c. A system-wide shell initialization file is run, if one exists
d. Your own personal shell initialization file is run, if one exists
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).
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:
commandname options arguments
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:
ls -l asdf
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:
^U (control-U): erase what you have typed until the start of the line. This is good if you made a mistake at the start of a command line, and notice it before you press return.
^W (control-W): erases just the last word you have typed
!! (two exclamation points; referred to as "bangs." So, this is "bang-bang"): re-do the last command you typed.
!x: re-do the last command you typed that started with "x." This is good for commands you want to avoid re-typing. For example, "!m" might re-execute your last "mail" command. You can provide more than one letter. !m might be confused for the "man" command or the "mail" command, so you might try !mai to make sure you get the right command.
^old^new: re-do the last command, but replace the first occurrence of old with new. This is good for correcting typing mistakes. For example:
% man grpe
man: no entry for "grpe"
% ^pe^ep
man grep
Instead of typing "man grep" (to get a manual entry for the Unix "grep" command), I made a typo. I could save myself a few keystrokes by doing command-line editing with the ^ command above, to get the right command.
^C: cancel the current command. Useful if you don't know how to get out of something you're stuck in, but it's not guaranteed to work for every command.
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.
|
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:
/usr/people/username
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:
/usr/people/gbnewby
~
~/
~gbnewby
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:
/usr/people/gbnewby/.login ~/.login ~gbnewby/.login
In addition, if my current working directory is my home directory, I could just specify:
.login
or ./.login
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 /.
Try using these commands to get information about directories. These are just examples. Mix and match the options and arguments.
ls: lists the files in your current
directory
ls -a: the -a option lists all files,
including those that start with a period
ls -l: gives a long listing, including
file attributes such as size and their date
ls /: look at the / directory...you
can look at other directories beneath it, too
ls -CF: puts a slash after sub-directories,
to differentiate from other files
pwd: prints your current directory
cd: changes directory. Followed by
a directory name to change to:
cd /: change to the root directory
cd ~: change to your home directory
(you can also just type "cd" with no argument). This is your
pair of "ruby slippers" that will always get you home!
cd ..: changes to the parent of the
current directory
A set of commands are available for looking at files. Another set is used for copying, renaming, and removing files.
more filename: Use the more command to view the contents of a file, one screen at a time.
To get around in more:
q will quit
spacebar will bring you the next
screen of the file
? will get help
b will go Back to the prior screen
(usually)
/something will look forwards for
"something" in the file (case sensitive)
100s skips 100 lines
100spacebar skips 100 screenfuls
less filename: less is a more capable version of more. The main advantage is that it can always go backwards, while more frequently cannot. It is not a standard part of all Unix systems, however.
cat filename: Displays a file without any page breaks. Be careful with long files!
head filename: Displays the first 10 lines of a file. head -20 filename will display the first 20 lines.
tail filename: Like "head" but displays the end of a file.
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:
cp .login testfile: will copy
.login to testfile
mv testfile test2: will rename (move)
testfile to test2
rm test2: will remove (permanently
delete) test2
| 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).
mkdir testdir: creates a new sub-directory
called "testdir"
rmdir testdir: removes the sub-directory,
provided it's empty (that is, that it doesn't contain any files)
mv testdir realdir: renames a sub-directory
cp .login realdir: copies a file,
.login, to the realdir sub-directory. The copied file will still be called
.login
cp .login realdir/testfile: as above,
but the copied will be called testfile.
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.
man man: get the manual entry for the "man" command
man -k file: get a listing of all manual pages with the keyword "file" in the header
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).
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.
telnet marvel.loc.gov: telnet
to a particular Internet computer
telnet galaxy.bvu.edu 7777: telnet
to a specific port on a computer
ftp uiarchive.cso.uiuc.edu: an FTP
session to a computer
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.
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:
ls > a: redirect the output
of the ls command to a file called "a" (note: this will erase
anything in "a" if it already existed, or create "a"
if it did not yet exist)
ls >> a: redirect the output
of the ls command, and append it to "a"
grep -i newby /etc/passwd > b:
find all (case insensitive) occurrences of "newby" in /etc/passwd,
and write them to "b"
ps -ef | more: pipe the output of
the "ps" command to the "more" program for viewing
one screenful at a time.
ps -ef | sort | more: Use the "sort"
command, too.
ps -ef | sort > a: Put the sorted
output in a file called "a"
ps -ef | sort > a &: Run the
above command in the background, so you can look at it later.
jobs: list all the processes, both
foreground and background
fg: put the last-backgrounded job
in the foreground
fg %1: put the job with number 1
(from the "jobs" command) to the foreground
^Z: suspend the current job
bg: put the suspended job in the
background
This is all a little complicated. Job control, redirection, and pipes are all related, but can also occur separately. A concrete example might help:
% pico assignment1.txt (start
the pico editor to write an assignment
^Z (stops pico)
% pine (start the pine command to
look for some email about the assignment. pico is suspended, in the background)
^Z (stops pine. Both pico and pine
are now suspended)
% finger gbnewby (use the finger
command to see if somebody is logged in)
% jobs (get a listing of what programs
are running. pico will be #1, pine is #2)
% fg %2 (get back into pine. Send
an email message, then exit pine normally)
% fg (get back into pico, to work
on the assignment some more)
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).
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.
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.
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:
h, j, k, and l: move left, down,
up ,and right respectively, by one character. (Old timers will remember
that these keys used to be how computer video games such as Hunt the Wumpus
were played.)
w, b: go forward or backwards one
word
^, $: to the start or end of a
line
10G: goto line 10; just G goes
to the end of the file
Basic editing includes keys such as these:
x: delete the character under
the cursor
10x deletes the next 10 characters
dw: delete the word under the cursor
dd: delete a line 10dd
deletes 10 lines
By using the colon, you can get a prompt at the bottom of the screen for more extended commands:
:s/i/e/ will change all i's to
e's on the current line
:%s/i/e/ will change all i's to e's
throughout the file
:r asdf will read in the file "asdf"
to the current file
:undo will reverse/undo the last
thing you did
:w writes the file you are editing
to disk
:q quits vi
:wq writes, then quits
:q! quits without saving any changes
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!
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:
^n to the next line
^p to the previous line
^a to the start of the current line
^e to the end of the current line
M-w to the next word (ESCAPE-w)
M-b back to the previous word
M-< to the start of the file
M-> to the end of the file
Since there is no "edit" mode, as with vi, you can always make changes (or do anything else) in Emacs.
^d deletes the character under
the cursor
^k deletes until the end of the current
line
^y undoes the last change
^w writes out the file
^x-^c writes out the file, then exits
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.
1. On a Unix system you have access to, determine the following:
Your username
Your home directory
Your shell
What files are in your directory
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?
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.