Tài liệu Teach yourself Unix in 24 hours - Pdf 95

Dave Taylor
James C. Armstrong, Jr.
Teach Yourself
UNIX
in 24 Hours
201 West 103rd Street
Indianapolis, Indiana 46290
Teach Yourself UNIX in 24 Hours
iv
President, Sams Publishng Richard K. Swadley
Publishing Manager Dean Miller
Director of Editorial Services Cindy Morrow
Director of Marketing Kelli Spencer
Product Marketing Manager Wendy Gilbride
Assistant Marketing Managers Jen Pock, Rachel Wolfe
Decimilli accipitrae Raptor Regina.—JA
To the newest light of my life: Ashley Elizabeth.—DT
Copyright  1997 by Sams Publishing
FIRST EDITION
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical, photo-
copying, recording, or otherwise, without written permission from the
publisher. No patent liability is assumed with respect to the use of the informa-
tion contained herein. Although every precaution has been taken in the
preparation of this book, the publisher and author assume no responsibility for
errors or omissions. Neither is any liability assumed for damages resulting from
the use of the information contained herein. For information, address Sams
Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
International Standard Book Number: 0-672-31107-0
Library of Congress Catalog Card Number: 97-66198
2000 99 98 97 4 3 2 1

Rhonda Tinch-Mize
Cover Designer
Tim Amrhein
Book Designer
Gary Adair
Copy Writer
David Reichwein
Production Team
Supervisors
Brad Chinn
Charlotte Clapp
Production
Brad Lenser
Chris Livengood
Gene Redding
Janet Seib
Overview
Introduction xvi
Hour 1 What Is this UNIX Stuff? 1
2 Getting onto the System and Using the Command Line 21
3 Moving About the File System 43
4 Listing Files and Managing Disk Usage 63
5 Ownership and Permissions 87
6 Creating, Moving, Renaming, and Deleting Files and Directories 113
7 Looking into Files 127
8 Filters and Piping 145
9 Wildcards and Regular Expressions 161
10 Power Filters and File Redirection 187
11 An Introduction to the vi Editor 199
12 Advanced vi Tricks, Tools, and Techniques 245

Key Terms 17
Questions 18
Preview of the Next Hour 19
2 Getting onto the System and Using the Command Line 21
Goals for This Hour 21
Task 2.1: Logging In and Out of the System 22
Task 2.2: Changing Passwords with passwd 25
Task 2.3: Picking a Secure Password 26
Task 2.4: Who Are You? 28
Task 2.5: Finding Out What Other Users Are Logged
in to the System 30
Task 2.6: What Is Everyone Doing on the Computer? 31
Task 2.7: Checking the Current Date and Time 33
Task 2.8: Looking at a Calendar 33
Simple Math with UNIX 36
Task 2.9: Using the bc Infix Calculator 36
Task 2.10: Using the dc Postfix Calculator 38
Summary 40
Workshop 40
Key Terms 40
Questions 41
Preview of the Next Hour 41
3 Moving About the File System 43
Goals for This Hour 43
What a Hierarchical File System Is All About 44
Task 3.1: The UNIX File System Organization 45
The bin Directory 46
The dev Directory 47
The etc Directory 47
The lib Directory 47

Permissions Strings 74
Task 4.8: Long Listing Format for Directories in ls 75
Task 4.9: Creating Files with the touch Command 78
Task 4.10: Check Disk-Space Usage with du 79
Task 4.11: Check Available Disk Space with df 82
Task 4.12: Shrink Big Files with the compress Program 83
Summary 84
Workshop 84
Key Terms 84
Questions 85
Preview of the Next Hour 85
Teach Yourself UNIX in 24 Hours
viii
5 Ownership and Permissions 87
Goals for This Hour 87
Task 5.1: Understand File Permissions Settings 88
Task 5.2: Directory Permissions Settings 93
Task 5.3: Modify File and Directory Permissions with chmod 96
Task 5.4: Set New File Permissions with chmod 98
Task 5.5: Calculating Numeric Permissions Strings 102
Task 5.6: Establish Default File and Directory Permissions
with the umask Command 104
Task 5.7: Identify Owner and Group for Any File or Directory 107
Task 5.8: Change the Owner of a File or Directory 108
Task 5.9: Change the Group of a File or Directory 109
Summary 110
Workshop 110
Key Terms 110
Questions 111
Preview of the Next Hour 111

