BUY THIS BOOK
Add to Cart

Print Book $19.95


Safari Books Online

What is this?

Add to UK Cart

Print Book £13.95

What is this?

Looking to Reprint this content?


Learning the Unix Operating System
Learning the Unix Operating System, Fifth Edition A Concise Guide for the New User

By Jerry Peek, Grace Todino-Gonguet, John Strang
Price: $19.95 USD
£13.95 GBP

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Getting Started
Before you can use Unix, a system staff person has to set up a Unix account for you. The account is identified by your username, which is usually a single word or an abbreviation. 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 username, you may also need to know the hostname (name) of the computer that has your account. Alternatively, your account may be shared between all computers on the local network, and you may be able to log into any of them.
Once you've logged in to your account, you'll interact with your system by typing commands at a command line, to a program called a shell. You'll get acquainted with the shell, enter a few commands, and see how to handle common problems. To finish your Unix session, you'll log out.
Each user communicates with the computer from a terminal. To get into the Unix environment, you first connect to the Unix computer. (Your terminal is probably already connected to a computer. But Unix systems also let you log into other computers across a network. In this case, log into your local computer first, then use a remote login command to connect to the remote computer. See Section 6.1 in Chapter 6.)
After connecting your terminal, if needed, you start a session by logging in to your Unix account. To log in, you need your username and a password. Logging in does two things: it identifies which user is in a session, and it tells the computer that you're ready to start work. When you've finished, log out—and, if necessary, disconnect from the Unix computer.
If someone else has your username and password, they probably can log into your account and do anything you can. They can read private information, corrupt or delete important files, send email messages as if they came from you, and more. If your computer is connected to a network—the Internet or a local network inside your organization—intruders may also be able to log in without sitting at your keyboard! See Section 6.1 in Chapter 6 for one explanation of one way this can be 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!
Working in the Unix Environment
Each user communicates with the computer from a terminal. To get into the Unix environment, you first connect to the Unix computer. (Your terminal is probably already connected to a computer. But Unix systems also let you log into other computers across a network. In this case, log into your local computer first, then use a remote login command to connect to the remote computer. See Section 6.1 in Chapter 6.)
After connecting your terminal, if needed, you start a session by logging in to your Unix account. To log in, you need your username and a password. Logging in does two things: it identifies which user is in a session, and it tells the computer that you're ready to start work. When you've finished, log out—and, if necessary, disconnect from the Unix computer.
If someone else has your username and password, they probably can log into your account and do anything you can. They can read private information, corrupt or delete important files, send email messages as if they came from you, and more. If your computer is connected to a network—the Internet or a local network inside your organization—intruders may also be able to log in without sitting at your keyboard! See Section 6.1 in Chapter 6 for one explanation of one way this can be done.
Anyone may be able to get your username—it's usually part of your email address, for instance. Your password is what keeps others from logging in as you. Don't leave your password anywhere around your computer. Don't give your password to anyone who asks you for it unless you're sure they'll preserve your account security. Also don't send your password by email; it can be stored, unprotected, on other systems and on backup tapes, where other people may find it and then break into your account.
If you suspect that someone is using your account, ask system staff for advice. If you can't do that, setting a new password may help; see Section 3.6 in Chapter 3.
Unix systems are case sensitive. Most usernames, commands, and filenames use lowercase letters (though good passwords use a mixture of lower- and uppercase letters). Before you log in, be sure 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!
Syntax of Unix Command Lines
Unix command lines can be simple, one-word entries such as the date command. They can also be more complex; you may need to type more than the command or program name.
A Unix command may or may not have arguments. An argument can be an option or a filename. The general format for Unix command lines 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 (-, also called "hyphen" or "minus") and set off by any number of spaces or tabs. Multiple options in one command line can be set off individually (such as -a -b). In some cases, you can combine them after a single dash (such as -ab)—but most commands' documentation doesn't tell you whether this will work; you'll have to try it.
    Some commands, including those on Linux systems, also have options made from complete words or phrases and starting with two dashes, like ––delete or ––confirm–delete. When you enter a command line, 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. Most Unix programs also accept multiple filenames, separated by spaces. If you don't enter a filename correctly, you may get a response such as "filename: no such file or directory" or "filename: cannot open."
    Some commands, such as 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.
    In a few cases, an option has another argument associated with it; type this special argument just after its option. Most options don't work this way, but you should know about them. The
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
When you use a program, you'll want to know how to control it. How can you tell it what job you want done? Do you give instructions before the program starts, or after it's started? There are three general ways to give commands on a Unix system, three different kinds of programs. It's good to be aware of them.
  1. Some Unix programs work only with a window system. For instance, when you type netscape at a shell prompt (or click a button or choose the command from a menu), the Netscape web browser starts. It opens one or more windows on your screen. The program has its own way to receive your commands—through menus and buttons on its windows, for instance.
  2. You've also seen (previously, in Section 1.2) Unix commands that you enter at a shell prompt. These programs work in a window system (from a terminal window) or from any terminal. Control those programs from the Unix command line—that is, by typing options and arguments from a shell prompt before you start the program running. After you start the program, wait for it to finish; you generally don't interact with it.
  3. Some Unix programs that work in terminals have commands of their own. (If you'd like some examples, see Section 3.2 in Chapter 3 and Section 4.3.2 in Chapter 4.) These programs may accept options and arguments on their command line. But, once you start the program, it prints its own prompt and/or menus and it understands its own commands; it takes instructions from your keyboard, which weren't given on its command line.
    For instance, if you enter pine at a shell prompt, you'll see a new prompt from the pine program. Enter Pine commands to handle email messages. When you enter the special command q to quit the pine program, pine will stop prompting you. Then you'll get another shell prompt, where you can enter other Unix commands.
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.
(Note that most of the techniques in this section apply to terminal windows in a window system, but not to nonterminal windows such as a web browser. In Chapter 2, Section 2.7 should help with windows in general.)
A session can hang for several reasons. For instance, the connection between your terminal and the computer can get too busy; your terminal has to wait its turn. (Other users or computers probably share the same connection.) In that case, your session starts 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 (how long that is depends on your individual situation; ask other users about their experiences), the following solutions usually work. Try the following steps in the order shown until the system responds:
  1. Press the RETURN key once.
    You may have typed text at a prompt (for example, a command line at a shell prompt) but haven't yet pressed RETURN to say that you're done typing and your text should be interpreted.
  2. If you can type commands, but nothing happens when you press RETURN, try pressing LINEFEED or typing CTRL-J. If this works, your terminal needs resetting 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. (But, before you turn off your terminal, read the notes earlier and later in this chapter about turning off the power.)
  3. If your shell has job control (see Chapter 7), type CTRL-Z.
    This suspends a program that may be running and gives you another shell prompt. Now you can enter the
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 alphanumeric terminals that handle a single session in a single screen, such as those described in Chapter 1. On most modern Unix versions, you can also use a window system. A window system is software that lets a single screen handle many sessions at once. Window systems use a mouse or another device (such as a trackball) to move a pointer across the screen. The pointer can be used to select and move parts of the screen, copy and paste text, work with menus of commands, and more. If you've used a Macintosh or Microsoft Windows, among others, you've used a window system. Figure 2-1 shows a typical screen with windows.
This chapter introduces the X Window System, which is called X for short, the most common Unix window system. This introduction should also help you use window systems other than X.
Like Unix, X is very flexible. The appearance of windows, the way menus and icons work, as well as other features, are controlled by a program called the window manager. There are many different window managers; some have many features and "eye candy," while others are simple and have just basic features. A window manager can make your desktop look a lot like a Macintosh or Microsoft Windows system, or it can look completely different. Your system may also have an optional desktop environment that provides even more features, such as support for "drag and drop" (for example, printing a file by dragging its icon onto a printer icon). Two popular desktop environments are GNOME and KDE. In this chapter, we show GNOME with the Sawfish window manager, as well as KDE with the kwm window manager. Details of other window managers, including how they make your screen look, are somewhat different—but this chapter should help you use them, too.
Figure 2-1: An X screen with KDE and kwm
There are several ways to start X and its window manager. This section explains a few common ways. Figure 2-2 shows some steps along a few different paths to starting X. (The large "X" on the figures is the mouse pointer, or cursor, that you may see on your screen.) If your screen is like any of the following, refer to the section noted. If none fits your situation, skim through the next three sections or ask another X user for 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!
Introduction to Windowing
Like Unix, X is very flexible. The appearance of windows, the way menus and icons work, as well as other features, are controlled by a program called the window manager. There are many different window managers; some have many features and "eye candy," while others are simple and have just basic features. A window manager can make your desktop look a lot like a Macintosh or Microsoft Windows system, or it can look completely different. Your system may also have an optional desktop environment that provides even more features, such as support for "drag and drop" (for example, printing a file by dragging its icon onto a printer icon). Two popular desktop environments are GNOME and KDE. In this chapter, we show GNOME with the Sawfish window manager, as well as KDE with the kwm window manager. Details of other window managers, including how they make your screen look, are somewhat different—but this chapter should help you use them, too.
Figure 2-1: An X screen with KDE and kwm
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 common ways. Figure 2-2 shows some steps along a few different paths to starting X. (The large "X" on the figures is the mouse pointer, or cursor, that you may see on your screen.) If your screen is like any of the following, refer to the section noted. If none fits your situation, skim through the next three sections or ask another X user for help.
  • Figure 2-2A, xdm (or another program, such as gdm or kdm) is running and waiting for you to log in graphically. Start reading at Section 2.2.1.
  • Figure 2-2B has a standard Unix login session; the X Window System is not running. Start reading at Section 2.2.2.
  • Figure 2-2C shows X running, but a window manager probably isn't. (You can tell because the window doesn't have a frame around it: there's no titlebar or border.) Read Section 2.2.3.
  • Figure 2-2D shows the window with a frame (titlebar and border), so X and the window manager (in this example, mwm) are running. You're ready to go! Skip ahead to Section 2.3.
Figure 2-2: Four scenarios that may occur while starting X
Some terminals, like the one whose screen is shown in Figure 2-2A, are ready to use X. Your terminal has probably been set up to use one of the X display managers called xdm, gdm, kdm, or others; these log you in to your account and usually also start the window manager.
When you start, there's a single window in the middle of the screen that has two prompts like "login:" and "password:". The cursor sits to the right of the "login:" line. To log in, type your username (login name) and press RETURN, then do the same for your password. The login window disappears.
If a screen 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 screen such as Figure 2-2C (a single window with no title and no border), read Section C. Or, if you get a blank screen, press and release your mouse buttons one by one, slowly, to see if a menu pops up.
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
A window manager can open windows of its own. But the main use of a window manager is to manage windows opened by other programs. We mention a few window programs here; Section 2.8, near the end of this chapter, has more.
One of the most important X features is that windows can come either from programs running on your local computer, or over a network from programs running on remote computers. The remote computers can run Unix or another operating system. So, if your favorite program from another operating system doesn't run under Unix but has an X interface, you may be able to run that program on its native OS and display its windows with X on your Unix computer. (Check your program's documentation; see Section 8.1 in Chapter 8.) 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 can explain here. We cover the basics here and in Chapter 6 in Section 6.2, which also has a figure showing how this works. If you'll do a lot of work with X, you may want a detailed reference that explains X and your window manager.
Of all the windows on your screen, only one window receives the keystrokes you type. This window is usually highlighted in some way. For instance, the titlebar of the window that receives your input may be blue instead of the default grey color. 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 ways:
  • Move the mouse pointer into a window and click a mouse button (usually the first button; see Section 2.4, later in this chapter). In some systems, you may need to click on the titlebar at the top of the window.
  • Simply move the pointer inside a window.
