Teach Yourself UNIX - PUCP

Transcription

Teach YourselfUNIXin 24 HoursDave TaylorJames C. Armstrong, Jr.201 West 103rd StreetIndianapolis, Indiana 46290

Teach Yourself UNIX in 24 HoursivDecimilli accipitrae Raptor Regina.—JATo the newest light of my life: Ashley Elizabeth.—DTCopyright 1997 by Sams PublishingFIRST EDITIONAll rights reserved. No part of this book shall be reproduced, stored in aretrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from thepublisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in thepreparation of this book, the publisher and author assume no responsibility forerrors or omissions. Neither is any liability assumed for damages resulting fromthe use of the information contained herein. For information, address SamsPublishing, 201 W. 103rd St., Indianapolis, IN 46290.International Standard Book Number: 0-672-31107-0Library of Congress Catalog Card Number: 97-6619820009998974321Acquisitions EditorGrace M. BuechleinDevelopment EditorBrian-Kent ProffittProduction EditorKristi HartIndexerGreg PearsonTechnical ReviewerRaj MangalEditorial CoordinatorsMandi RouellKatie WiseTechnical Edit CoordinatorLynette QuinnResource CoordinatorDeborah FrisbyInterpretation of the printing code: the rightmost double-digit number is theyear of the book’s printing; the rightmost single-digit, the number of the book’sprinting. For example, a printing code of 97-1 shows that the first printing ofthe book occurred in 1997.Editorial AssistantsCarol AckermanAndi RichterRhonda Tinch-MizeComposed in AGaramond and MCPdigital by Macmillan Computer PublishingCover DesignerTim AmrheinPrinted in the United States of AmericaAll terms mentioned in this book that are known to be trademarks or servicemarks have been appropriately capitalized. Sams Publishing cannot attest to theaccuracy of this information. Use of a term in this book should not be regardedas affecting the validity of any trademark or service mark.Book DesignerGary AdairCopy WriterDavid ReichweinProduction TeamSupervisorsBrad ChinnCharlotte ClappPresident, Sams Publishng Richard K. SwadleyPublishing Manager Dean MillerDirector of Editorial Services Cindy MorrowDirector of Marketing Kelli SpencerProduct Marketing Manager Wendy GilbrideAssistant Marketing Managers Jen Pock, Rachel WolfeProductionBrad LenserChris LivengoodGene ReddingJanet Seib

OverviewHour nWhat Is this UNIX Stuff?Getting onto the System and Using the Command LineMoving About the File SystemListing Files and Managing Disk UsageOwnership and PermissionsCreating, Moving, Renaming, and Deleting Files and DirectoriesLooking into FilesFilters and PipingWildcards and Regular ExpressionsPower Filters and File RedirectionAn Introduction to the vi EditorAdvanced vi Tricks, Tools, and TechniquesAn Overview of the emacs EditorIntroduction to Command ShellsGetting the Most Out of the C ShellBasic Shell ProgrammingJob ControlPrinting in the UNIX EnvironmentSearching for Information and FilesCommunicating with OthersUsing Netscape To See the World Wide WebInternet E-Mail, Netnews, and IRCUsing telnet and ftpProgramming in C for 5323347361379397407425443479509531Index541

Teach Yourself UNIX in 24 HoursviContentsHour 1What Is This UNIX Stuff?1Goals for This Hour . 1What Is UNIX? . 2A Brief History of UNIX . 3The C Programming Language . 4UNIX Becomes Popular . 5What’s All This About Multiuser Systems? . 5Cracking Open the Shell . 6Getting Help . 7Task 1.1: Man Pages, UNIX Online Reference . 7Task 1.2: Other Ways to Find Help in UNIX . 14Summary . 17Workshop . 17Key Terms . 17Questions . 18Preview of the Next Hour . 192Getting onto the System and Using the Command Line21Goals for This Hour . 21Task 2.1: Logging In and Out of the System . 22Task 2.2: Changing Passwords with passwd . 25Task 2.3: Picking a Secure Password . 26Task 2.4: Who Are You? . 28Task 2.5: Finding Out What Other Users Are Loggedin to the System . 30Task 2.6: What Is Everyone Doing on the Computer? . 31Task 2.7: Checking the Current Date and Time . 33Task 2.8: Looking at a Calendar . 33Simple Math with UNIX . 36Task 2.9: Using the bc Infix Calculator . 36Task 2.10: Using the dc Postfix Calculator . 38Summary . 40Workshop . 40Key Terms . 40Questions . 41Preview of the Next Hour . 413Moving About the File System43Goals for This Hour . 43What a Hierarchical File System Is All About . 44Task 3.1: The UNIX File System Organization . 45The bin Directory . 46