Task 8.1: The Secrets of File Redirection 146
Task 8.2: Counting Words and Lines Using wc 147
Task 8.3: Removing Extraneous Lines Using uniq 149
Task 8.4: Sorting Information in a File Using sort 150
Task 8.5: Number Lines in Files Using cat -n and nl 153
Task 8.6: Cool nl Tricks and Capabilities 154
Summary 157
Workshop 158
Key Terms 158
Questions 158
Preview of the Next Hour 159
9 Wildcards and Regular Expressions 161
Goals for This Hour 161
Task 9.1: Filename Wildcards 162
Task 9.2: Advanced Filename Wildcards 164
Task 9.3: Creating Sophisticated Regular Expressions 167
Task 9.4: Searching Files Using grep 172
Task 9.5: For Complex Expressions, Try egrep 175
Task 9.6: Searching for Multiple Patterns at Once with fgrep 176
Task 9.7: Changing Things En Route with sed 179
Summary 185
Workshop 185
Key Terms 185
Questions 185
Preview of the Next Hour 186
10 Power Filters and File Redirection 187
Goals for This Hour 187
Task 10.1: The Wild and Weird awk Command 188
Task 10.2: Re-routing the Pipeline with tee 196
Summary 197

Summary of vi Commands 278
Summary 279
Workshop 279
Key Terms 279
Questions 279
Preview of the Next Hour 280
13 An Overview of the emacs Editor 281
Goals for This Hour 281
Task 13.1: Launching emacs and Inserting Text 282
Task 13.2: How To Move Around in a File 285
Task 13.3: How To Delete Characters and Words 289
Task 13.4: Search and Replace in emacs 294
Task 13.5: Using the emacs Tutorial and Help System 297
Task 13.6: Working with Other Files 299
Summary 303
Workshop 303
Key Terms 303
Questions 303
Preview of the Next Hour 304
14 Introduction to Command Shells 305
Goals for This Hour 305
Task 14.1: What Shells Are Available? 306
Task 14.2: Identifying Your Shell 309
Task 14.3: How To Choose a New Shell 310
Task 14.4: Learning the Shell Environment 313
Task 14.5: Exploring csh Configuration Files 317
Summary 321
Workshop 321
Key Terms 321
Questions 321

Task 17.3: Finding Out What Tasks Are Running 368
Task 17.4: Terminating Processes with kill 374
Summary 377
Workshop 377
Key Terms 377
Questions 378
Preview of the Next Hour 378
18 Printing in the UNIX Environment 379
Goals for This Hour 379
Task 18.1: Find Local Printers with printers 380
Task 18.2: Printing Files with lpr or lp 384
Task 18.3: Formatting Print Jobs with pr 387
Task 18.4: Working with the Print Queue 391
xiContents
Teach Yourself UNIX in 24 Hours
xii
Summary 394
Workshop 394
Key Terms 395
Questions 395
Preview of the Next Hour 395
19 Searching for Information and Files 397
Goals for This Hour 397
Task 19.1: The find Command and Its Weird Options 398
Task 19.2: Using find with xargs 403
Summary 405
Workshop 405
Questions 405
Preview of the Next Hour 406
20 Communicating with Others 407

Workshop 477
Key Terms 477
Questions 477
Preview of the Next Hour 478
23 Using telnet and ftp 479
Goals for This Hour 479
Task 23.1: Connecting to Remote Internet Sites 480
Task 23.2: Copying Files from Other Internet Sites 483
Task 23.3: Finding Archives with archie 493
Task 23.4: A Few Interesting telnet Sites 499
Workshop 507
Key Terms 507
Questions 507
Preview of the Next Hour 507
24 Programming in C for UNIX 509
Goals for This Hour 509
Task 24.1: Your First Program 510
Task 24.2: Basic Data Types and Operators 512
Task 24.3: Conditional Statements 517
Task 24.4: Looping Statements 520
Task 24.5: Functions 521
Task 24.6: Arrays 523
Task 24.7: Pointers 524
Task 24.8: Structures 526
Summary 528
Where To Go Next 528
Workshop 529
Key Terms 529
Questions 530
Glossary 531

James has a Bachelor’s degree in Computer Science from Duke University and has done some
graduate study at the University of St. Andrews in Scotland. James is an avid naturalist and
environmentalist and has traveled the world to photograph the beauty of nature.
Tell Us What You Think!
As a reader, you are the most important critic and commentator of our books. We value your
opinion and want to know what we’re doing right, what we could do better, what areas you’d
like to see us publish in, and any other words of wisdom you’re willing to pass our way. You
can help us make strong books that meet your needs and give you the computer guidance you
require.
Do you have access to CompuServe or the World Wide Web? Then check out our
CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web,
check out our site at .
If you have a technical question about this book, call the technical support
line at 317-581-4669.
As the team leader of the group that created this book, I welcome your comments. You can
fax, e-mail, or write me directly to let me know what you did or didn’t like about this book—
as well as what we can do to make our books stronger. Here’s the information:
Fax: 317-581-4669
E-mail:
Mail: Dean Miller
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290
JUST A MINUTE
Teach Yourself UNIX in 24 Hours
xvi
Introduction
Welcome to Teach Yourself UNIX in 24 Hours! This book has been designed so it is helpful
for both beginning users and those with previous UNIX experience. This text is helpful as

