Get The Linux Command Line: A Complete Introduction 2nd Edition William E. Shotts PDF Ebook With Full Chapters Now
Get The Linux Command Line: A Complete Introduction 2nd Edition William E. Shotts PDF Ebook With Full Chapters Now
Get The Linux Command Line: A Complete Introduction 2nd Edition William E. Shotts PDF Ebook With Full Chapters Now
com
https://textbookfull.com/product/the-linux-
command-line-a-complete-introduction-2nd-edition-
william-e-shotts/
https://textbookfull.com/product/bash-script-linux-command-line-
reference-1st-edition-aditya-tiwari/
textbookfull.com
https://textbookfull.com/product/mastering-linux-shell-scripting-a-
practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-ebrahim/
textbookfull.com
https://textbookfull.com/product/computing-with-the-raspberry-pi-
command-line-and-gui-linux-1st-edition-brian-schell/
textbookfull.com
https://textbookfull.com/product/life-to-the-limit-my-autobiography-
jenson-button/
textbookfull.com
https://textbookfull.com/product/strontium-contamination-in-the-
environment-pankaj-pathak/
textbookfull.com
https://textbookfull.com/product/nursing-in-critical-care-setting-an-
overview-from-basic-to-sensitive-outcomes-irene-comisso-et-al/
textbookfull.com
https://textbookfull.com/product/match-made-in-manhattan-1st-edition-
stauffer-amanda/
textbookfull.com
https://textbookfull.com/product/evolutionary-games-in-natural-social-
and-virtual-worlds-1st-edition-friedman/
textbookfull.com
Competition Law in China: A Law and Economics Perspective
Jingyuan Ma
https://textbookfull.com/product/competition-law-in-china-a-law-and-
economics-perspective-jingyuan-ma/
textbookfull.com
E
2 TI
D
N O
I
D N
THE LINU X
BA NISH YOUR OVER 100,000
MOUSE COPIES SOLD 2ND EDITION
CO M M A N D L I N E
T H E L IN U X COMM A ND L INE
The Linux Command Line will take you from your first • Administer your system, from networking and
terminal keystrokes to writing full programs in the latest package installation to process management
version of bash, the most popular Linux shell. A COMPLETE IN T RODUC T ION
• Use standard input and output, redirection, and
The second edition of this worldwide best seller pipelines
includes coverage of bash 4.x features like new re-
• Edit files with vi, the world’s most popular text editor
direction operators and shell expansions. An updated
WILLIAM SHOT TS
shell scripting section discusses modern, robust shell • Write shell scripts to automate common or boring tasks
scripting practices and ways of avoiding common
• Slice and dice text files with cut, paste, grep, patch,
types of potentially dangerous failures.
and sed
As with the first edition, you’ll learn timeless command
Once you’ve overcome your initial “shell shock,” you’ll
line skills like file navigation, environment configuration,
find that the command line is a natural and expressive
command chaining, and pattern matching with regular
way to communicate with your computer.
expressions. You’ll even explore the philosophy behind
the many command line tools and the rich heritage that ABOUT THE AUTHOR
your Linux machine inherits from Unix supercomputers
William Shotts has been a software professional and
of yore—all handed down by generations of mouse-
avid Linux user for more than 15 years. He has an
shunning gurus.
extensive background in software development, including
As you work through the book’s short, easily digestible technical support, quality assurance, and documentation.
chapters, you’ll learn how to: He is also the creator of LinuxCommand.org, a Linux
education and advocacy site featuring news, reviews,
• Create and delete files, directories, and symlinks
and extensive support for using the Linux command line.
T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™
w w w.nostarch.com SHOT TS
b y W il l ia m S ho t t s
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
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.
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.
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Chapter 2: Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter 6: Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Chapter 9: Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
x Brief Contents
Conte nt s in De ta il
Acknowledgments xxiii
First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Introduction xxv
Why Use the Command Line? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
What This Book Is About . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
What’s New in the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Your Feedback Is Needed! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
1
What Is the Shell? 3
Terminal Emulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Making Your First Keystrokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Cursor Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Try Some Simple Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Ending a Terminal Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2
Navigation 7
Understanding the File System Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Current Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Listing the Contents of a Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Changing the Current Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Absolute Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Relative Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Some Helpful Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3
Exploring the System 13
More Fun with ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Options and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
A Longer Look at Long Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Determining a File’s Type with file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Viewing File Contents with less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Taking a Guided Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4
Manipulating Files and Directories 23
Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
mkdir—Create Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
cp—Copy Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Useful Options and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
mv—Move and Rename Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Useful Options and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
rm—Remove Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Useful Options and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
ln—Create Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Building a Playground . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Creating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Copying Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Moving and Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Creating Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Creating Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Removing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5
Working with Commands 39
What Exactly Are Commands? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Identifying Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
type—Display a Command’s Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
which—Display an Executable’s Location . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Getting a Command’s Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
help—Get Help for Shell Builtins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
--help—Display Usage Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
man—Display a Program’s Manual Page . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
apropos—Display Appropriate Commands . . . . . . . . . . . . . . . . . . . . . . . . . . 44
whatis—Display One-line Manual Page Descriptions . . . . . . . . . . . . . . . . . . . 44
info—Display a Program’s Info Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
README and Other Program Documentation Files . . . . . . . . . . . . . . . . . . . . . 46
xii Contents in Detail
Creating Our Own Commands with alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6
Redirection 49
Standard Input, Output, and Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Redirecting Standard Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Redirecting Standard Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Redirecting Standard Output and Standard Error to One File . . . . . . . . . . . . . . 52
Disposing of Unwanted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Redirecting Standard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
cat: Concatenate Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
uniq: Report or Omit Repeated Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
wc: Print Line, Word, and Byte Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
grep: Print Lines Matching a Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
head/tail: Print First/Last Part of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
tee: Read from Stdin and Output to Stdout and Files . . . . . . . . . . . . . . . . . . . . 59
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7
Seeing the World as the Shell Sees It 61
Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Pathname Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Tilde Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Arithmetic Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Brace Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Parameter Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Double Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Single Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Escaping Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Backslash Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8
Advanced Keyboard Tricks 73
Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Cursor Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Modifying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Cutting and Pasting (Killing and Yanking) Text . . . . . . . . . . . . . . . . . . . . . . . . 75
Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Using History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Searching History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
History Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Contents in Detail xiii
9
Permissions 81
Owners, Group Members, and Everybody Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Reading, Writing, and Executing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
chmod: Change File Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Setting File Mode with the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
umask: Set Default Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Some Special Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Changing Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
su: Run a Shell with Substitute User and Group IDs . . . . . . . . . . . . . . . . . . . . . 92
sudo: Execute a Command As Another User . . . . . . . . . . . . . . . . . . . . . . . . . 93
chown: Change File Owner and Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
chgrp: Change Group Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Exercising Our Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Changing Your Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
10
Processes 99
How a Process Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Viewing Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Viewing Processes Dynamically with top . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Controlling Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Interrupting a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Putting a Process in the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Returning a Process to the Foreground . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Stopping (Pausing) a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Sending Signals to Processes with kill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Sending Signals to Multiple Processes with killall . . . . . . . . . . . . . . . . . . . . . 109
Shutting Down the System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
More Process-Related Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
11
The Environment 115
What Is Stored in the Environment? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Examining the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Some Interesting Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
How Is the Environment Established? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
What’s in a Startup File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Modifying the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Which Files Should We Modify? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
xiv Contents in Detail
Using a Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Activating Our Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
12
A Gentle Introduction to vi 127
Why We Should Learn vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
A Little Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Starting and Stopping vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Editing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Entering Insert Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Saving Our Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Moving the Cursor Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Basic Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Appending Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Opening a Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Deleting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Cutting, Copying, and Pasting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Joining Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Search-and-Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Searching Within a Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Searching the Entire File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Global Search-and-Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Editing Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Switching Between Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Opening Additional Files for Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Copying Content from One File into Another . . . . . . . . . . . . . . . . . . . . . . . . 141
Inserting an Entire File into Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Saving Our Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
13
Customizing the Prompt 145
Anatomy of a Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Trying Some Alternative Prompt Designs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Adding Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Moving the Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Saving the Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
14
Package Management 155
Packaging Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
How a Package System Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Package Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Contents in Detail xv
Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
High- and Low-Level Package Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Common Package Management Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Finding a Package in a Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Installing a Package from a Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Installing a Package from a Package File . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Removing a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Updating Packages from a Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Upgrading a Package from a Package File . . . . . . . . . . . . . . . . . . . . . . . . . 160
Listing Installed Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Determining Whether a Package Is Installed . . . . . . . . . . . . . . . . . . . . . . . . 161
Displaying Information About an Installed Package . . . . . . . . . . . . . . . . . . . 161
Finding Which Package Installed a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
15
Storage Media 165
Mounting and Unmounting Storage Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Viewing a List of Mounted File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Determining Device Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Creating New File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Manipulating Partitions with fdisk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Creating a New File System with mkfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Testing and Repairing File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Moving Data Directly to and from Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Creating CD-ROM Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Creating an Image Copy of a CD-ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Creating an Image from a Collection of Files . . . . . . . . . . . . . . . . . . . . . . . . 178
Writing CD-ROM Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Mounting an ISO Image Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Blanking a Rewritable CD-ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Writing an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
16
Networking 181
Examining and Monitoring a Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
ping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
traceroute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
ip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
netstat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Transporting Files over a Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
ftp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
lftp—a Better ftp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
wget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Secure Communication with Remote Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
ssh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
scp and sftp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
xvi Contents in Detail
17
Searching for Files 195
locate—Find Files the Easy Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
find—Find Files the Hard Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Predefined Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
User-Defined Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Improving Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
A Return to the Playground . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
find Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
18
Archiving and Backup 209
Compressing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
bzip2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Archiving Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
tar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
zip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Synchronizing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Using rsync over a Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
19
Regular Expressions 225
What Are Regular Expressions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Metacharacters and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
The Any Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Bracket Expressions and Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Traditional Character Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
POSIX Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
POSIX Basic vs. Extended Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Alternation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
? —Match an Element Zero or One Time . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
*—Match an Element Zero or More Times . . . . . . . . . . . . . . . . . . . . . . . . . 237
+—Match an Element One or More Times . . . . . . . . . . . . . . . . . . . . . . . . . . 238
{ }—Match an Element a Specific Number of Times . . . . . . . . . . . . . . . . . . . 238
Putting Regular Expressions to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Validating a Phone List with grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Finding Ugly Filenames with find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Searching for Files with locate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Searching for Text with less and vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Contents in Detail xvii
20
Text Processing 245
Applications of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Printer Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Program Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Revisiting Some Old Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
uniq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Slicing and Dicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
cut—Remove Sections from Each Line of Files . . . . . . . . . . . . . . . . . . . . . . . 256
paste—Merge Lines of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
join—Join Lines of Two Files on a Common Field . . . . . . . . . . . . . . . . . . . . . 260
Comparing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
comm—Compare Two Sorted Files Line by Line . . . . . . . . . . . . . . . . . . . . . . 262
diff—Compare Files Line by Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
patch—Apply a diff to an Original . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Editing on the Fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
tr—Transliterate or Delete Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
sed—Stream Editor for Filtering and Transforming Text . . . . . . . . . . . . . . . . . 268
aspell—Interactive Spellchecker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
21
Formatting Output 279
Simple Formatting Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
nl—Number Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
fold—Wrap Each Line to a Specified Length . . . . . . . . . . . . . . . . . . . . . . . . 282
fmt—A Simple Text Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
pr—Format Text for Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
printf—Format and Print Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Document Formatting Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
groff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
22
Printing 297
A Brief History of Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Printing in the Dim Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Character-Based Printers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Graphical Printers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Printing with Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Preparing Files for Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
pr—Convert Text Files for Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
xviii Contents in Detail
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Sending a Print Job to a Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
lpr—Print Files (Berkeley Style) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
lp—Print Files (System V Style) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Another Option: a2ps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Monitoring and Controlling Print Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
lpstat—Display Print System Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
lpq—Display Printer Queue Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
lprm/cancel—Cancel Print Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
23
Compiling Programs 309
What Is Compiling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Are All Programs Compiled? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Compiling a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Obtaining the Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Examining the Source Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Building the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Installing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
24
Writing Your First Script 323
What Are Shell Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
How to Write a Shell Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Script File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Executable Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Script File Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Good Locations for Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
More Formatting Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Long Option Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Indentation and Line Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
25
Starting a Project 329
First Stage: Minimal Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Second Stage: Adding a Little Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Assigning Values to Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . 334
Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Contents in Detail xix
26
Top-Down Design 339
Shell Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Keep Scripts Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
27
Flow Control: Branching with if 349
if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
File Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
String Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Integer Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
A More Modern Version of test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
(( ))—Designed for Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Combining Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Control Operators: Another Way to Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
28
Reading Keyboard Input 363
read—Read Values from Standard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
IFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Validating Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
29
Flow Control: Looping with while/until 375
Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
until . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Reading Files with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
30
Troubleshooting 383
Syntactic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Missing Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Missing or Unexpected Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Unanticipated Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
xx Contents in Detail
Logical Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Defensive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Watch Out for Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Verifying Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Finding the Problem Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Examining Values During Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
31
Flow Control: Branching with case 395
The case Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Performing Multiple Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
32
Positional Parameters 401
Accessing the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Determining the Number of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
shift—Getting Access to Many Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Simple Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Using Positional Parameters with Shell Functions . . . . . . . . . . . . . . . . . . . . . 405
Handling Positional Parameters en Masse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
A More Complete Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
33
Flow Control: Looping with for 413
for: Traditional Shell Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
for: C Language Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
34
Strings and Numbers 419
Parameter Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Basic Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Expansions to Manage Empty Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Expansions That Return Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Case Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Arithmetic Evaluation and Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Number Bases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Simple Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Contents in Detail xxi
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Bit Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
bc—An Arbitrary Precision Calculator Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Using bc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
An Example Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
35
Arrays 437
What Are Arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Assigning Values to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Outputting the Entire Contents of an Array . . . . . . . . . . . . . . . . . . . . . . . . . 441
Determining the Number of Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . 441
Finding the Subscripts Used by an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Adding Elements to the End of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Deleting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
36
Exotica 447
Group Commands and Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Process Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Traps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Asynchronous Execution with wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Named Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Setting Up a Named Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Using Named Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Index 459
xxii Contents in Detail
Acknowledgments
I want to thank the following people, who helped make this book possible.
First Edition
First, the people who inspired me:
Jenny Watson, acquisitions editor at Wiley Publishing who originally
suggested that I write a shell scripting book.
John C. Dvorak, noted columnist and pundit. In an episode of his video
podcast, Cranky Geeks, Mr. Dvorak described the process of writing: “Hell.
Write 200 words a day, and in a year, you have a novel.” This advice led me
to write a page a day until I had a book.
Dmitri Popov wrote an article in Free Software Magazine titled “Creating
a book template with Writer,” which inspired me to use OpenOffice.org
Writer (and later, LibreOffice Writer) for composing the text. As it turned
out, it worked wonderfully.
Next, the volunteers who helped me produce the original, freely distrib-
uted version of this book (available at LinuxCommand.org):
Mark Polesky performed an extraordinary review and test of the text.
Random documents with unrelated
content Scribd suggests to you:
HOUSE OF JACQUES CŒUR, BOURGES
THE exterior of the Cathedral of Toledo is far less ornate than that of
the Cathedral of Burgos: it has no efflorescence of ornaments, no
arabesques, and no collarette of statues enlivening the porches; it
has solid buttresses, bold and sharp angles, a thick facing of stone,
a stolid tower, with no delicacies of the Gothic jewel-work, and it is
covered entirely with a reddish tint, like that of a piece of toast, or the
sunburnt skin of a pilgrim from Palestine; as if to make up the loss,
the interior is hollowed and sculptured like a grotto of stalactites.
The door by which we entered is of bronze, and bears the
following inscription: Antonio Zurreno del arte de oro y plata, faciebat
esta media puerta. The first impression is most vivid and imposing;
five naves divide the church: the middle one is of an immeasurable
height, and the others beside it seem to bow their heads and kneel in
token of admiration and respect; eighty-eight pillars, each as large as
a tower and each composed of sixteen spindle-shaped columns
bound together, sustain the enormous mass of the building; a
transept cuts the large nave between the choir and the high altar,
and forms the arms of the cross. The architecture of the entire
building is homogeneous and perfect, a very rare virtue in Gothic
cathedrals, which have generally been built at different periods; the
original plan has been adhered to from one end to the other, with the
exception of a few arrangements of the chapels, which, however, do
not interfere with the harmony of the general effect. The windows,
glittering with hues of emerald, sapphire, and ruby set in the ribs of
stone, worked like rings, sift in a soft and mysterious light which
inspires religious ecstasy; and, when the sun is too strong, blinds of
spartium are let down over the windows, and through the building is
then diffused that cool half-twilight which makes the churches of
Spain so favourable for meditation and prayer.
The high altar, or retablo, alone might pass for a church; it is an
enormous accumulation of small columns, niches, statues, foliage,
and arabesques, of which the most minute description would give
but a faint idea; all this sculpture, which extends up to the vaulted
roof and all around the sanctuary, is painted and gilded with
unimaginable wealth. The warm and tawny tones of the antique gold,
illumined by the rays and patches of light interrupted in their passage
by the tracery and projections of the ornaments, stand out superbly
and produce the most admirable effects of grandeur and richness.
The paintings, with their backgrounds of gold which adorn the panels
of this altar, equal in richness of colour the most brilliant Venetian
canvases; this union of colour with the severe and almost hieratic
forms of mediæval art is rarely found; some of these paintings might
be taken for Giorgione’s first manner.
THE CATHEDRAL OF TOLEDO.