The dev Directory . 47The etc Directory . 47The lib Directory . 47The lost found Directory . 48The mnt and sys Directories . 48The tmp Directory . 48The usr Directory . 48Other Miscellaneous Stuff at the Top Level . 49How Mac and PC File Systems Differ from the UNIX File System . 50Directory Separator Characters . 50The Difference Between Relative and Absolute Filenames . 51Task 3.2: Hidden Files in UNIX . 52Task 3.3: The Special Directories “.” and “.” . 55Task 3.4: The env Command . 56Task 3.5: PATH and HOME . 57Task 3.6: Find Where You Are with pwd . 58Task 3.7: Move to Another Location with cd . 58Summary . 60Workshop . 60Key Terms . 60Questions . 62Preview of the Next Hour . 624Listing Files and Managing Disk Usage63Goals for This Hour . 63The ls Command . 64Task 4.1: All About the ls Command . 64Task 4.2: Having ls Tell You More . 65Task 4.3: Combining Flags . 68Task 4.4: Listing Directories Without Changing Location . 69Special ls Command Flags . 71Task 4.5: Changing the Sort Order in ls . 71Task 4.6: Listing Directory Trees Recursively in ls . 73Task 4.7: Long Listing Format in ls . 74Permissions Strings . 74Task 4.8: Long Listing Format for Directories in ls . 75Task 4.9: Creating Files with the touch Command . 78Task 4.10: Check Disk-Space Usage with du . 79Task 4.11: Check Available Disk Space with df . 82Task 4.12: Shrink Big Files with the compress Program . 83Summary . 84Workshop . 84Key Terms . 84Questions . 85Preview of the Next Hour . 85

Teach Yourself UNIX in 24 Hoursviii5Ownership and Permissions87Goals for This Hour . 87Task 5.1: Understand File Permissions Settings . 88Task 5.2: Directory Permissions Settings . 93Task 5.3: Modify File and Directory Permissions with chmod . 96Task 5.4: Set New File Permissions with chmod . 98Task 5.5: Calculating Numeric Permissions Strings . 102Task 5.6: Establish Default File and Directory Permissionswith the umask Command . 104Task 5.7: Identify Owner and Group for Any File or Directory . 107Task 5.8: Change the Owner of a File or Directory . 108Task 5.9: Change the Group of a File or Directory . 109Summary . 110Workshop . 110Key Terms . 110Questions . 111Preview of the Next Hour . 11167Creating, Moving, Renaming, and Deleting Filesand Directories113Goals for This Hour . 113Task 6.1: Creating New Directories Using mkdir . 114Task 6.2: Copying Files to New Locations Using cp . 116Task 6.3: Moving Files to New Locations Using mv . 118Task 6.4: Renaming Files with mv . 119Task 6.5: Removing Directories with rmdir . 120Task 6.6: Removing Files Using rm . 121Task 6.7: Minimizing the Danger of the rm Command . 123Summary . 125Workshop . 125Key Terms . 125Questions . 126Preview of the Next Hour . 126Looking into Files127Goals for This Hour . 127Task 7.1: Using file to Identify File Types . 128Task 7.2: Exploring UNIX Directories with file . 130Task 7.3: Peeking at the First Few Lines with head . 133Task 7.4: Viewing the Last Few Lines with tail . 135Task 7.5: Viewing the Contents of Files with cat . 136Task 7.6: Viewing Larger Files with more . 139Summary . 143Workshop . 143Key Terms . 143Questions . 144Preview of the Next Hour . 144

Contents8Filters and Pipingix145Goals for This Hour . 145Task 8.1: The Secrets of File Redirection . 146Task 8.2: Counting Words and Lines Using wc . 147Task 8.3: Removing Extraneous Lines Using uniq . 149Task 8.4: Sorting Information in a File Using sort . 150Task 8.5: Number Lines in Files Using cat -n and nl . 153Task 8.6: Cool nl Tricks and Capabilities . 154Summary . 157Workshop . 158Key Terms . 158Questions . 158Preview of the Next Hour . 15991011Wildcards and Regular Expressions161Goals for This Hour . 161Task 9.1: Filename Wildcards . 162Task 9.2: Advanced Filename Wildcards . 164Task 9.3: Creating Sophisticated Regular Expressions . 167Task 9.4: Searching Files Using grep . 172Task 9.5: For Complex Expressions, Try egrep . 175Task 9.6: Searching for Multiple Patterns at Once with fgrep . 176Task 9.7: Changing Things En Route with sed . 179Summary . 185Workshop . 185Key Terms . 185Questions . 185Preview of the Next Hour . 186Power Filters and File Redirection187Goals for This Hour . 187Task 10.1: The Wild and Weird awk Command . 188Task 10.2: Re-routing the Pipeline with tee . 196Summary . 197Workshop . 197Questions . 197Preview of the Next Hour . 198An Introduction to the vi Editor199Goals for This Hour . 200Task 11.1: How To Start and Quit vi . 200Task 11.2: Simple Cursor Motion in vi . 205Task 11.3: Moving by Words and Pages . 208Task 11.4: Inserting Text into the File Using i, a, o, and O . 212Task 11.5: Deleting Text . 220Task 11.6: Searching Within a File . 229Task 11.7: How To Start vi Correctly . 234Task 11.8: The Colon Commands in vi . 236