effects on the task at hand. These are the most important special elements
in this book.
Tasks
This book offers another special element called a Task. These step-by-step exercises are
designed to quickly walk you through the most important skills you can learn in UNIX. Each
Task has three parts—Description, Action, and Summary.
Workshops
The Workshop section at the end of each lesson provides Key Terms and Questions that
reinforce concepts you learned in the lesson and help you apply them in new situations. You
can skip this section, but it is advised that you go through the exercises to see how the concepts
can be applied to other common tasks. The Key Terms also are compiled in one alphabetized
list in the Glossary at the end of the book.
TIME SAVER
CAUTION
1What Is This UNIX Stuff?
1
Hour 1
What Is This UNIX
Stuff?
Welcome to Teach Yourself UNIX in 24 Hours! This hour starts you toward
becoming a UNIX expert. Our goal for the first hour is to introduce you to some
UNIX history and to teach you where to go for help online.
Goals for This Hour
In the first hour, you learn
■ The history of UNIX
■ Why it’s called UNIX
■ What multiuser systems are all about
■ The difference between UNIX and other operating systems
■ About command-line interpreters and how users interact with UNIX
■ How to use man pages, UNIX’s online reference material

be completely different from how your UNIX-expert friend does the same task. Worse, some
specific commands in UNIX have many different versions, partly because of the variations
from different UNIX vendors. (You’ve heard of these variations and vendors, I’ll bet:
UNIXWare from Novell, Solaris from Sun, SCO from Santa Cruz, System V Release 4
(pronounce that “system five release four” or, to sound like an ace, “ess-vee-are-four”), and
BSD UNIX (pronounced “bee-ess-dee”) from University of California at Berkeley are the
primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
3What Is This UNIX Stuff?
1
I must admit that I, too, am guilty of rewriting a variety of UNIX com-
mands, including those for an electronic mail system, a simple line-
oriented editor, a text formatter, a programming language interpreter,
calendar manager, and even slightly different versions of the file-listing
command ls and the remove-files command rm. As a programmer, I found
that trying to duplicate the functionality of a particular command or utility
was a wonderful way to learn more about UNIX and programming.
Given the multichoice nature of UNIX, I promise to teach you the most popular UNIX
commands, and, if there are alternatives, I will teach you about those, too. The goal of this
book is for you to learn UNIX and to be able to work alongside long-time UNIX folk as a
peer, sharing your expertise with them and continuing to learn about the system and its
commands from them and other sources.
A Brief History of UNIX
To understand why the UNIX operating system has so many commands and why it’s not only
the premier multiuser, multitasking operating system, but also the most successful and the
most powerful multichoice system for computers, you’ll have to travel back in time. You’ll
need to learn where UNIX was designed, what were the goals of the original programmers,
and what has happened to UNIX in the subsequent decades.

Once he’d explored some of the capabilities of the PDP-7, Thompson couldn’t resist
building on the game, starting with an implementation of an earlier file system he’d designed,
then adding processes, simple file utilities (cp, mv), and a command interpreter that he called
a “shell.” It wasn’t until the following year that the newly created system acquired its name,
UNIX, which Brian Kernighan suggested as a pun on Multics.
The Thompson file system was built around the low-level concept of i-nodes—linked blocks
of information that together comprise the contents of a file or program—kept in a big list
called the i-list, subdirectories, and special types of files that described devices and acted as
the actual device driver for user interaction. What was missing in this earliest form of UNIX
was pathnames. No slash (/) was present, and subdirectories were referenced through a
confusing combination of file links that proved too complex, causing users to stop using
subdirectories. Another limitation in this early version was that directories couldn’t be added
while the system was running and had to be added to the preload configuration.
In 1970, Thompson’s group requested and received a Digital PDP-11 system for the purpose
of creating a system for editing and formatting text. It was such an early unit that the first disk
did not arrive at Bell Labs until four months after the CPU showed up. The first important
program on UNIX was the text-formatting program roff, which—keep with me now—was
inspired by McIlroy’s BCPL program on Multics, which in turn had been inspired by an
earlier program called runoff on the CTSS operating system.
The initial customer was the Patent Department inside the Labs, a group that needed a system
for preparing patent applications. There, UNIX was a dramatic success, and it didn’t take
long for others inside Bell Labs to begin clamoring for their own UNIX computer systems.
The C Programming Language
That’s where UNIX came from. What about C, the programming language that is integral
to the system?
5What Is This UNIX Stuff?
1
In 1969, the original UNIX had a very-low-level assembly language compiler available for
writing programs; all the PDP-7 work was done in this primitive language. Just before the
PDP-11 arrived, McIlroy ported a language called TMG to the PDP-7, which Thompson

What’s All This About Multiuser Systems?
Among the many multi words you learned earlier was one that directly concerns how you
interact with the computer, multiuser. The goal of a multiuser system is for all users to feel
6
Hour 1
1
as though they’ve each been given their own personal computer, their own individual UNIX
system, although they actually are working within a large system. To accomplish this, each
user is given an account—usually based on the person’s last name, initials, or another unique
naming scheme—and a home directory, the default place where his or her files are saved. This
leads to a bit of a puzzle: When you’re working on the system, how does the system know
that you’re you? What’s to stop someone else from masquerading as you, going into your files,
prying into private letters, altering memos, or worse?
On a Macintosh or PC, anyone can walk up to your computer when you’re not around, flip
the power switch, and pry, and you can’t do much about it. You can add some security
software, but security isn’t a fundamental part of the system, which results in an awkward fit
between system and software. For a computer sitting on your desk in your office, though,
that’s okay; the system is not a shared multiuser system, so verifying who you are when you
turn on the computer isn’t critical.
But UNIX is a system designed for multiple users, so it is very important that the system can
confirm your identity in a manner that precludes others from masquerading as you. As a
result, all accounts have passwords associated with them—like a PIN for a bank card, keep
it a secret!—and, when you use your password in combination with your account, the
computer can be pretty sure that you are who you’re claiming to be. For obvious reasons,
when you’re done using the computer, you always should remember to end your session, or,
in effect, to turn off your virtual personal computer when you’re done.
In the next hour, you learn your first UNIX commands. At the top of the list are commands
to log in to the system, enter your password, and change your password to be memorable and
highly secure.
Cracking Open the Shell

own examples!
Getting Help
Throughout this book, the focus is on the most important and valuable flags and options for
the commands covered. That’s all well and good, but how do you find out about the other
alternatives that might actually work better for your use? That’s where the UNIX “man” pages
come in. You will learn how to browse them to find the information desired.
Task 1.1: Man Pages, UNIX Online Reference
It’s not news to you that UNIX is a very complex operating system, with hundreds
of commands that can be combined to execute thousands of possible actions. Most
commands have a considerable number of options, and all seem to have some subtlety or
other that it’s important to know. But how do you figure all this out? You need to look up
commands in the UNIX online documentation set. Containing purely reference materials,
the UNIX man pages (man is short for manual ) cover every command available.
To search for a man page, enter man followed by the name of the command to find. Many
sites also have a table of contents for the man pages (it’s called a whatis database, for obscure
historical reasons.) You can use the all-important -k flag for keyword searches, to find the
name of a command if you know what it should do but you just can’t remember what it’s
called.
JUST A MINUTE
8
Hour 1
1
A command performs a basic task, which can be modified by adding
flags to the end of the command when you enter it on the command line.
These flags are described in the man pages. For example, to use the –k
flag for man, enter:
% man –k
The command apropos is available on most UNIX systems and is often just
an alias to man -k. If it’s not on your system, you can create it by adding
the following line to your .cshrc file:

Mkdir creates specified directories in mode 777. Standard
entries, `.’, for the directory itself, and ` ’ for its
parent, are made automatically.
Mkdir requires write permission in the parent directory.
SEE ALSO
rmdir(1)
Revision 1.4.2.2 88/08/13 1
%
Notice in the example, that in the first line, the command itself is in
boldface type, but everything else is not bold. Throughout this book,
whenever an example contains both user input and UNIX output, the user
input will be bold so that you can spot easily what you are supposed to
enter.
The very first line of the output tells me that it’s found the mkdir command in
Section 1 (user commands) of the man pages, with the middle phrase, DYNIX
Programmer’s Manual, indicating that I’m running on a version of UNIX called
DYNIX. The NAME section always details the name of the command and a one-line
summary of what it does. SYNOPSIS explains how to use the command, including all
possible command flags and options.
DESCRIPTION is where all the meaningful information is, and it can run on for
dozens of pages, explaining how complex commands like csh or vi work. SEE ALSO
suggests other commands that are related in some way. The Revision line at the
bottom is different on each version of man, and it indicates the last time, presum-
ably, that this document was revised.
JUST A MINUTE
10
Hour 1
1
2. The same man page from a Sun workstation is quite different:
% man mkdir

%
That’s not quite what I want, unfortunately. Because it’s in Section 4 (note that the
word in parentheses is 4S, not 1), this document will describe the disk driver rather
than any command to work with floppy disks.
I can look up the disk command instead:
% man -k disk
acctdisk, acctdusg, accton, acctwtmp (8) - overview of accounting and
➥miscellaneous accounting commands
add_client (8) - create a diskless network bootable NFS client on


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status