Mastering Modern Linux Second Edition Mastering Modern Linux Second Edition Paul S. Wang Kent State University, Kent, Ohio CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2018 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed on acid-free paper Version Date: 20180420 International Standard Book Number-13: 978-0-8153-8098-6 (Paperback) International Standard Book Number-13: 978-0-8153-8111-2 (Hardback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging-in-Publication Data Names: Wang, Paul S., author. Title: Mastering modern Linux / Paul S. Wang. Other titles: Mastering Linux Description: Second edition. | Boca Raton : Taylor & Francis, CRC Press, 2018. | Revised edition of: Mastering Linux / Paul S. Wang. 2011. | Includes bibliographical references and index. Identifiers: LCCN 2018008944| ISBN 9780815380986 (pbk. : alk. paper) | ISBN 9780815381112 (hardback) Subjects: LCSH: Linux. | Operating systems (Computers) Classification: LCC QA76.774.L46 W36 2018 | DDC 005.4/46--dc23 LC record available at https://lccn.loc.gov/2018008944 Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents Preface Introduction CHAPTER 1 ■A Linux Primer 1.1 What Is an Operating System? 1.2 Getting Started: Login and Logout Desktop Login Starting a Terminal Window Remote Login 1.3 Understanding the Shell Entering Commands Trying a Few Commands Correcting Typing Mistakes Aborting a Command 1.4 Exercise A 1.5 Using Files and Directories Current Working Directory and Filenames Handling Files and Directories Standard Personal Directories 1.6 Protecting Files: Access Control The Super User Examining the Permission Settings Setting Permissions 1.7 Exercise B 1.8 Text Editing 1.9 Getting Hard/Saved Copies 1.10 Communicating with Others Who’s Who on the System: finger Email 1.11 Browsing the Web 1.12 Exercise C 1.13 Creating and Running Your Own Program Compiling 1.14 Exercise D 1.15 Consulting Linux Documentation 1.16 Exercise E 1.17 Rounding Up Useful Commands 1.18 Summary CHAPTER 2 ■The Desktop Environment 2.1 Desktop Overview 2.2 Desktop Components 2.3 The GNOME 3 Desktop 2.4 Understanding GUI 2.5 Working with the Desktop Session Control and System Settings Launching Application Programs Managing Files Multiple Workspaces Desktop Appearance 2.6 Windows The X Window System Window Manager Window Information 2.7 The File Browser Navigating the File Tree Opening a File or Folder Finding Files Managing Files and Folders Access Control for Files and Folders Writing CDs or DVDs Changing Your Default File Browser 2.8 Terminal Window Starting a GNOME Terminal Terminal Window and the Shell Select, Copy, and Paste Web and Email Links 2.9 Accessing Help and Documentation 2.10 Summary 2.11 Exercises CHAPTER 3 ■Interacting with the BASH Shell 3.1 Bash 3.2 Interacting with Bash 3.3 Command-Line Editing and Command Completion 3.4 Bash Command Execution 3.5 Bash Input/Output Redirection Standard Input and Output I/O Redirection Pipes 3.6 Bash Job Control 3.7 Bash Shell Expansions History Expansion Alias Expansion Brace and Tilde Expansions Variable Expansion Command Expansion Process Expansion Filename Expansion 3.8 Bash Built-in Commands 3.9 Shell Variables 3.10 Environment of a Program Command Execution Environment 3.11 Examples of Bash Usage Customized Prompt Removing Files Safely Copy, Paste, and I/O Redirection Displaying Manual Pages Setting Up Your Personal Web Folder 3.12 Default File Permissions 3.13 Shell Startup and Initialization 3.14 Shell Special Characters and Quoting Quoting in Bash 3.15 Simple Functions 3.16 For More Information 3.17 Summary 3.18 Exercises CHAPTER 4 ■Putting Commands and Applications to Use 4.1 Useful GUI Apps Word Processing Document Formatting and Typesetting Drawing and Diagramming Raster Graphics and Image Processing File Upload and Download Password Manager Cloud Storage 3D Modeling and 3D Printing Mathematical Calculations 4.2 Commands and Filters Leading and Trailing Lines: head and tail Character Translation: tr Tab Expansion Folding Text Lines Calendar Reminders by Email Sorting Text Lines 4.3 The grep Command 4.4 Regular Expressions Quoting in Search Patterns 4.5 Patterns for grep 4.6 A Stream Editor: sed 4.7 Building Pipelines Address Processing 4.8 For More Information 4.9 Summary 4.10 Exercises CHAPTER 5 ■Writing BASH Scripts 5.1 Invoking Shell Scripts 5.2 A First Shell Script 5.3 Shell Script Execution 5.4 Positional Parameters 5.5 The for Command 5.6 The if Command 5.7 Test Expressions and Exit Status Exit Status Test Expressions 5.8 The shift Command 5.9 The case Command 5.10 The while and until Commands 5.11 Numerical Expressions 5.12 The break and continue Commands 5.13 File Queries 5.14 Variables 5.15 Arrays 5.16 Variable Modifiers 5.17 The Here Document 5.18 More on Functions Function Arguments Return Value of a Function 5.19 Redefining Bash Built-in Functions 5.20 Example Bash Scripts Example: Removing Unwanted Files Example: Conditional Copy Example: Total File Sizes Example: Secure File Transfer Example: Resizing Pictures 5.21 Debugging Shell Scripts 5.22 Error and Interrupt Handling Interrupt Handling 5.23 The Perl and PHP Alternatives 5.24 For More Information 5.25 Summary 5.26 Exercises CHAPTER 6 ■The File System 6.1 A File Location Road Map 6.2 File Types Ordinary Files Directories Special Files Links Symbolic Links 6.3 More on File Access Control Meaning of Permissions for a Directory 6.4 File Status File Mode File Userid and Groupid Access Control Enforcement Setuid and Setgid Modes Establishing a Group DAC and MAC