Teach Yourself UNIX in 24 HoursxSummary . 242Workshop . 243Key Terms . 243Questions . 244Preview of the Next Hour . 244121314Advanced vi Tricks, Tools, and Techniques245Goals for This Hour . 245Task 12.1: The Change and Replace Commands . 246Task 12.2: Numeric Repeat Prefixes . 253Task 12.3: Numbering Lines in the File . 255Task 12.4: Search and Replace . 257Task 12.5: Mapping Keys with the :map Command . 260Task 12.6: Moving Sentences and Paragraphs . 266Task 12.7: Access UNIX with ! . 270Summary of vi Commands . 278Summary . 279Workshop . 279Key Terms . 279Questions . 279Preview of the Next Hour . 280An Overview of the emacs Editor281Goals for This Hour . 281Task 13.1: Launching emacs and Inserting Text . 282Task 13.2: How To Move Around in a File . 285Task 13.3: How To Delete Characters and Words . 289Task 13.4: Search and Replace in emacs . 294Task 13.5: Using the emacs Tutorial and Help System . 297Task 13.6: Working with Other Files . 299Summary . 303Workshop . 303Key Terms . 303Questions . 303Preview of the Next Hour . 304Introduction to Command Shells305Goals for This Hour . 305Task 14.1: What Shells Are Available? . 306Task 14.2: Identifying Your Shell . 309Task 14.3: How To Choose a New Shell . 310Task 14.4: Learning the Shell Environment . 313Task 14.5: Exploring csh Configuration Files . 317Summary . 321Workshop . 321Key Terms . 321Questions . 321Preview of the Next Hour . 322

Contents15Getting the Most Out of the C Shellxi323Goals for This Hour . 323Task 15.1: The C Shell and Korn Shell History Mechanisms . 324Task 15.2: Using History to Cut Down on Typing . 327Task 15.3: Command Aliases . 333Task 15.4: Some Power Aliases . 335Task 15.5: Setting Custom Prompts . 338Task 15.6: Creating Simple Shell Scripts . 340Summary . 344Workshop . 344Key Terms . 344Questions . 344Preview of the Next Hour . 345161718Basic Shell Programming347Goals for This Hour . 347Task 16.1: Shell Variables . 348Task 16.2: Shell Arithmetic . 350Task 16.3: Comparison Functions . 351Task 16.4: Conditional Expressions . 355Task 16.5: Looping expressions . 357Summary . 359Workshop . 359Key Terms . 360Questions . 360Preview of the Next Hour . 360Job Control361Goals for This Hour . 361Task 17.1: Job Control in the Shell: Stopping Jobs . 362Task 17.2: Foreground/Background and UNIX Programs . 365Task 17.3: Finding Out What Tasks Are Running . 368Task 17.4: Terminating Processes with kill . 374Summary . 377Workshop . 377Key Terms . 377Questions . 378Preview of the Next Hour . 378Printing in the UNIX Environment379Goals for This Hour . 379Task 18.1: Find Local Printers with printers . 380Task 18.2: Printing Files with lpr or lp . 384Task 18.3: Formatting Print Jobs with pr . 387Task 18.4: Working with the Print Queue . 391

Teach Yourself UNIX in 24 HoursxiiSummary . 394Workshop . 394Key Terms . 395Questions . 395Preview of the Next Hour . 39519202122Searching for Information and Files397Goals for This Hour . 397Task 19.1: The find Command and Its Weird Options . 398Task 19.2: Using find with xargs . 403Summary . 405Workshop . 405Questions . 405Preview of the Next Hour . 406Communicating with Others407Goals for This Hour . 407Task 20.1: Enabling Messages Using mesg . 408Task 20.2: Writing to Other Users with write . 409Task 20.3: Reading Electronic Mail with mailx . 411Task 20.4: Sending Mail with mailx . 417Task 20.5: The Smarter Electronic Mail Alternative, elm . 420Summary . 423Workshop . 423Key Terms . 424Questions . 424Preview of the Next Hour . 424Using Netscape To See the World Wide Web425Goals for This Hour . 425Introduction to the Internet . 426Task 21.1: Starting Your Browser . 427Task 21.2: Finding Some Sites . 432Task 21.3: Customizing Your Browser . 437Summary . 440Workshop . 440Key Terms .

iv Teach Yourself UNIX in 24 Hours 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