Mark G. Sobell - A Practical Guide to Linux Commands, Editors, and Shell Programming (2012, Prentice

This preview shows page 1 out of 2227 pages.

Unformatted text preview: A Practical Guide to Linux® Commands, Editors, and Shell Programming Third Edition Mark G. Sobell Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States, please contact: International Sales [email protected] Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data Sobell, Mark G. A practical guide to Linux commands, editors, and shell programming / Mark G. Sobell.—3rd ed. p. cm. Includes bibliographical references and index. ISBN 978-0-13-308504-4 (pbk. : alk. paper) 1. Linux. 2. Operating systems (Computers) I. Title. QA76.76.O63S59483 2012 005.4′32—dc23 2012028015 Copyright © 2013 Mark G. Sobell All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-13-308504-4 ISBN-10: 0-13-308504-X Text printed in the United States at Edwards Brothers Malloy in Ann Arbor, Michigan. Fourth printing, September 2014 Praise for Previous Editions of A Practical Guide to Linux® Commands, Editors, and Shell Programming “This book is a very useful tool for anyone who wants to ‘look under the hood’ so to speak, and really start putting the power of Linux to work. What I find particularly frustrating about man pages is that they never include examples. Sobell, on the other hand, outlines very clearly what the command does and then gives several common, easy-to-understand examples that make it a breeze to start shell programming on one’s own. As with Sobell’s other works, this is simple, straight-forward, and easy to read. It’s a great book and will stay on the shelf at easy arm’s reach for a long time.” —Ray Bartlett Travel Writer “Overall I found this book to be quite excellent, and it has earned a spot on the very front of my bookshelf. It covers the real ‘guts’ of Linux—the command line and its utilities—and does so very well. Its strongest points are the outstanding use of examples, and the Command Reference section. Highly recommended for Linux users of all skill levels. Well done to Mark Sobell and Prentice Hall for this outstanding book!” —Dan Clough Electronics Engineer and Slackware Linux User “Totally unlike most Linux books, this book avoids discussing everything via GUI and jumps right into making the power of the command line your friend.” —Bjorn Tipling Software Engineer ask.com “This book is the best distro-agnostic, foundational Linux reference I’ve ever seen, out of dozens of Linux-related books I’ve read. Finding this book was a real stroke of luck. If you want to really understand how to get things done at the command line, where the power and flexibility of free UNIX-like OSes really live, this book is among the best tools you’ll find toward that end.” —Chad Perrin Writer, TechRepublic “I moved to Linux from Windows XP a couple of years ago, and after some distro hopping settled on Linux Mint. At age 69 I thought I might be biting off more than I could chew, but thanks to much reading and the help of a local LUG I am now quite at home with Linux at the GUI level. “Now I want to learn more about the CLI and a few months ago bought your book: A Practical Guide to Linux® Commands, Editors, and Shell Programming, Second Edition. “For me, this book is proving to be the foundation upon which my understanding of the CLI is being built. As a comparative ‘newbie’ to the Linux world, I find your book a wonderful, easyto-follow guide that I highly recommend to other Linux users.” —John Nawell CQLUG (Central Queensland Linux User Group) “I have the second edition of A Practical Guide to Linux® Commands, Editors, and Shell Programming and am a big fan. I used it while working as a Cisco support engineer. I plan to get the third edition as soon as it is released. We will be doing a ton of command-line work on literally 1000 boxes (IMS core nodes). I feel you have already given me a lot of tools with the second edition. I want to get your new book as soon as possible. The way you write works very well for my style of learning.” —Robert Lingenfelter Support Engineer, VoIP/IMS Praise for Other Books by Mark G. Sobell “Since I’m in an educational environment, I found the content of Sobell’s book to be right on target and very helpful for anyone managing Linux in the enterprise. His style of writing is very clear. He builds up to the chapter exercises, which I find to be relevant to real-world scenarios a user or admin would encounter. An IT/IS student would find this book a valuable complement to their education. The vast amount of information is extremely well balanced and Sobell manages to present the content without complicated asides and meandering prose. This is a ‘must have’ for anyone managing Linux systems in a networked environment or anyone running a Linux server. I would also highly recommend it to an experienced computer user who is moving to the Linux platform.” —Mary Norbury IT Director Barbara Davis Center University of Colorado at Denver from a review posted on slashdot.org “I had the chance to use your UNIX books when I when was in college years ago at Cal Poly, San Luis Obispo, CA. I have to say that your books are among the best! They’re quality books that teach the theoretical aspects and applications of the operating system.” —Benton Chan IS Engineer “The book has more than lived up to my expectations from the many reviews I read, even though it targets FC2. I have found something very rare with your book: It doesn’t read like the standard technical text, it reads more like a story. It’s a pleasure to read and hard to put down. Did I say that?! :-)” —David Hopkins Business Process Architect “Thanks for your work and for the book you wrote. There are really few books that can help people to become more efficient administrators of different workstations. We hope (in Russia) that you will continue bringing us a new level of understanding of Linux/UNIX systems.” —Anton Petukhov “Mark Sobell has written a book as approachable as it is authoritative.” —Jeffrey Bianchine Advocate, Author, Journalist “Excellent reference book, well suited for the sysadmin of a Linux cluster, or the owner of a PC contemplating installing a recent stable Linux. Don’t be put off by the daunting heft of the book. Sobell has striven to be as inclusive as possible, in trying to anticipate your system administration needs.” —Wes Boudville Inventor “A Practical Guide to Red Hat® Linux® is a brilliant book. Thank you Mark Sobell.” —C. Pozrikidis University of California at San Diego “This book presents the best overview of the Linux operating system that I have found. . . . [It] should be very helpful and understandable no matter what the reader’s background: traditional UNIX user, new Linux devotee, or even Windows user. Each topic is presented in a clear, complete fashion and very few assumptions are made about what the reader knows. . . . The book is extremely useful as a reference, as it contains a 70-page glossary of terms and is very well indexed. It is organized in such a way that the reader can focus on simple tasks without having to wade through more advanced topics until they are ready.” —Cam Marshall Marshall Information Service LLC Member of Front Range UNIX Users Group [FRUUG] Boulder, Colorado “Conclusively, this is THE book to get if you are a new Linux user and you just got into RH/Fedora world. There’s no other book that discusses so many different topics and in such depth.” —Eugenia Loli-Queru Editor in Chief OSNews.com “I currently own one of your books, A Practical Guide to Linux®. I believe this book is one of the most comprehensive and, as the title says, practical guides to Linux I have ever read. I consider myself a novice and I come back to this book over and over again.” —Albert J. Nguyen “Thank you for writing a book to help me get away from Windows XP and to never touch Windows Vista. The book is great; I am learning a lot of new concepts and commands. Linux is definitely getting easier to use.” —James Moritz “I am so impressed by how Mark Sobell can approach a complex topic in such an understandable manner. His command examples are especially useful in providing a novice (or even an advanced) administrator with a cookbook on how to accomplish real-world tasks on Linux. He is truly an inspired technical writer!” —George Vish II Senior Education Consultant Hewlett-Packard Company “Overall, I think it’s a great, comprehensive Ubuntu book that’ll be a valuable resource for people of all technical levels.” —John Dong Ubuntu Forum Council Member Backports Team Leader “The JumpStart sections really offer a quick way to get things up and running, allowing you to dig into the details of the book later.” —Scott Mann Aztek Networks “I would so love to be able to use this book to teach a class about not just Ubuntu or Linux but about computers in general. It is thorough and well written with good illustrations that explain important concepts for computer usage.” —Nathan Eckenrode New York Local Community Team “Ubuntu is gaining popularity at the rate alcohol did during Prohibition, and it’s great to see a well-known author write a book on the latest and greatest version. Not only does it contain Ubuntu-specific information, but it also touches on general computer-related topics, which will help the average computer user to better understand what’s going on in the background. Great work, Mark!” —Daniel R. Arfsten Pro/ENGINEER Drafter/Designer “I read a lot of Linux technical information every day, but I’m rarely impressed by tech books. I usually prefer online information sources instead. Mark Sobell’s books are a notable exception. They’re clearly written, technically accurate, comprehensive, and actually enjoyable to read.” —Matthew Miller Senior Systems Analyst/Administrator BU Linux Project Boston University Office of Information Technology “This is well-written, clear, comprehensive information for the Linux user of any type, whether trying Ubuntu on for the first time and wanting to know a little about it, or using the book as a very good reference when doing something more complicated like setting up a server. This book’s value goes well beyond its purchase price and it’ll make a great addition to the Linux section of your bookshelf.” —Linc Fessenden Host of The LinuxLink TechShow tllts.org “The author has done a very good job at clarifying such a detail-oriented operating system. I have extensive Unix and Windows experience and this text does an excellent job at bridging the gaps between Linux, Windows, and Unix. I highly recommend this book to both ‘newbs’ and experienced users. Great job!” —Mark Polczynski Information Technology Consultant “Your text, A Practical Guide to Ubuntu Linux®, Third Edition, is a well constructed, informative, superbly written text. You deserve an award for outstanding talent; unfortunately my name is not Pulitzer.” —Harrison Donnelly Physician “When I first started working with Linux just a short ten years or so ago, it was a little more difficult than now to get going. . . . Now, someone new to the community has a vast array of resources available on the web, or if they are inclined to begin with Ubuntu, they can literally find almost every single thing they will need in the single volume of Mark Sobell’s A Practical Guide to Ubuntu Linux®. “I’m sure this sounds a bit like hyperbole. Everything a person would need to know? Obviously not everything, but this book, weighing in at just under 1200 pages, covers so much so thoroughly that there won’t be much left out. From install to admin, networking, security, shell scripting, package management, and a host of other topics, it is all there. GUI and commandline tools are covered. There is not really any wasted space or fluff, just a huge amount of information. There are screen shots when appropriate but they do not take up an inordinate amount of space. This book is information-dense.” —JR Peck Editor GeekBook.org “I have been wanting to make the jump to Linux but did not have the guts to do so—until I saw your familiarly titled A Practical Guide to Red Hat® Linux® at the bookstore. I picked up a copy and am eagerly looking forward to regaining my freedom.” —Carmine Stoffo Machine and Process Designer to pharmaceutical industry “I am currently reading A Practical Guide to Red Hat® Linux® and am finally understanding the true power of the command line. I am new to Linux and your book is a treasure.” —Juan Gonzalez “Overall, A Practical Guide to Ubuntu Linux® by Mark G. Sobell provides all of the information a beginner to intermediate user of Linux would need to be productive. The inclusion of the Live DVD of the Gutsy Gibbon release of Ubuntu makes it easy for the user to test-drive Linux without affecting his installed OS. I have no doubts that you will consider this book money well spent.” —Ray Lodato Slashdot contributor For my great-nephew, Benjamin. Nice to have you around! Brief Contents Contents Preface 1 Welcome to Linux and Mac OS X Part I The Linux and Mac OS X Operating Systems 2 Getting Started 3 The Utilities 4 The Filesystem 5 The Shell Part II The Editors 6 The vim Editor 7 The emacs Editor Part III The Shells 8 The Bourne Again Shell (bash) 9 The TC Shell (tcsh) Part IV Programming Tools 10 Programming the Bourne Again Shell (bash) 11 The Perl Scripting Language 12 The Python Programming Language 13 The MySQL Database Management System 14 The AWK Pattern Processing Language 15 The sed Editor Part V Secure Network Utilities 16 The rsync Secure Copy Utility 17 The OpenSSH Secure Communication Utilities Part VI Command Reference Command Reference Part VII Appendixes A Regular Expressions B Help C Keeping the System Up-to-Date D Mac OS X Notes Glossary File Tree Index Utility Index Main Index Contents Preface Chapter 1: Welcome to Linux and Mac OS X The History of UNIX and GNU–Linux The Heritage of Linux: UNIX Fade to 1983 Next Scene, 1991 The Code Is Free Have Fun! What Is So Good About Linux? Why Linux Is Popular with Hardware Companies and Developers Linux Is Portable The C Programming Language Overview of Linux Linux Has a Kernel Programming Interface Linux Can Support Many Users Linux Can Run Many Tasks Linux Provides a Secure Hierarchical Filesystem The Shell: Command Interpreter and Programming Language A Large Collection of Useful Utilities Interprocess Communication System Administration Additional Features of Linux GUIs: Graphical User Interfaces (Inter)Networking Utilities Software Development Chapter Summary Exercises Part I The Linux and Mac OS X Operating Systems Chapter 2: Getting Started Conventions Used in This Book Logging In from a Terminal (Emulator) Working from the Command Line Which Shell Are You Running? Correcting Mistakes Repeating/Editing Command Lines / : Curbing Your Power (root Privileges) Where to Find Documentation : Displays the System Manual : Searches for a Keyword : Displays Information About Utilities The ––help Option The help Command Getting Help More About Logging In and Passwords What to Do If You Cannot Log In Logging In Remotely: Terminal Emulators, , and Dial-Up Connections Using Virtual Consoles Logging Out Changing Your Password Chapter Summary Exercises Advanced Exercises Chapter 3: The Utilities Special Characters Basic Utilities : Lists the Names of Files : Displays a Text File : Deletes a File Is : Display a Text File One Screen at a Time : Displays the System Name Working with Files : Copies a File : Changes the Name of a File : Prints a File : Searches for a String : Displays the Beginning of a File : Displays the End of a File : Displays a File in Order : Removes Duplicate Lines from a File : Compares Two Files : Identifies the Contents of a File | (Pipeline): Communicates Between Processes Four More Utilities : Displays Text : Displays the Time and Date : Records a Shell Session : Converts Linux Files to Windows and Macintosh OS X Format Compressing and Archiving Files : Compresses a File and : Decompress a File : Compresses a File : Packs and Unpacks Archives Locating Utilities and : Locate a Utility : Searches for a File Displaying User and System Information : Lists Users on the System : Lists Users on the System : Displays System Load and Duration Information : Lists Users on the System : Displays Memory Usage Information Communicating with Other Users : Sends a Message : Denies or Accepts Messages Email Chapter Summary Exercises Advanced Exercises Chapter 4: The Filesystem The Hierarchical Filesystem Directory Files and Ordinary Files Filenames The Working Directory Your Home Directory Pathnames Absolute Pathnames Relative Pathnames Working with Directories : Creates a Directory : Changes to Another Working Directory : Deletes a Directory Using Pathnames , : Move or Copy Files : Moves a Directory Important Standard Directories and Files Access Permissions ls –l: Displays Permissions : Changes Access Permissions Setuid and Setgid Permissions Directory Access Permissions ACLs: Access Control Lists Enabling ACLs Working with Access Rules Setting Default Rules for a Directory Links Hard Links Symbolic Links : Removes a Link Dereferencing Symbolic Links Chapter Summary Exercises Advanced Exercises Chapter 5: The Shell The Command Line A Simple Command Syntax Simple Commands Processing the Command Line Executing a Command Editing the Command Line Standard Input and Standard Output The Screen as a File The Keyboard and Screen as Standard Input and Standard Output Redirection Pipelines Lists Running a Command in the Background Filename Generation/Pathname Expansion The ? Special Character The * Special Character The [ ] Special Characters Builtins Chapter Summary Utilities and Builtins Introduced in This Chapter Exercises Advanced Exercises Part II The Editors Chapter 6: The History Editor Tutorial: Using to Create and Edit a File Starting Command and Input Modes Entering Text Getting Help Ending the Editing Session The compatible Parameter Introduction to Features Online Help Terminology Modes of Operation The Display Correcting Text as You Insert It Work Buffer Line Length and File Size Windows File Locks Abnormal Termination of an Editing Session Recovering Text After a Crash Command Mode: Moving the Cursor Moving the Cursor by Characters Moving the Cursor to a Specific Character Moving the Cursor by Words Moving the Cursor by Lines Moving the Cursor ...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture