BUY THIS BOOK

Safari Books Online

What is this?

Looking to Reprint this content?


Learning the UNIX Operating System
Learning the UNIX Operating System, Fourth Edition

By Grace Todino-Gonguet, John Strang, Jerry Peek

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Getting Started
Before you can start using UNIX, your system administrator has to set up a UNIX account for you. Think of this account as your office—it's your place in the UNIX environment. Other users may also be at work on the same system. At many sites, there will be a whole network of UNIX computers. So in addition to knowing your account name, you may also need to know the hostname (name) of the computer that has your account.
Each user communicates with the computer from a terminal or a window. To get into the UNIX environment, you first connect to the UNIX computer. (You may have a terminal that's already connected to the computer.) Next, you start a session by logging in to your UNIX account. Logging in does two things: it identifies which user is in a session, and it tells the computer that you're ready to start working. When you've finished working, you log out—and, if necessary, disconnect from the UNIX computer.
If you turn on your terminal and see a message from the UNIX computer that looks something like this:
login:
you can probably skip ahead to Section 1.1.2 later in this chapter. Otherwise, browse through the next few sections and find the one that applies to you. (We can't cover every user's situation exactly. If none of these suggestions helps you enough, ask another UNIX user or your system administrator.)

Section 1.1.1.1: Connecting from another operating system

If you're using a personal computer to connect to the UNIX system, you'll probably need to start a terminal emulation program. Some common programs are procomm, qmodem, kermit, minicom, and telnet. (There are lots of others.)
If you start the program and get a UNIX "login:" prompt, you're ready to log in. But if your screen stays blank or you get another message that you don't understand, check with another user or your system administrator for help.

Section 1.1.1.2: Connecting with a data switch

Your office may have a data switch, a port contender, or another system that allows you to select which computer you will connect to. Like a telephone switchboard, this connects your terminal to one of a number of computers. Enter your computer's hostname or code number at the prompt—or choose from the menu of hosts.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Working in the UNIX Environment
Before you can start using UNIX, your system administrator has to set up a UNIX account for you. Think of this account as your office—it's your place in the UNIX environment. Other users may also be at work on the same system. At many sites, there will be a whole network of UNIX computers. So in addition to knowing your account name, you may also need to know the hostname (name) of the computer that has your account.
Each user communicates with the computer from a terminal or a window. To get into the UNIX environment, you first connect to the UNIX computer. (You may have a terminal that's already connected to the computer.) Next, you start a session by logging in to your UNIX account. Logging in does two things: it identifies which user is in a session, and it tells the computer that you're ready to start working. When you've finished working, you log out—and, if necessary, disconnect from the UNIX computer.
If you turn on your terminal and see a message from the UNIX computer that looks something like this:
login:
you can probably skip ahead to Section 1.1.2 later in this chapter. Otherwise, browse through the next few sections and find the one that applies to you. (We can't cover every user's situation exactly. If none of these suggestions helps you enough, ask another UNIX user or your system administrator.)

Section 1.1.1.1: Connecting from another operating system

If you're using a personal computer to connect to the UNIX system, you'll probably need to start a terminal emulation program. Some common programs are procomm, qmodem, kermit, minicom, and telnet. (There are lots of others.)
If you start the program and get a UNIX "login:" prompt, you're ready to log in. But if your screen stays blank or you get another message that you don't understand, check with another user or your system administrator for help.

Section 1.1.1.2: Connecting with a data switch

Your office may have a data switch, a port contender, or another system that allows you to select which computer you will connect to. Like a telephone switchboard, this connects your terminal to one of a number of computers. Enter your computer's hostname or code number at the prompt—or choose from the menu of hosts.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Syntax of UNIX Command Lines
UNIX command lines can be simple, one-word entries like the date command. They can also be more complex: you may need to type more than the command name.
A UNIX command may or may not have arguments. An argument can be an option or a filename. The general format for UNIX commands is:
command option(s) filename(s)
There isn't a single set of rules for writing UNIX commands and arguments, but you can use these general rules in most cases:
  • Enter commands in lowercase.
  • Options modify the way in which a command works. Options are often single letters prefixed with a dash (-) and set off by any number of spaces or tabs. Multiple options in one command line can be set off individually (like -a -b), or, in some cases, you can combine them after a single dash (like -ab).
    Some commands, including those on Linux systems, also have options made from complete words or phrases, like --delete or --confirm-delete. When you enter a command, you can use this option style, the single-letter options (which all start with a single dash), or both.
  • The argument filename is the name of a file that you want to use. If you don't enter a filename correctly, you may get the response "filename: no such file or directory" or "filename: cannot open."
    Some commands, like telnet and who (shown earlier in this chapter), have arguments that aren't filenames.
  • You must type spaces between commands, options, and filenames.
  • Options come before filenames.
  • Two or more commands can be written on the same command line, each separated by a semicolon (;). Commands entered this way are executed one after another by the shell.
UNIX has a lot of commands! Don't try to memorize all of them. In fact, you'll probably need to know just a few commands and their options. As time goes on, you'll learn these commands and the best way to use them for your job. We cover some useful UNIX commands in later chapters.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Types of Commands
The previous section was about UNIX commands you enter at a shell prompt. Some UNIX commands have commands of their own. (For examples, look at the more, mail, and pg commands in Chapter 3. Text editors like vi and emacs also have their own commands.) Once you start the command, it prints its own prompt and understands its own set of commands (not UNIX commands).
For instance, if you enter mail, you'll see a new prompt from the mail program. You'll enter mail commands to handle mail messages. When you enter the special command (q) to quit the mail program, mail will stop prompting you. Then you'll get another shell prompt; you can enter UNIX commands again.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Unresponsive Terminal
During your UNIX session (while you're logged in), your terminal may not respond when you type a command, or the display on your screen may stop at an unusual place. That's called a "hung" or "frozen" terminal or session.
A session can be hung for several reasons. One of the most common is that the connection between your terminal and the computer gets too busy and your terminal has to wait its turn. (Other users or computers are probably sharing the same connection.) In that case, your session will start by itself in a few moments. You should not try to "un-hang" the session by entering extra commands because those commands will all take effect after the connection resumes.
If the system doesn't respond for quite a while (and how long that is depends on your individual situation; ask your system administrator for advice), the following solutions will usually work. Try these in the order shown until the system responds.
  1. Press the RETURN key.
    You may have typed a command but forgotten to press RETURN to tell the shell that you're done typing and it should now interpret the command.
  2. If you can type commands, but nothing happens when you press RETURN, try pressing LINE FEED or typing CTRL-J. If this works, your terminal needs to be reset to fix the RETURN key. Some systems have a reset command that you can run by typing CTRL-J reset CTRL-J. If this doesn't work, you may need to log out and log back in or turn your terminal off and on again.
  3. If your shell has job control (see Chapter 6), type CTRL-Z.
    This suspends a program that may be running and gives you another shell prompt. Now you can enter the jobs command to find the program's name, then restart the program with fg or terminate it with kill.
  4. Use your interrupt key (found earlier in this chapter—typically DELETE or CTRL-C.
    This interrupts a program that may be running. (Unless a program is run in the background, as described in Chapter 6, the shell will wait for it to finish before giving a new prompt. A long-running program may thus appear to hang the terminal.) If this doesn't work the first time, try it once more; doing it more than twice usually won't help.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Using Window Systems
All versions of UNIX work with computer terminals that handle a single window or a single login session. Most modern UNIX versions support one or more window systems. A window system is a package of programs that let a terminal handle many sessions at once. Along with the keyboard, window systems use a mouse or another device (such as a trackball) to move a pointer across the screen. The pointer can select parts of the screen, move them, help you copy and paste text, work with menus of commands, and more. If you've used a Macintosh, any version of Microsoft Windows, or OS/2 and its Presentation Manager (among others), you've used a window system. Figure 2.1 shows a typical display with windows.
Here's a special note for Linux users. Most Linux systems support window systems. But they also have a surprisingly handy substitute: virtual consoles. If you're using the terminal that's directly connected to a personal computer running Linux, you can access up to eight separate screens on the same display. To use virtual consoles, hold down the left ALT key and press one of the function keys F1 through F8. Each of those function keys will bring up a separate UNIX session with its own shell prompt. Use each one for whatever you want—just remember to log out from each virtual console when you're done!
We won't mention Linux virtual consoles any more. This chapter introduces the X Window System (called X for short), the most common UNIX window system. This introduction should also help you use non-X window systems.
Like UNIX, X is very flexible. The appearance of windows, the way menus work, and other features are controlled by a program called the window manager. Three common window managers are mwm, fvwm, and twm. There are plenty of other window managers—including fvwm95, which simulates a Windows 95 desktop on UNIX. This chapter explains mwm and uses it in examples. The details of using other window managers, and the ways they appear on the display, are somewhat different—but this chapter should help you use them, too.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction to Windowing
All versions of UNIX work with computer terminals that handle a single window or a single login session. Most modern UNIX versions support one or more window systems. A window system is a package of programs that let a terminal handle many sessions at once. Along with the keyboard, window systems use a mouse or another device (such as a trackball) to move a pointer across the screen. The pointer can select parts of the screen, move them, help you copy and paste text, work with menus of commands, and more. If you've used a Macintosh, any version of Microsoft Windows, or OS/2 and its Presentation Manager (among others), you've used a window system. Figure 2.1 shows a typical display with windows.
Here's a special note for Linux users. Most Linux systems support window systems. But they also have a surprisingly handy substitute: virtual consoles. If you're using the terminal that's directly connected to a personal computer running Linux, you can access up to eight separate screens on the same display. To use virtual consoles, hold down the left ALT key and press one of the function keys F1 through F8. Each of those function keys will bring up a separate UNIX session with its own shell prompt. Use each one for whatever you want—just remember to log out from each virtual console when you're done!
We won't mention Linux virtual consoles any more. This chapter introduces the X Window System (called X for short), the most common UNIX window system. This introduction should also help you use non-X window systems.
Like UNIX, X is very flexible. The appearance of windows, the way menus work, and other features are controlled by a program called the window manager. Three common window managers are mwm, fvwm, and twm. There are plenty of other window managers—including fvwm95, which simulates a Windows 95 desktop on UNIX. This chapter explains mwm and uses it in examples. The details of using other window managers, and the ways they appear on the display, are somewhat different—but this chapter should help you use them, too.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Starting X
There are several ways to start X and its window manager. This section explains a few of the most common ways. Figure 2.2 shows some steps along a few different paths to starting X. If your display is like any of the following, refer to the section noted. (If none of these fits your situation, skim through the next three sections or ask your system administrator for help.)
  • Figure 2.2A, xdm is running. Start with Section A.
  • Figure 2.2B, you have a standard UNIX login session. Start with Section B.
  • Figure 2.2C, X is running but a window manager probably isn't. (You can tell because the window doesn't have a "frame" around it.) Read Section C.
  • Figure 2.2D, the window has a frame, so X and the window manager (in this example, mwm) are running. You can skip ahead to the next main section, Section 2.3
Figure 2.2: Some possibilities
Some terminals, like the one shown in Figure 2.2A, are probably ready to use X. Your terminal has probably been set up to use xdm, the X display manager; xdm logs you in to your UNIX host and (usually) starts the window manager.
When you start, there's a single window in the middle of the screen that says something like "X Window System on hostname." The window has two prompts, "Login:" and "Password:". A cursor (a vertical bar) sits to the right of the "Login:" line. Type your username (login name) and press RETURN. Do the same for your password. The login window disappears.
If a display something like Figure 2.1 or Figure 2.2D appears, You're ready to use X! You can skip ahead to Section 2.3
If you get a display like Figure 2.2C, read Section C below. Or, if you get a blank display, try pressing and releasing your mouse buttons one by one, slowly, to see if a menu pops up.
If your terminal shows something like Figure 2.2B, with a standard UNIX "login:" prompt (not in a separate window; the whole screen looks like a terminal), then X is not running. Log in (as Chapter 1, explains) and get a shell prompt (like
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Running Programs
One of the most important X features is that windows can come either from programs running on another computer or from an operating system other than UNIX. So, if your favorite MS-DOS program doesn't run under UNIX but has an X interface, you can run the program under MS-DOS and display its windows with X on your UNIX computer. Researchers can run graphical data analysis programs on supercomputers in other parts of the country and see the results in their offices. There's much more than we could cover here. The O'Reilly & Associates book X Window System User's Guide, Volume Three, OSF/Motif Edition has all the details.
Of all the windows on your screen, only one window receives the keystrokes you type. This window is usually highlighted in some way. By default in the mwm window manager, for instance, the frame of the window that receives your input is a darker shade of grey. In X jargon, choosing the window you type to is called "setting the input focus." Most window managers can be configured to set the focus in one of the following two ways:
  • Point to the window and click a mouse button (usually the first button). You may need to click on the titlebar at the top of the window.
  • Simply move the pointer inside the window.
When you use mwm, any new windows will get the input focus automatically as they pop up.
One of the most important windows is an xterm window. xterm makes a terminal emulator window with a UNIX login session inside, just like a miniature terminal. You can have several xterm windows at once, each doing something different. To enter a UNIX command or answer a prompt in a window, set the focus there and type. Programs in other windows will keep running; if they need input from you, they'll wait just as they would on a separate terminal.
Figure 2.2D and Figure 2.4 show a single xterm window with a shell prompt (%) inside. If you enter a UNIX command (like date), it will run just as it would on a non-window terminal.
You can also start separate X-based window programs (typically called
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Working with a Mouse
Let's look at some basics of using a mouse or other pointing device.
As you move the mouse pointer from the root window onto other windows or menus, the shape of the pointer will change. For instance, on the root window, the pointer is a big X. The pointer may change to an hourglass shape to tell you to wait. When you resize a window, the pointer changes to a cross with arrows.
What's "pointing and clicking"? That's when you move the pointer to a place (usually over part of a window), then quickly press and release a mouse button (usually the first button). It's the same idea as pressing a button on a telephone or another electronic appliance.
Something else you'll do is "dragging." That means moving the pointer to a place (such as the corner of a window), then pressing a mouse button and holding it down while you move the pointer. This is called "dragging" a pointer or object, because the object will be dragged along with the pointer until you let go of the mouse button.
Xterm windows have an advantage over plain UNIX terminals in that you can copy and paste text within a window or between windows. To get started, move the pointer inside an xterm window and select the window (set the focus there). Notice that the pointer changes to an "I-beam" shape. There's also a block cursor. As you type, notice that text you input appears at the block-shaped cursor, just like it would on a standard terminal. So, think of the block cursor as the window's input point.
The I-beam pointer selects text for copying. Let's try it. Point to the first character of a command line (not the prompt) and click the first mouse button. Next, move the pointer to the end of the text you want to select and click the third button. The text between the first and third clicks should be highlighted. (If you accidentally click another button, you may need to start over.) Your
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Working with Windows
A window manager program helps you control windows. This section explains how mwm manages windows. Other window managers do the same kinds of things—but with some variation. Let's start by looking at Figure 2.4, a typical window under mwm.
The top part of every window has a titlebar with the title of the window and three buttons. The edges of the window can be used to resize the window. (See the Note in "Resizing Windows," later in this chapter.)
The top part of a window has three buttons (see Figure 2.4).
The two buttons at the top right corner have boxes inside them. Clicking the button with the small box makes the window as small as possible; the window turns into an icon. "Iconifying" puts unneeded windows out of the way without quitting the program inside them; it also keeps you from accidentally typing into a window. (Figure 2.1 shows an icon.) The button with the big square maximizes a window. That makes it as big as the client will allow, often as big as the screen.
Figure 2.4: A window running with the Motif Window Manager
The left button opens the window menu; this is explained in Section 2.5.4 later in this chapter.
To move a window, start by pointing to the titlebar. To move an icon, point to it. Hold down the first mouse button and drag to the new location—then let go of the button. You can also start the move from the window menu (see the following section on the window menu), but we think this way is easier.
If you have the pointer inside a window and then move the pointer to an edge, the pointer will change to an arrow shape. The arrow points the direction that you can resize the window. If you pointed to one of the corners, you can resize both sides that meet at the corner. To resize when you have the arrow pointer, press and hold the first button, then drag the window border until the window size is what you want and release the button. If you don't get quite the size you want, just do it again.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Other X Clients
Here are a few of the X client programs that your system may have:
  • resize: helps programs in xterm windows recognize new window size
  • xbiff: tells you when new electronic mail comes in
  • xclipboard: helps with copying and pasting text
  • xdpr: prints a window (with the printer)
  • xedit: simple text editor
  • xmag: magnifies parts of the screen
  • xman: browses UNIX manual (reference) pages
  • xmh: electronic mail program
  • xset: sets user preferences
For more information on those programs, see your online documentation or the O'Reilly X Window System User's Guide.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Quitting
Like almost everything in X, the way to quit X is configurable. The key to shutting down X is to know which of your programs (your windows or window manager) is the controlling program. When the controlling program quits, any leftover X programs are killed immediately. The controlling program is usually either the window manager or the single xterm window that started your X session.
Find the controlling program for your X session and write it down:
_____________________________ Program to quit last
If your controlling program is an xterm window, we suggest leaving that window iconified from just after you've logged in until you've shut down all the other X clients. That way, you won't end your X session accidentally by closing that xterm window too soon.
To quit the window manager, select the Exit or Quit command on the root menu.
Here are the steps to shut down X:
  1. Quit all noncontrolling programs (all programs other than the controlling program). If any windows are running programs that have their own "quit" commands, it's a good idea to use those special commands to quit. For example, if you're running a text editor in an xterm window, use the editor's "quit" command, then finish the xterm window by entering exit at the shell prompt.
    Using the program's own "quit" command gives the program time to clean up and shut down gracefully. On the other hand, the Close item on the mwm window menu can interrupt and kill a program before it's ready. If, however, a program doesn't have its own "quit" command, use Close on the window menu.
    If any icons are running programs that have their own "quit" command, open the icons into windows and use the "quit" commands.
  2. Quit the controlling program.
    After X shuts down, you may get a UNIX shell prompt. If you do, you can log out by entering exit. If you simply get another login box from xdm (as in Figure 2.2), you're done.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Your UNIX Account
Once you log in, you can use the many facilities UNIX provides. As an authorized system user, you have an account that gives you:
  • A place in the UNIX filesystem where you can store your files.
  • A username that identifies you and lets you control access to your files and receive messages from other users.
  • A customizable environment that you can tailor to your preferences.
A file is the unit of storage in UNIX, as in many other systems. A file can hold anything: text (a report you're writing, a to-do list), a program, digitally encoded pictures or sound, and so on. All of those are just sequences of raw data until they are interpreted by the right program.
In UNIX, files are organized into directories. A directory is actually a special kind of file where the system stores information about other files. A directory can be thought of as a place, so that files are said to be contained in directories and you are said to work inside a directory. (If you've used a Macintosh or Microsoft Windows computer, a UNIX directory is a lot like a folder. MS-DOS and UNIX directories are very similar.)
When you log in to UNIX, you're placed in a directory called your home directory. This home directory, a unique place in the UNIX filesystem, contains the files you use almost every time you log in. In your home directory, you can make your own files. As you'll see in a minute, you can also store your own directories within your home directory. Like folders in a file cabinet, this is a good way to organize your files.
Your working directory (sometimes called your current working directory) is the directory you're currently working in. At the start of every session, your home directory is your working directory. You may change to another directory, in which case the directory you move to becomes your working directory.
Unless you tell UNIX otherwise, all commands that you enter apply to the files in your working directory. In the same way, when you create files, they're created in your working directory.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The UNIX Filesystem
A file is the unit of storage in UNIX, as in many other systems. A file can hold anything: text (a report you're writing, a to-do list), a program, digitally encoded pictures or sound, and so on. All of those are just sequences of raw data until they are interpreted by the right program.
In UNIX, files are organized into directories. A directory is actually a special kind of file where the system stores information about other files. A directory can be thought of as a place, so that files are said to be contained in directories and you are said to work inside a directory. (If you've used a Macintosh or Microsoft Windows computer, a UNIX directory is a lot like a folder. MS-DOS and UNIX directories are very similar.)
When you log in to UNIX, you're placed in a directory called your home directory. This home directory, a unique place in the UNIX filesystem, contains the files you use almost every time you log in. In your home directory, you can make your own files. As you'll see in a minute, you can also store your own directories within your home directory. Like folders in a file cabinet, this is a good way to organize your files.
Your working directory (sometimes called your current working directory) is the directory you're currently working in. At the start of every session, your home directory is your working directory. You may change to another directory, in which case the directory you move to becomes your working directory.
Unless you tell UNIX otherwise, all commands that you enter apply to the files in your working directory. In the same way, when you create files, they're created in your working directory.
All directories on a UNIX system are organized into a hierarchical structure that you can imagine as a family tree. The parent directory of the tree is known as the root directory and is written as a forward slash (/).
The root contains several directories. Figure 3.1 shows the top of an imaginary UNIX filesystem tree—the root directory and some of the directories under the root.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Looking Inside Files
By now, you're probably tired of looking at files from the outside. It's kind of like going to a bookstore and looking at the covers, but never getting to read a word. Let's look at three programs for reading files: cat, more, and pg.
Most first-time users of UNIX think that cat is a strange name for a program. As we'll see later, cat, which is short for "concatenate," puts files together (concatenates them) to make a bigger file. It can also display files on your screen.
To display files on the standard output (your screen; see Chapter 5), use:
cat file(s)
For example, let's display the contents of the file /etc/passwd. This system file describes users' accounts. (Your system may have a more complete list somewhere else.)
% cat /etc/passwd
root:x&k8KP30f;(:0:0:Root:/:
daemon:*:1:1:Admin:/:
	.
	.
	.
%
cat works best for short files containing one screenful or less. If you cat a file that is too long, it may roll up the screen faster than you can read it. You cannot go back to view the previous screens when you use cat (unless you're using an xterm window with a scrollbar, that is).
If you enter cat without a filename, get out by pressing RETURN followed by a single CTRL-D.
If you want to "read" a long file on the screen, your system may have the more command to display one screen or "page" of text at a time. A standard terminal screen can usually display 24 lines of text; a window can display almost any number of lines. The syntax is:
more file(s)
more lets you move forward in the files by any number of pages or lines. Most versions let you move backward, too. You can also move back and forth between two or more files specified on the command line. When you invoke more, the first "page" of the file appears on the screen. A prompt appears at the bottom of the screen, as in the following example:
% more ch03
A file is the unit of storage in UNIX as in many other systems.
A file can be anything: a program,
	.
	.
	.
--More--(47%)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Protecting and Sharing Files
UNIX makes it easy for users to share files and directories. Controlling exactly who has access takes some explaining, though—more explaining than we can do here. So here's a cookbook set of instructions. If you have critical security needs or you just want more information, see the references in Appendix A.
A directory's access permissions help to control access to the files in it. These affect the overall ability to use files and subdirectories in the directory. (Once you have access to a directory, the ability to read or modify the contents of specific files is controlled by the file access permissions; see the second of the following two lists.)
In the commands below, replace dirname with the directory's pathname. An easy way to change permissions on the working directory is by using its relative pathname, . (dot), as in "chmod 755 .".
  • To keep yourself from accidentally removing files (or adding or renaming files) in a directory, use chmod 555 dirname. To do the same, but also deny other users any access, use chmod 500 dirname.
  • To protect the files in a directory and all its subdirectories from everyone else on your system—but still be able to do anything you want to dp there—use chmod 700 dirname.
  • To let other people on the system see what's in a directory—and read or edit the files if the file permissions let them—but not rename, remove, or add files—use chmod 755 dirname.
  • To let people in your UNIX group add, delete, and rename files in a directory of yours—and read or edit other people's files if the file permissions let them—use chmod 775 dirname.
  • To give full access to everyone on the system, use chmod 777 dirname.
Remember, to access a directory, a user must also have execute (x) permission to all of its parent directories, all the way up to the root.
The access permissions on a file control what can be done to the file's
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Electronic Mail
When you log in to your system, you may see a notice that says "You have mail." Someone has sent you a message or document by electronic mail (email). With email, you can compose a message at your terminal and send it to another user or list of users. You can also read any messages that others may have sent to you.
Email has several advantages over paper mail: it's convenient if you're already logged in, it's delivered much more quickly, you can send it to any number of people almost as easily as to just one person, and the messages can be stored for later reference.
There are a lot of email programs for UNIX. Some UNIX systems have only an old, simple program named mail, which this book doesn't cover. Most UNIX systems have a Berkeley program called Mail (with an uppercase "M"), mailx, or just mail. A popular menu-driven program that's easier to learn is called pine. All programs' basic principles are the same, though. We'll cover the Berkeley mail program.
Your mail's recipient doesn't have to be logged in. The messages you send are stored in the recipient's "mailbox," a file deep in the UNIX filesystem (often located in the directory /usr/mail). Messages are kept there until the recipient logs in and decides to read them.
To send mail, give the address of each person you want to send a message to, like this:
mail address1 address2 ...
There are several kinds of addresses, too many to explain here. If you have questions, see one of the references in Appendix A or ask your system administrator or postmaster (the person who maintains your email system). The most common addresses have this syntax:
username@hostname
username is the person's username and hostname is the name of their computer. If the recipient reads email on the same computer you do, you may omit the @hostname. To keep a copy of your message, just add your username to the list of addresses.
After you enter mail and the addresses, in most cases the program (depending on how it's set up) will prompt you for the subject of the message. Type a one-line summary of the message (just like a paper memo) and press
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Changing Your Password
On most UNIX systems, everyone knows (or can find) your username. When you log in, how does the system decide that you are really the owner of your account—not an intruder trying to break in? It uses your password. If anyone knows both your username and password, they can probably use your account, and that includes sending mail that looks like you wrote it. So you should keep your password a secret!
If you think that someone knows your password, you should probably change it right away—although, if you suspect a computer "cracker" (or "hacker") is using your account to break into your system, ask your system administrator for advice first, if possible! You should also change your password periodically—every few months is recommended.
In general, a password should be something that's easy for you to remember but hard for other people (or password-guessing programs!) to guess. Your system should have guidelines for secure passwords. If it doesn't, here are some suggestions. A password should be between six and eight characters long. It should not be a word in any language, your phone number, your address, or anything that anyone else might know or guess that you'd use as a password. It's best to mix upper- and lower-case letters, punctuation, and numbers.
To change your password, you'll probably use either the passwd or yppasswd command. After you enter the command, it will prompt you to enter your current password ("old password"). If that's correct, it will ask you to enter your new password—twice, to be sure you don't make a typing mistake. For security, neither the old nor new passwords appear as you type them.
On some systems, your password change won't take effect for some time: from a few minutes to a day or so after you make it.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Customizing Your Account
As we saw earlier, your home directory may have a hidden file called .profile. If it doesn't, there'll probably be one or more files named .login, .cshrc, .tshrc, .bashrc, .bash_profile, or .bash_login. These file are shell setup files, the key to customizing your account. Shell setup files contain commands that are automatically executed when a new shell starts—especially when you log in.
Let's take a look at these files. Go to your home directory, then use cat to display the file. Your .profile might look something like this:
PATH=/bin:/usr/bin:/usr/local/bin:
export PATH
/usr/games/fortune
date
umask 002
stty erase ^H intr ^C
A .login file might look something like this:
set path = (/bin /usr/bin /usr/local/bin .)
/usr/games
date
umask 002
stty erase ^H intr ^C
As you can see, these sample setup files contain commands to print a "fortune" and the date—just what happened earlier when we logged in! (/usr/games/fortune is a useless but entertaining program that prints a randomly selected saying from its collection. fortune isn't available on all systems.)
But what are these other commands?
  • The line with PATH= or set path = tells the shell which directories to search for UNIX commands. This saves you the trouble of typing the complete pathname for each program you run. (Notice that /usr/games isn't part of the path, so we had to use the absolute pathname to get our daily dose of wisdom from the fortune command.)
  • The umask command sets the default file permissions assigned to all files you create. Briefly, a value of 022 sets the permissions rw-r--r-- (read-write by owner, but read-only by everyone else), and 002 will produce rw-rw-r-- (read-write by owner and group, but read-only by everyone else). If this file is a program or a directory, both umasks will also give execute (x) permission to all users. See one of the books in Appendix A or your UNIX documentation for details.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 4: File Management
You'll usually create a text file with a text editor. An editor lets you add, change, and rearrange text easily. Two common UNIX editors are vi (pronounced "vee-eye") and emacs ("ee-macs").
Neither of those editors has the same features as popular word processing software on personal computers. Instead of being designed for making documents, envelopes, and so on, vi and emacs are very sophisticated, extremely flexible editors for all kinds of text files: programs, email messages, and so on. Many UNIX systems also support easy-to-use word processors. Ask your system administrator what's available. Pico is a simple editor (not word processor) that has been added to many UNIX systems.
Since there are several editor programs, you can choose one you're comfortable with. vi is probably the best choice because almost all UNIX systems have it, but emacs is also widely available. The O'Reilly & Associates books Learning the vi Editor and Learning GNU Emacs cover those editors in detail. If you'll be doing only simple editing, however, pico is a great choice. Although pico is much less powerful than emacs or vi, it's also a lot easier to learn.
You can also create a file by using a UNIX feature called input/output redirection, as Chapter 5, explains. This sends the output of a command directly to a file—to make a new file or make an existing file larger.
As Chapter 3, explained, both files and directories are identified by their names. A directory is really just a special kind of file, so the rules for naming directories are the same as the rules for naming files.
Filenames may contain any character except /, which is reserved as the separator between files and directories in a pathname. Filenames are usually made of upper- and lowercase letters, numbers, "." (dot), and "_" (underscore). Other characters (including spaces) are legal in a filename—but they can be hard to use because the shell gives them special meanings. So we recommend using only letters, numbers, dot, and underscore characters.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Methods of Creating Files
You'll usually create a text file with a text editor. An editor lets you add, change, and rearrange text easily. Two common UNIX editors are vi (pronounced "vee-eye") and emacs ("ee-macs").
Neither of those editors has the same features as popular word processing software on personal computers. Instead of being designed for making documents, envelopes, and so on, vi and emacs are very sophisticated, extremely flexible editors for all kinds of text files: programs, email messages, and so on. Many UNIX systems also support easy-to-use word processors. Ask your system administrator what's available. Pico is a simple editor (not word processor) that has been added to many UNIX systems.
Since there are several editor programs, you can choose one you're comfortable with. vi is probably the best choice because almost all UNIX systems have it, but emacs is also widely available. The O'Reilly & Associates books Learning the vi Editor and Learning GNU Emacs cover those editors in detail. If you'll be doing only simple editing, however, pico is a great choice. Although pico is much less powerful than emacs or vi, it's also a lot easier to learn.
You can also create a file by using a UNIX feature called input/output redirection, as Chapter 5, explains. This sends the output of a command directly to a file—to make a new file or make an existing file larger.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
File and Directory Names
As Chapter 3, explained, both files and directories are identified by their names. A directory is really just a special kind of file, so the rules for naming directories are the same as the rules for naming files.
Filenames may contain any character except /, which is reserved as the separator between files and directories in a pathname. Filenames are usually made of upper- and lowercase letters, numbers, "." (dot), and "_" (underscore). Other characters (including spaces) are legal in a filename—but they can be hard to use because the shell gives them special meanings. So we recommend using only letters, numbers, dot, and underscore characters.
Unlike some operating systems, UNIX doesn't require a dot (.) in a filename; in fact, you can use as many as you want. For instance, the filenames pizza and this.is.a.mess are both legal.
Some UNIX systems limit filenames to 14 characters. Most newer systems allow much longer filenames.
A filename must be unique inside its directory, but other directories may have files with the same names. For example, you may have the files called chap1 and chap2 in the directory /users/carol/work and also have files with the same names in /users/carol/play.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
File and Directory Wildcards
When you have a number of files named in series (for example, chap1 to chap12) or filenames with common characters (like aegis, aeon, and aerie), you can use wildcards (also called metacharacters) to specify many files at once. These special characters are * (asterisk), ? (question mark), and [ ] (square brackets). When used in a filename given as an argument to a command:
*
An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.txt) or to name many files at once (as in ae*).
?
A question mark is replaced by any single character (so h?p matches hop and hip, but not help).
[]
Square brackets can surround a choice of characters you'd like to match. Any one of the characters between the brackets will be matched. For example, [Cc]hapter would match either Chapter or chapter, but [ch]apter would match either capter or hapter. Use a hyphen (-) to separate a range of consecutive characters. For example, chap[13] would match chap1, chap2, or chap3.
The examples below demonstrate the use of wildcards. The first command lists all the entries in a directory, and the rest use wildcards to list just some of the entries. The last one is a little tricky; it matches files whose names contain two (or more) a's.
% ls
chap10       chap2        chap5     cold
chap1a.old   chap3.old    chap6     haha
chap1b       chap4        chap7     oldjunk
% ls chap?
chap2    chap5    chap7
chap4    chap6
% ls chap[5-8]
chap5    chap6    chap7
% ls chap??
chap10   chap1b
% ls *old
chap1a.old   chap3.old    cold
% ls *a*a*
chap1a.old   haha
Wildcards are useful for more than listing files. Most UNIX commands accept more than one filename, and you can use wildcards to put multiple files on the command line. For example, the command
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Managing Your Files
The tree structure of the UNIX filesystem makes it easy to organize your files. After you make and edit some files, you may want to copy or move files from one directory to another, rename files to distinguish different versions of a file, or give several names to the same file. You may want to create new directories each time you start working on a different project.
A directory tree can get cluttered with old files you don't need. If you don't need a file or a directory, delete it to free storage space on the disk. The sections below explain how to make and remove directories and files.
It's handy to group related files in the same directory. If you were writing a spy novel, you probably wouldn't want your intriguing files mixed with restaurant listings. You could create two directories: one for all the chapters in your novel (spy, for example), and another for restaurants (boston.dine).

Section 4.4.1.1: mkdir

To create a new directory, use the mkdir command. The format is:
mkdir dirname(s)
dirname is the name of the new directory. To make several directories, put a space between each directory name. To continue our example, you would enter:
% mkdir spy boston.dine
                  
If you're about to edit a file, you may want to save a copy of it first. Doing that makes it easy to get back the original version.

Section 4.4.2.1: cp

The cp command can put a copy of a file into the same directory or into another directory. cp doesn't affect the original file, so it's a good way to keep an identical backup of a file.
To copy a file, use the command:
cp old new
where old is a pathname to the original file and new is the pathname you want for the copy. For example, to copy the /etc/passwd file into a file called password in your working directory, you would enter:
% cp /etc/passwd password
%
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Printing Files
Before you print a file on a UNIX system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not look quite as nice.
Many versions of UNIX include two powerful text formatters, nroff and troff. (There are also versions called gnroff and groff.) They are much too complex to describe here. Before we cover printing itself, let's look at a simple formatting program called pr.
The pr command does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.
The