THE LINUX COMMAND LINE T H E L I N U X C O M M A N D L I N E 2 N D E D I T I O N A Complete Introduction by William Shotts San Francisco THE LINUX COMMAND LINE, 2ND EDITION. Copyright © 2019 by William Shotts. 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. ISBN-10: 1-59327-952-3 ISBN-13: 978-1-59327-952-3 Publisher: William Pollock Production Editors: Meg Sneeringer and Serena Yang Cover Illustration: Octopod Studios Developmental Editor: Chris Cleveland Technical Reviewer: Jordi Gutiérrez Hermoso Copyeditor: Kim Wimpsett Compositors: Britt Bogan and Meg Sneeringer Proofreader: James Fraleigh For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; [email protected] www.nostarch.com The Library of Congress issued the following Cataloging-in-Publication Data for the first edition: 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. [S] To Karen About the Author William Shotts has been a software professional for more than 30 years and an avid Linux user for more than 20 years. He has an extensive background in software development, including technical support, quality assurance, and documentation. He is also the creator of LinuxCommand.org, a Linux education and advocacy site featuring news, reviews, and extensive support for using the Linux command line. About the Technical Reviewer Jordi Gutiérrez Hermoso is a coder, mathematician, and hacker-errant. He runs Debian GNU/Linux exclusively since 2002, both at home and at work. Jordi has been involved with GNU Octave, a free numerical computing envi- ronment largely compatible with Matlab, and with Mercurial, a distributed version control system. He enjoys pure and applied mathematics, skating, swimming, and knitting. Nowadays he thinks a lot about environmental mapping, greenhouse gas emissions, and rhino conservation efforts. BRIEF CONTENTS Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv PART I: 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 5: Working with Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 6: Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chapter 7: Seeing the World as the Shell Sees It . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Chapter 8: Advanced Keyboard Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Chapter 9: Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Chapter 10: Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 PART II: CONFIGURATION AND THE ENVIRONMENT Chapter 11: The Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Chapter 12: A Gentle Introduction to vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Chapter 13: Customizing the Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 PART III: COMMON TASKS AND ESSENTIAL TOOLS Chapter 14: Package Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Chapter 15: Storage Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Chapter 16: Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Chapter 17: Searching for Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Chapter 18: Archiving and Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209