Your window manager may be configured to give the input focus automatically to any new windows that pop up.
One of the most important windows is a
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 basics of using a mouse or other pointing device. We assume that your mouse has three buttons, as mice on Unix systems usually do. (If your mouse has two buttons, you may be able to simulate the missing middle button by pressing both of the other buttons at once. Your X system may need to be reconfigured to work this way, though.) When we say "the first button," we mean the leftmost button for right-handed users, or the rightmost button for left-handed users. (Under X, a mouse can be set for either left-handed or right-handed users, so the button you use to click on and drag is the button under your index finger.)
As you move the mouse pointer from the desktop on to other windows or menus, the shape of the pointer changes. For instance, on some window systems, while the pointer is over the desktop, it's shaped like a big X. The pointer may change to an hourglass shape to tell you to wait. When you resize a window, the pointer could change to a cross with arrows.
One advantage terminal windows have over plain (nonwindowing) terminals is you can copy and paste text within an xterm window or between xterm windows.
Although this section is specifically about xterm windows, most tips here also apply to other kinds of terminal windows. Check your documentation or just try these and see!
To get started, move the pointer inside an xterm window and be sure it has the focus (if your window manager doesn't do that automatically). Notice that the pointer changes to an "I-beam" shape as you move it into the window. We discuss this I-beam pointer later in this section. There's also a block cursor (which is shown in all terminal windows in this chapter—and labeled at Figure 2-4.) The block cursor is the window's insertion point, where text goes when you type on the keyboard. The block cursor doesn't follow the mouse.
If your window doesn't have some previously completed command lines, as in Figure 2-3, type command lines at a couple of prompts; this gives you text to copy. (The
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. Various window managers do the same kinds of things, with some variation. Let's start by looking at Figure 2-4, which shows a typical window under KDE and kwm.
The edges of the window can be used to resize the window. The top of every window has a titlebar that includes the window title as well as three buttons.
The titlebar is the top of a window; it has the window's title, and, usually, some buttons or other features. See Figure 2-4.
Figure 2-4: A window under KDE and kwm
The three buttons at the top right corner have boxes inside them.
  • Click the button with the small square to mimimize the window (make the window as small as possible); this is also called "hiding" the window. With some window manager configurations, a minimized window turns into an icon; on others, the window may have a title button in a list of window buttons at the bottom of a window (or elsewhere). No matter how your window manager treats a minimized window, though, the important point here is that minimizing puts unneeded windows out of the way without quitting the program inside them; it also keeps you from accidentally typing into a window.
    You can restore ("show") a minimized ("hidden") window by clicking on its icon, its button on the panel at the bottom of the screen, or in several other ways—again, depending on your window manager.
  • Maximizing a window makes it as big as the program will allow, often as big as the screen. One way to do this—which is different in different window managers, of course—is to click the button on the window frame that has a large square.
    Maximizing windows that have graphical applications in them—such as a web browser—works just fine. But it can be a bad idea to maximize terminal windows (and give them more than 80 characters per line), especially if there's a program running in that window. See the note in Section 2.5.3, later in this chapter.
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 Window Manager Features
Your window manager and/or desktop environment may have some other handy features. Let's look at two. Explore these after you've learned more basic operations; they can be real timesavers!
A panel is a thin bar across the top or bottom of the screen. If you have a panel, it probably has icons you can click to launch a program. It also may have a row of buttons, one for each window that's either open or minimized (or both); you can use these buttons to open a minimized window or raise an open window to the top of a stack. If you move the mouse pointer over an object without clicking, a "tool tip" box may appear with more information about it. You also can try clicking on these and/or holding down your mouse buttons (try each mouse button, in turn) to see what they might do.
Sophisticated window managers can manage multiple desktops on the same physical screen. A pager lets you choose between these virtual desktops. Each virtual desktop is like the desktop you see when you open the window system—but you can open different windows on each virtual desktop. It's kind of like having two or more separate physical terminals stacked on top of each other, as well as side-to-side, on your desk—each with its own desktop displayed, all driven from the same keyboard and mouse. All virtual desktops appear on the same screen, but only one is visible at a time. If you're using GNOME or KDE, you may have four virtual desktops available automatically, though that number can be changed.
Think of these desktops arranged in a grid, as Figure 2-6 shows. You can refer to the desktops by saying "the desktop above" or "the desktop to the right"; they may also be numbered. The window manager has commands to let you move a window from one desktop to another (see Section 2.5.5, earlier in this chapter). You also may be able to use the mouse to drag a window "off the edge" of one desktop and onto the next.
Figure 2-6: Four virtual desktops, one screen
If you're working on three projects, for instance, each project can have its own desktop, where you can put all the windows for that project. The pager lets you choose which desktop you want to see. If your window manager has a panel, each virtual desktop probably has its own panel.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Unresponsive Windows
While your window system is running, windows may seem to stop responding to commands or mouse clicks. Some or all of the window contents—but not necessarily the window frame itself—may go partly or completely blank. In this case, either a window or the entire window system may be "frozen" or "hung."
The best thing to do in a situation like this is to wait a little while. If the window is showing something from a network, such as a web browser showing a web page from the Internet—or, especially, if the window is from another computer across the network—the cause may be a network delay. If you can wait a minute or two (without pressing extra keys or clicking the mouse around; just wait!) and nothing happens, then it's time to take action. Here are some things to try at that point:
  1. Try to find out whether just one window is frozen, or if all windows (and the entire window system) are frozen.
    If you have other windows open, try to use them. For instance, if a text editor is open, try to add a word to the text or use a command from one of its menus. If other windows seem to work normally, you can guess that the problem is only in the one window (or family of windows, from one program) that seems frozen.
    If you don't have other windows open, try to open a new window from the window manager's menu, icons, panel, etc. Try to minimize and maximize that window, move it around the screen, and so on. If this seems to work normally, the trouble is probably in the original frozen window. But, if nothing seems to work, the whole window system may be frozen.
  2. Next, handle the frozen window or windows:
    • If just one window is frozen, you'll probably need to kill it. Click the first mouse button on the "close" box in the window's title bar; this box is often an X symbol. In many Unix window managers, clicking that box sends a "close" signal to the window, and it may do the trick. If it doesn't work, click the third mouse button on that box; in some cases, this will send a "destroy" signal to the window. (All this depends on the window manager you're using!) If several windows are frozen, try these techniques on each window.
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 Window Programs
Following are a few standard X window programs that your system may have:
  • xbiff: tells you when new electronic mail comes in
  • xclipboard: helps with copying and pasting text
  • xmag: magnifies parts of the screen
  • xman: browses Unix manual (reference) pages
