The Linux Command Line Command Linux the the Linux Command Line Command Linux the Thethe Linuxlinux
Total Page:16
File Type:pdf, Size:1020Kb
BANISHBANISH YOURYOUR MOUSEMOUSE THE LINUX COMMAND LINE THE LINUX COMMAND LINE THETHE LINUXLINUX You’ve experienced the shiny, point-and-click surface • Use standard input and output, redirection, and COMMANDCOMMAND LINELINE of your Linux computer—now dive below and explore pipelines its depths with the power of the command line. • Edit files with Vi, the world’s most popular text editor A COMPLETE INTRODUCTION The Linux Command Line takes you from your very first • Write shell scripts to automate common or boring tasks terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you’ll learn • Slice and dice text files with cut, paste, grep, patch, the timeless skills handed down by generations of and sed WILLIAM E. SHOTTS, JR. gray-bearded, mouse-shunning gurus: file navigation, Once you overcome your initial “shell shock,” you’ll environment configuration, command chaining, pattern find that the command line is a natural and expressive matching with regular expressions, and more. way to communicate with your computer. Just don’t be In addition to that practical knowledge, author William surprised if your mouse starts to gather dust. Shotts reveals the philosophy behind these tools and ABOUT THE AUTHOR the rich heritage that your desktop Linux machine has inherited from Unix supercomputers of yore. William E. Shotts, Jr., has been a software professional and avid Linux user for more than 15 years. He has an As you make your way through the book’s short, easily extensive background in software development, including digestible chapters, you’ll learn how to: technical support, quality assurance, and documentation. • Create and delete files, directories, and symlinks He is also the creator of LinuxCommand.org, a Linux education and advocacy site featuring news, reviews, • Administer your system, including networking, and extensive support for using the Linux command line. package installation, and process management THE FINEST IN GEEK ENTERTAINMENT™ www.nostarch.com SHOTTS “I LIE FLAT.” $49.95 ($52.95 CDN) This book uses RepKover —a durable binding that won’t snap shut. COMPUTERS/LINUX SHELVE IN: FSC LOGO THE LINUX COMMAND LINE THE LINUX COMMAND LINE A Complete Introduction by William E. Shotts, Jr. San Francisco THE LINUX COMMAND LINE. Copyright © 2012 by William E. Shotts, Jr. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. 16 15 14 13 12 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-389-4 ISBN-13: 978-1-59327-389-7 Publisher: William Pollock Production Editor: Serena Yang Cover Design: Octopod Studios Developmental Editor: Keith Fancher Technical Reviewer: Therese Bao Copyeditor: Ward Webber Compositors: Serena Yang and Alison Law Proofreader: Paula L. Fleming For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; [email protected]; www.nostarch.com Library of Congress Cataloging-in-Publication Data Shotts, William E. The Linux command line: a complete introduction / William E. Shotts, Jr. p. cm. Includes index. ISBN-13: 978-1-59327-389-7 (pbk.) ISBN-10: 1-59327-389-4 (pbk.) 1. Linux. 2. Scripting Languages (Computer science) 3. Operating systems (Computers) I. Title. QA76.76.O63S5556 2011 005.4'32--dc23 2011029198 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. To Karen BRIEF CONTENTS Acknowledgments...................................................................................................................xxiii Introduction.............................................................................................................................xxv PART 1: LEARNING THE SHELL Chapter 1: What Is the Shell?.......................................................................................................3 Chapter 2: Navigation................................................................................................................7 Chapter 3: Exploring the System.................................................................................................13 Chapter 4: Manipulating Files and Directories..............................................................................25 Chapter 5: Working with Commands..........................................................................................39 Chapter 6: Redirection..............................................................................................................49 Chapter 7: Seeing the World as the Shell Sees It..........................................................................59 Chapter 8: Advanced Keyboard Tricks........................................................................................69 Chapter 9: Permissions..............................................................................................................77 Chapter 10: Processes...............................................................................................................95 PART 2: CONFIGURATION AND THE ENVIRONMENT Chapter 11: The Environment...................................................................................................109 Chapter 12: A Gentle Introduction to vi.....................................................................................121 Chapter 13: Customizing the Prompt.........................................................................................139 PART 3: COMMON TASKS AND ESSENTIAL TOOLS Chapter 14: Package Management..........................................................................................149 Chapter 15: Storage Media.....................................................................................................159 Chapter 16: Networking.........................................................................................................175 Chapter 17: Searching for Files................................................................................................187 Chapter 18: Archiving and Backup...........................................................................................201 Chapter 19: Regular Expressions..............................................................................................215 Chapter 20: Text Processing....................................................................................................233 Chapter 21: Formatting Output.................................................................................................267 Chapter 22: Printing...............................................................................................................285 Chapter 23: Compiling Programs.............................................................................................297 PART 4: WRITING SHELL SCRIPTS Chapter 24: Writing Your First Script........................................................................................309 Chapter 25: Starting a Project..................................................................................................315 Chapter 26: Top-Down Design.................................................................................................325 Chapter 27: Flow Control: Branching with if..............................................................................333 Chapter 28: Reading Keyboard Input........................................................................................347 Chapter 29: Flow Control: Looping with while and until..............................................................357 Chapter 30: Troubleshooting....................................................................................................363 Chapter 31: Flow Control: Branching with case.........................................................................375 Chapter 32: Positional Parameters............................................................................................381 Chapter 33: Flow Control: Looping with for...............................................................................393 Chapter 34: Strings and Numbers............................................................................................399 Chapter 35: Arrays.................................................................................................................415 Chapter 36: Exotica................................................................................................................423 Index.....................................................................................................................................433