Statistics
3
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-10
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
(This page has no text content)
PRAISE FOR THE LINUX COMMAND LINE “The most approachable tome on the subject.” —FEDERICO LUCIFREDI, LINUX MAGAZINE “This excellent Linux command line book is more than cubicle decoration; it’s a secret superpower.” —KEN HESS, RED HAT “I can honestly say I have found the beginner’s guide to Linux.” —JAYSON BROUGHTON, LINUX JOURNAL “This is the best introduction to the command line I have read.” —BEGINLINUX.COM “For those looking to master the Linux command line and get an essential understanding of the core Linux command line tools, this book is a highly effective and useful guide.” —BEN ROTHKE, RSA CONFERENCE “Anyone who reads this book and makes use of the examples provided will not be able to avoid becoming a Unix command line pro by the time they’ve hit the end of the book. It provides an excellent introduction to the command line that takes students from knowing nearly nothing to using impressively sophisticated commands.” —SANDRA HENRY-STOCKER, ITWORLD “Thorough and approachable. A great resource for those new to Linux and for seasoned Linux veterans wanting to dive deeper.” —PHILIP POLSTRA, PHD, AUTHOR OF LINUX FORENSICS “It trains you to think like the system thinks—not just run commands until something works.” —LINUXSECURITY.COM
THE LINUX COMMAND LINE 3rd Edition A Complete Introduction by William Shotts no starch press® San Francisco
THE LINUX COMMAND LINE, 3RD EDITION. Copyright © 2026 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. First printing 29 28 27 26 25 1 2 3 4 5 ISBN-13: 978-1-7185-0452-3 (print) ISBN-13: 978-1-7185-0453-0 (ebook) Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Jennifer Kepler Developmental Editor: Jill Franklin Cover Illustrator: Rob Fiore Interior Design: Octopod Studios Technical Reviewer: Mitch Frazier Copyeditor: Kim Wimpsett Indexer: BIM Creatives, LLC The Library of Congress has catalogued the first edition as follows: 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 For customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.com. For permission to translate this work: rights@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b-14, 11317 Tallinn, Estonia, hello@eucompliancepartner.com, +3375690241. No Starch Press and the No Starch Press iron 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.
For Karen
About the Author William Shotts is the former vice president of technical services at Media Cybernetics LLC. Now retired, he started his computing career in 1978 when he acquired his first computer. Since then, he has been a teacher, a photographer, a programmer, and a technical manager of diverse teams of technical support specialists, software quality assurance testers, system administrators, and technical writers. He earned a bachelor of industrial design degree from Syracuse University. He has been a Linux user and advocate since 1996 and is the creator of LinuxCommand.org.
About the Technical Reviewer Mitch Frazier is a programmer who works for Emerson Electric doing mostly embedded systems programming in C. He also occasionally writes code in Golang, Python, Tcl, JavaScript, and bash. He previously worked for Linux Journal, as both a technical editor and a system administrator.
BRIEF CONTENTS Acknowledgments Introduction PART I: LEARNING THE SHELL Chapter 1: What Is the Shell? Chapter 2: Navigation Chapter 3: Exploring the System Chapter 4: Manipulating Files and Directories Chapter 5: Working with Commands Chapter 6: Redirection Chapter 7: Seeing the World as the Shell Sees It Chapter 8: Advanced Keyboard Tricks Chapter 9: Permissions Chapter 10: Processes PART II: CONFIGURATION AND THE ENVIRONMENT Chapter 11: The Environment Chapter 12: A Gentle Introduction to vi(m) Chapter 13: Customizing the Prompt PART III: COMMON TASKS AND ESSENTIAL TOOLS Chapter 14: Package Management Chapter 15: Storage Media Chapter 16: Networking Chapter 17: Searching for Files Chapter 18: Archiving and Backup Chapter 19: Regular Expressions Chapter 20: Text Processing Chapter 21: Formatting Output Chapter 22: Printing Chapter 23: Compiling Programs
PART IV: WRITING SHELL SCRIPTS Chapter 24: Writing Your First Script Chapter 25: Starting a Project Chapter 26: Top-Down Design Chapter 27: Flow Control: Branching with if Chapter 28: Reading Keyboard Input Chapter 29: Flow Control: Looping with while/until Chapter 30: Troubleshooting Chapter 31: Flow Control: Branching with case Chapter 32: Positional Parameters Chapter 33: Flow Control: Looping with for Chapter 34: Strings and Numbers Chapter 35: Arrays Chapter 36: Exotica Index
CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Why Use the Command Line? What This Book Is About Who Should Read This Book What’s in This Book How to Read This Book Prerequisites What’s New in the Third Print Edition Your Feedback Is Needed! PART I: LEARNING THE SHELL 1 WHAT IS THE SHELL? Terminal Emulators Making Your First Keystrokes Command History Cursor Movement Try Some Simple Commands Ending a Terminal Session Summing Up 2 NAVIGATION Understanding the Filesystem Tree The Current Working Directory Listing the Contents of a Directory Changing the Current Working Directory Absolute Pathnames Relative Pathnames
Some Helpful Shortcuts Summing Up 3 EXPLORING THE SYSTEM More Fun with ls Options and Arguments A Longer Look at Long Format Determining a File’s Type with file Viewing File Contents with less Taking a Guided Tour Symbolic Links Hard Links Summing Up 4 MANIPULATING FILES AND DIRECTORIES Wildcards Dot Files mkdir—Create Directories cp—Copy Files and Directories Useful Options and Examples mv—Move and Rename Files Useful Options and Examples rm—Remove Files and Directories Useful Options and Examples ln—Create Links Hard Links Symbolic Links Let’s Build a Playground Creating Directories Copying Files Moving and Renaming Files Creating Hard Links Creating Symbolic Links Removing Files and Directories Summing Up
5 WORKING WITH COMMANDS What Exactly Are Commands? Identifying Commands type—Display a Command’s Type which—Display an Executable’s Location Getting a Command’s Documentation help—Get Help for Shell Builtins --help—Display Usage Information man—Display a Program’s Manual Page apropos—Display Appropriate Commands whatis—Display One-Line Manual Page Descriptions info—Display a Program’s Info Entry README and Other Program Documentation Files Creating Our Own Commands with alias Summing Up 6 REDIRECTION Standard Input, Output, and Error Redirecting Standard Output Group Commands Redirecting Standard Error Redirecting Standard Output and Standard Error to One File Disposing of Unwanted Output Redirecting Standard Input cat—Concatenate Files Pipelines Filters uniq—Report or Omit Repeated Lines wc—Print Line, Word, and Byte Counts grep—Print Lines Matching a Pattern head/tail—Print First/Last Part of Files tee—Read from Stdin and Output to Stdout and Files Summing Up 7 SEEING THE WORLD AS THE SHELL SEES IT
Expansion Pathname Expansion Tilde Expansion Arithmetic Expansion Brace Expansion Parameter Expansion Command Substitution Quoting Double Quotes Single Quotes Escaping Characters Backslash Escape Sequences Summing Up 8 ADVANCED KEYBOARD TRICKS Command Line Editing Cursor Movement Modifying Text Cutting and Pasting (Killing and Yanking) Text Command Completion Using History Searching History History Expansion Summing Up 9 PERMISSIONS Users, Group Members, and Everybody Else Reading, Writing, and Executing chmod—Change File Mode Setting File Mode with the GUI umask—Set Default Permissions Some Special Permissions Changing Identities su—Run a Shell with Substitute User and Group IDs sudo—Execute a Command as Another User chown—Change File Owner and Group
chgrp—Change Group Ownership Exercising Our Privileges Changing Your Password Summing Up 10 PROCESSES How a Process Works Viewing Processes Viewing Processes Dynamically with top Controlling Processes Interrupting a Process Putting a Process in the Background Returning a Process to the Foreground Stopping (Pausing) a Process Changing Process Priority Signals Sending Signals to Processes with kill Making a Process Hang-Up Proof Sending Signals to Multiple Processes with killall Shutting Down the System More Process-Related Commands Summing Up PART II: CONFIGURATION AND THE ENVIRONMENT 11 THE ENVIRONMENT What Is Stored in the Environment? Examining the Environment Some Interesting Variables How Is the Environment Established? What’s in a Startup File? Exploring How Child Processes Inherit Their Environments Launching a Program with a Temporary Environment Modifying the Environment Which Files Should We Modify?
Text Editors Using a Text Editor Activating Our Changes Summing Up 12 A GENTLE INTRODUCTION TO VI(M) Why We Should Learn vi A Little Background Starting and Stopping vi Editing Modes Entering Insert Mode Saving Our Work Moving the Cursor Around Basic Editing Appending Text Opening a Line Deleting Text Cutting, Copying, and Pasting Text Joining Lines Search-and-Replace Searching Within a Line Searching the Entire File Global Search-and-Replace Editing Multiple Files Switching Between Files Opening Additional Files for Editing Copying Content from One File into Another Inserting an Entire File into Another Saving Our Work bash Does vi Too Summing Up 13 CUSTOMIZING THE PROMPT Anatomy of a Prompt Trying Some Alternative Prompt Designs Adding Color
Moving the Cursor Saving the Prompt Summing Up PART III: COMMON TASKS AND ESSENTIAL TOOLS 14 PACKAGE MANAGEMENT Packaging Systems How a Package System Works Package Files Repositories Dependencies High- and Low-Level Package Tools Common Package Management Tasks Finding a Package in a Repository Installing a Package from a Repository Installing a Package from a Package File Removing a Package Updating Packages from a Repository Upgrading a Package from a Package File Listing Installed Packages Determining Whether a Package Is Installed Displaying Information About an Installed Package Finding Which Package Installed a File Summing Up 15 STORAGE MEDIA Mounting and Unmounting Storage Devices Viewing a List of Mounted Filesystems Determining Device Names Creating New Filesystems Manipulating Partitions with parted Creating a New Filesystem with mkfs Testing and Repairing Filesystems Moving Data Directly to and from Devices
Creating CD-ROM Images Creating an Image Copy of a CD-ROM Creating an Image from a Collection of Files Writing CD-ROM Images Mounting an ISO Image Directly Blanking a Rewritable CD-ROM Writing an Image Verifying Data Summing Up 16 NETWORKING Examining and Monitoring a Network ping traceroute ip Transporting Files Over a Network ftp lftp—A Better ftp curl—Transfer a URL wget—Non-Interactive Network Downloader Secure Communication with Remote Hosts ssh scp and sftp Summing Up 17 SEARCHING FOR FILES locate—Find Files the Easy Way find—Find Files the Hard Way Tests Operators Predefined Actions User-Defined Actions Improving Efficiency xargs A Return to the Playground Options
Summing Up 18 ARCHIVING AND BACKUP Compressing Files gzip bzip2 Archiving Files tar zip Synchronizing Files and Directories Using rsync Over a Network Summing Up 19 REGULAR EXPRESSIONS What Are Regular Expressions? grep Metacharacters and Literals The Any Character Anchors Bracket Expressions and Character Classes Negation Traditional Character Ranges POSIX Character Classes POSIX Basic vs. Extended Regular Expressions Alternation Quantifiers ?—Match an Element Zero or One Time *—Match an Element Zero or More Times +—Match an Element One or More Times { }—Match an Element a Specific Number of Times Putting Regular Expressions to Work Validating a Phone List with grep Finding Ugly Filenames with find Searching for Files with locate Searching for Text with less and vim Summing Up
20 TEXT PROCESSING Applications of Text Documents Web Pages Email Printer Output Program Source Code Revisiting Some Old Friends cat sort uniq Slicing and Dicing cut paste join tac rev Comparing Text comm diff patch Editing on the Fly tr sed aspell Summing Up Extra Credit 21 FORMATTING OUTPUT Simple Formatting Tools nl—Number Lines fold—Wrap Each Line to a Specified Length fmt—A Simple Text Formatter pr—Format Text for Printing printf—Format and Print Data Document Formatting Systems