For more information on those programs and the many other standard X programs, see a reference file or book. There's a list of the X programs that come with XFree86™—and, in general, with other X Releases too—at (as of this printing) http://www.xfree86.org/current/manindex1.html.
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 terminal 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 a terminal window, we suggest leaving that window minimized from just after you've logged in until you've shut down all the other X programs. That way, you won't end your X session accidentally by closing that terminal window too soon.
No matter what your controlling program is, closing it sooner than last means the other windows will be killed quickly and automatically. There's a chance, though, that programs running in those killed windows—especially if they're terminal windows—won't be killed, and will keep running, disconnected from your login session. So it's best to close the controlling program window last.
To quit the window manager, select the "Exit" or "Quit" command on its main 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 a terminal window, use the editor's "quit" command, then close the terminal window by entering exit at the shell prompt. (Most terminal windows will close when their shell program exits. If yours doesn't do this, though, you'll need to finish by using the terminal window's own "close" command.)
    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 a menu from the window manager can interrupt and kill a program before it's ready. If, however, a program doesn't have its own "quit" command, use
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: Using Your Unix Account
Once you log in, you can use the many facilities that 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, lets you control access to your files, and is an address for your email.
  • An environment you can customize.
A file is the unit of storage in Unix, as in most 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're 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. You can think of a directory 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 similar to a folder.)
This section introduces the Unix filesystem. Later sections in this chapter show how you can look in files and protect them. Chapter 4 has more information.
When you log in to Unix, you're placed in a directory called your home directory. This 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 (also called your current directory) is the directory you're currently working in. Every time you log in, 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 unless you specify another directory. For instance, if you type 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!
The Unix Filesystem
A file is the unit of storage in Unix, as in most 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're 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. You can think of a directory 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 similar to a folder.)
This section introduces the Unix filesystem. Later sections in this chapter show how you can look in files and protect them. Chapter 4 has more information.
When you log in to Unix, you're placed in a directory called your home directory. This 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 (also called your current directory) is the directory you're currently working in. Every time you log in, 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 unless you specify another directory. For instance, if you type the command pico report, the Pico editor is started on a file named report in your working directory. But if you type a command such as pico
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 with less
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 a program for reading files.
If you want to "read" a long file on the screen, your system may have the less command to display one "page" (a terminal filled from top to bottom) of text at a time.
If you don't have less, you'll probably have similar programs named more or pg. (In fact, the name less is a play on the name of more, which came first.) The syntax is:
less option(s) file(s)
less lets you move forward or backward in the files by any number of pages or lines; you can also move back and forth between two or more files specified on the command line. When you invoke less, the first "page" of the file appears. A prompt appears at the bottom of the terminal (or terminal window), as in the following example:
$ less ch03
A file is the unit of storage in Unix, as in most other systems.
A file can hold anything: text (a report you're writing,
	.
	.
	.
:
The basic less prompt is just a colon (:)—although, for the first screenful, less displays the file's name as a prompt. The cursor sits to the right of this prompt as a signal for you to enter a less command to tell less what to do.
Like almost everything about less, the prompt can be customized. For example, using the less -M option on the less command line makes the prompt show the filename and your position in the file. (If you want this to happen every time you use less, you can set the LESS environment variable to M (without a dash) in your shell setup file. See Section 3.7, later in this chapter.)
You can set or unset most options temporarily from the less prompt. For instance, if you have the short less prompt (a colon), you can enter -M while less is running. less responds "Long prompt (press RETURN)," and for the rest of the session, less prompts with the filename, line number, and percentage of the file viewed.
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. For instance, everyone in a group can read documents stored in one of their manager's directories without needing to make their own copies—if the manager has allowed access. There might be no need to fill peoples' email inboxes with file attachments if everyone can access those files directly through the Unix filesystem.
Here's a brief introduction to file security and sharing. Networked systems with multiple users, such as Unix, have complex security issues that take tens or hundreds of pages to explain. If you have critical security needs or you just want more information, talk to your system staff or see an up-to-date book on Unix security.
Note that the system's superuser (the system administrator and possibly other users) can do anything to any file at any time, no matter what its permissions are. So, access permissions won't keep your private information safe from everyone—although let's hope that you can trust your system staff!
Your system staff should also keep backup copies of users' files. These backup copies may be readable by anyone who has physical access to them. That is, anyone who can take the backup out of a cabinet (or wherever) and mount it on a computer system may be able to read the file copies. The same is true for files stored on floppy disks and any other removable media. (Once you take a file off of a Unix system, that system can't control access to it anymore.)
A directory's access permissions help to control access to the files and subdirectories in that directory:
  • If a directory has read permission, a user can run ls to see what's in the directory and use wildcards to match files in it.
  • A directory that has write permission allows users to add, rename, and delete files in the directory.
  • To access a directory—that is, to read or write the files in the directory or to run the files if they're programs—a user needs execute permission on that directory. Note that to access a directory, a user must also have execute permission to all of its parent directories, all the way up to 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!
Graphical Filesystem Browsers
Most Unix window systems give you a graphical way to do some of the things you can do with files from the command line. A filesystem browser, such as the GNOME File Manager or KDE's Konqueror, lets you see a graphical representation of the filesystem and do a limited number of operations on it. Figure 3-6 shows the GNOME filesystem browser. The left pane has a directory tree. The right pane shows the contents of the directory that's selected (open) in the left pane; here, this is the directory /home/mpeek. The titlebar shows the pathname of the selected directory.
Figure 3-6: GNOME filesystem browser
A filesystem browser can be handy for seeing what's in the filesystem. Unfortunately, because a filesystem browser takes you away from the shell you're using for other work, it can limit what you're able to do with Unix. (You'll see additional information about why this is true when we cover more advanced features such as input-output redirection in Chapter 5.) We recommend learning about your filesystem browser but also learning what you can do at the more powerful Unix command line.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Completing File and Directory Names
Most Unix shells can complete a partly typed file or directory name for you. Different shells have different methods. In many shells, you type the first few letters of the name, then press TAB. If the shell can find just one way to finish the name, it will; your cursor will move to the end of the new name, where you can type more or press RETURN to run the command. (You also can edit or erase the completed name.)
What happens if more than one file or directory name matches what you've typed so far? Again, that depends on the shell you're using. The cursor will probably stay where it is, and the terminal may beep. At this point, the easiest answer could be to type more characters of the name (to make the name unique) and press TAB again to complete the rest of the name. You may also be able to get a list of all possible completions; after the first beep, try pressing TAB again (or CTRL-D, depending on your shell) and you may see a list of all names starting with the characters you've typed so far. Here's an example from the bash shell:
$ cp /etc/pa
               TAB
               (beep)
               TAB
pam.d         paper.config  passwd        passwd-       passwd.OLD
$ cp /etc/pa
At this point, I could type another character or two—an s, for example—and then press TAB once more to make /etc/passwd.
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 really own your account and aren't an intruder trying to break in? Unix uses your password. If anyone knows both your username and password, they can use your account—including sending email that looks as if you wrote it. So you should keep your password a secret! Never write it down and leave it anywhere near your terminal.
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.
A password should be 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 anyone else might know or guess that you'd use as a password. It's best to mix upper- and lowercase letters, punctuation, and numbers.
To change your password, you'll probably use either the passwd or yppasswd program from a shell prompt. After you enter the command, it prompts you to enter your password ("old password"). If the password is correct, it asks you to enter your new password—twice, to be sure there is no 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. The change may require between a few minutes to a day.
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, .tcshrc, .bashrc, .bash_profile, or .bash_login. These files are shell setup files, and are 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 less to display the file. Your .profile might look something like this:
PATH='/bin:/usr/bin:/usr/local/bin:'
LESS='eMq'
export PATH LESS
/usr/games/fortune
date
umask 002
A .login file could look like this:
set path = (/bin /usr/bin /usr/local/bin .)
setenv LESS 'eMq'
/usr/games/fortune
date
umask 002
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 programs. 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 program.) The export PATH is needed in the .profile, but not in .login.
  • The line with LESS= or setenv LESS tells the less program which options you want to set every time you use it. This saves you the trouble of typing the options on every less command line. The export LESS line is needed in the .profile, but not in .login.
  • The umask command sets the default file permissions assigned to all files you create. Briefly, a value of 022 sets the permissions
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
Chapter 3 introduced the Unix filesystem. This chapter explains how to name, edit, copy, move, find, and print files.
As Chapter 3 explains, 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, "." (dots), and "_" (underscores). 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, dots, and underscore characters. You can also use "-" (dashes), as long as they aren't the first character of a filename, which can make a program think the filename is an option.
If you have a file with a space in its name, the shell will be confused if you type its name on the command line. That's because the shell breaks command lines into separate arguments at the spaces.
To tell the shell not to break an argument at spaces, put quote marks (") around the argument. For example, the rm program, covered later in this chapter, removes files.
To remove a file named a confusing name, the first rm command, which follows, doesn't work; the second one does:
$ ls -l
total 2
-rw-r--r--  1  jpeek users     0 Oct 23 11:23 a confusing name
-rw-r--r--  1  jpeek users  1674  Oct 23 11:23 ch01
$ rm a confusing name
rm: a: no such file or directory
rm: confusing: no such file or directory
rm: name: no such file or directory
$ rm "a confusing name"
$
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
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
Content preview·Buy PDF of this chapter|