Get The Linux Command Line: A Complete Introduction 2nd Edition William E. Shotts PDF Ebook With Full Chapters Now

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Download the full version of the textbook now at textbookfull.

com

The Linux Command Line: A Complete


Introduction 2nd Edition William E. Shotts

https://textbookfull.com/product/the-linux-
command-line-a-complete-introduction-2nd-edition-
william-e-shotts/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

BASH SCRIPT Linux Command Line Reference 1st Edition


Aditya Tiwari

https://textbookfull.com/product/bash-script-linux-command-line-
reference-1st-edition-aditya-tiwari/

textbookfull.com

Mastering Linux Shell Scripting a practical guide to Linux


command line Bash scripting and Shell programming Ebrahim

https://textbookfull.com/product/mastering-linux-shell-scripting-a-
practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-ebrahim/
textbookfull.com

Computing with the Raspberry Pi: Command Line and GUI


Linux 1st Edition Brian Schell

https://textbookfull.com/product/computing-with-the-raspberry-pi-
command-line-and-gui-linux-1st-edition-brian-schell/

textbookfull.com

Applied Informatics and Cybernetics in Intelligent Systems


Proceedings of the 9th Computer Science On line Conference
2020 Volume 3 Radek Silhavy
https://textbookfull.com/product/applied-informatics-and-cybernetics-
in-intelligent-systems-proceedings-of-the-9th-computer-science-on-
line-conference-2020-volume-3-radek-silhavy/
textbookfull.com
Life to the Limit My Autobiography Jenson Button

https://textbookfull.com/product/life-to-the-limit-my-autobiography-
jenson-button/

textbookfull.com

Strontium Contamination in the Environment Pankaj Pathak

https://textbookfull.com/product/strontium-contamination-in-the-
environment-pankaj-pathak/

textbookfull.com

Nursing in Critical Care Setting: An Overview from Basic


to Sensitive Outcomes Irene Comisso Et Al.

https://textbookfull.com/product/nursing-in-critical-care-setting-an-
overview-from-basic-to-sensitive-outcomes-irene-comisso-et-al/

textbookfull.com

Match Made in Manhattan 1st Edition Stauffer Amanda

https://textbookfull.com/product/match-made-in-manhattan-1st-edition-
stauffer-amanda/

textbookfull.com

Evolutionary games in natural, social, and virtual worlds


1st Edition Friedman

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

$39.95 ($53.95 CDN)


COMPUTERS/LINUX
SHELVE IN:
The LInux Command Line
The Linux
Command Line
2 ND E d i t i o n
A Complete Introduction

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

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.
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.
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
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


Chapter 19: Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

Chapter 20: Text Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Chapter 21: Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Chapter 22: Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

Chapter 23: Compiling Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

PART IV: WRITING SHELL SCRIPTS


Chapter 24: Writing Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Chapter 25: Starting a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Chapter 26: Top-Down Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

Chapter 27: Flow Control: Branching with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

Chapter 28: Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

Chapter 29: Flow Control: Looping with while/until . . . . . . . . . . . . . . . . . . . . . . . . . 375

Chapter 30: Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Chapter 31: Flow Control: Branching with case . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

Chapter 32: Positional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Chapter 33: Flow Control: Looping with for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

Chapter 34: Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

Chapter 35: Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

Chapter 36: Exotica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

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

PART I: LEARNING THE SHELL

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

PART II: CONFIGURATION AND THE ENVIRONMENT

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

PART III: COMMON TASKS AND ESSENTIAL TOOLS

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

PART IV: WRITING SHELL SCRIPTS

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 plan of the building is an irregular pentagon, composed of


different bodies of buildings joined without any symmetry, according
to the general disposition of almost all mediæval civil and military
buildings. The large towers are Jacques Cœur’s original ones. One
was entirely reconstructed by him with the exception of the first story,
which is of Roman work, as the layers of brick and masonry indicate;
the other, on the contrary, received only its crown and a new interior
construction, and, like the first, was flanked by a tower destined to
serve as a cage for the stairway. The court of honour is vast, and
arranged so that it was easy to communicate with the different parts
of the hôtel.
The façade is composed of a pavilion flanked by two wings.
Following an arrangement borrowed from military architecture, two
doors were contrived, the little one for the foot-passengers and the
large one, which was the door of honour, through which the
Cavaliers entered. Both had pointed arches and were ornamented
with an archivolt with crockets. One of them still possessed, until
about a dozen years ago, its ancient sculptured panels and
ornamental iron-work. Above these doors is a large niche with very
rich ornamentation, which originally sheltered the equestrian statue
of Charles VII. On its right and left is a false window, in which you
see the statue of a man-servant in the one and that of a maid-
servant in the other, both in the costume of the period. Above this
niche the wall is pierced by a large window with four panes, whose
tracery reproduces hearts, armes parlantes of the proprietor, and a
fleur-de-lis, a sign of his recognition by King Charles. A cornice of
foliage forms the top of the wall of the pavilion, which is crowned by
a very high roof with four sloping and concave sides. Upon the front
and back faces of this roof is a large skylight-window and on its
lateral faces, a stock of chimneys. On the summit of the roof is an
imposing ridge which ends with two long spikes.
The back of the pavilion is exactly like the front, with the
exception of a statue of Cœur corresponding to that of the king. To
the right of the pavilion there rises an octagonal campanile of great
elegance; at its base is a balustrade in whose open-work runs a
phylactery, carrying the motto, which is frequently repeated in the
building and which characterizes perfectly him who adopted it:
11
À vaillans cœurs rien d’impossible.
Notwithstanding the mutilations to which the house of Jacques
Cœur has been condemned by its fate, it is certainly one of the most
interesting and best preserved of all the civil buildings of the Middle
Ages. A vast amount of information regarding the intimate life of the
people, which has so great an attraction for the archæologist, is to
be found here. If the fact that the study of buildings should be the
inseparable companion to that of history was less evident, the house
of Jacques Cœur would afford us an opportunity to demonstrate the
truth; in reality, when we have studied this building we certainly gain
a much clearer idea of the manners of Charles VII.’s reign than could
be obtained from a host of lecturers upon history.

Jules Gailhabaud, Monuments anciens et modernes. (Paris,


1865).
WAT PHRA KAO.
CARL BOCK.

THE first glimpse of Siam which the traveller obtains at Paknam is a


fair sample of what is to be seen pretty well throughout the country.
As Constantinople is called the City of Mosques, so Bangkok may,
with even more reason, be termed the City of Temples. And not in
Bangkok only and its immediate neighbourhood, but in the remotest
parts of the country, wherever a few people live now, or ever have
lived, a Wat with its image, or collection of images, of Buddha, is to
be found, surrounded by numberless phrachedees, those curious
structures which every devout Buddhist—and all Buddhists are in
one sense or another devout—erects at every turn as a means of
gaining favour with the deity, or of making atonement for his sins. On
the rich plains, in the recesses of the forests, on the tops of high
mountains, in all directions, these monuments of universal allegiance
to a faith which, more perhaps than any other, claims a devotee in
almost every individual inhabitant of the lands over which it has once
obtained sway, are to be found. The labour, the time, and the wealth
lavished upon these structures are beyond calculation....
The work which, in popular estimation at least, will make his
Majesty’s reign most memorable in Siam, is the completion and
dedication of the great royal temple, Phra Sri Ratana Satsadaram,
or, as it is usually called, Wat Phra Kao. The erection of this
magnificent pile of buildings was commenced by Phra Puttha Yot Fa
Chulalok, “as a temple for the Emerald Buddha, the palladium of the
capital, for the glory of the king, and as an especial work of royal
piety.” This temple was inaugurated with a grand religious festival in
the year Maseng, 7th of the cycle, 1147 (A. D. 1785), but, having
been very hastily got ready for the celebration of the third
anniversary of the foundation of the capital, it was incomplete, only
the church and library being finished. Various additions were made
from time to time, but the Wat remained in an unfinished state until
the present king came to the throne. The vow to complete the works
was made on Tuesday, the 23rd of December, 1879. The works were
commenced during the next month and completed on Monday, the
17th of April, 1882, a period of two years, three months, and twenty
days. Thus it was reserved for King Chulalonkorn, at an enormous
outlay, entirely defrayed out of his private purse, and by dint of great
exertions on the part of those to whom the work was immediately
entrusted, to complete this structure, and, on the hundredth
anniversary of the capital of Siam, to give the city its crowning glory.
WAT PHRA KAO.

The work was placed under the direct superintendence of the


king’s brothers, each of whom had a particular part of the work
allotted to him. One, for instance, relaid the marble pavement, and
decorated the Obosot with pictures of the sacred elephant; while a
second renewed the stone inscriptions inside the Obosot; a third laid
down a brass pavement in the Obosot; a fourth undertook to restore
all the inlaid pearl work; another undertook the work of repairing the
ceiling, paving, and wall-decoration, and made three stands for the
seals of the kingdom; another changed the decayed roof-beams;
another covered the great phrachedee with gold tiles—the effect of
which in the brilliant sunlight is marvellously beautiful—and repaired
and gilded all the small phrachedees; another renewed and repaired
and redecorated all the stone ornaments and flower-pots in the
temple-grounds, and made the copper-plated and gilt figures of
demons, and purchased many marble statues; two princes divided
between them the repairs of the cloisters, renewing the roof where
required, painting, gilding, paving with stone, and completing the
capitals of columns, and so on. Thus, by division of labour, under the
stimulus of devotion to the religion of the country, and of brotherly
loyalty to the king, the great work was at length completed, after
having been exactly one hundred years in course of construction. On
the 21st of April, 1882, the ceremony of final dedication was
performed, with the greatest pomp, and amid general rejoicings.
Under the name “Wat Phra Kao” are included various buildings
covering a large area of ground, which is surrounded by walls
decorated with elaborate frescoes. In the centre is a temple, called
the Phra Marodop, built in the form of a cross, where on festive
occasions the king goes to hear a sermon from the prince-high-
priest. The walls of this building are richly decorated with inlaid work,
and the ceiling painted with a chaste design in blue and gold. The
most striking feature, however, is the beautiful work in the ebony
doors, which are elaborately inlaid with mother-of-pearl figures
representing Thewedas, bordered by a rich scroll. Behind this
chapel-royal is the great phrachedee, called the Sri Ratana
Phrachedee, entirely covered with gilt tiles, which are specially made
for the purpose in Germany to the order of H. R. H. Krom Mun
Aditson Udom Det.
There are several other large buildings in the temple-grounds,
but the structure in which the interest of the place centres is the
Obosot, which shelters the famous “Emerald Buddha,” a green jade
figure of matchless beauty, which was found at Kiang Hai in A. D.
1436, and, after various vicissitudes of fortune, was at last placed in
safety in the royal temple at Bangkok. This image is, according to the
season of the year, differently attired in gold ornaments and robes.
The Emerald Buddha is raised so high up, at the very summit of a
high altar, that it is somewhat difficult to see it, especially as light is
not over plentiful, the windows being generally kept closely
shuttered. For the convenience of visitors, however, the attendants
will for a small fee open one or two of the heavy shutters, which are
decorated on the outside with gilt figures of Thewedas in contorted
attitudes. When at last the sun’s rays are admitted through the “dim
religious light,” and the beam of brightness shines on the
resplendent figure—enthroned above a gorgeous array of coloured
vases, with real flowers and their waxen imitations, of gold, silver,
and bronze representations of Buddha, of Bohemian glassware,
lamps, and candlesticks, with here and there a flickering taper still
burning, and surrounded with a profusion of many-storied umbrellas,
emblems of the esteem in which the gem is held—the scene is
remarkably beautiful, and well calculated to have a lasting effect on
the minds of those who are brought up to see in the calm, solemn,
and dignified form of Buddha the representation of all that is good
here, and the symbol of all happiness hereafter. The floor of the
Obosot is of tessellated brass, and the walls are decorated with the
usual perspectiveless frescoes, representing scenes in Siamese or
Buddhist history.
It is in this Obosot that the semi-annual ceremony of Tunam, or
drinking the water of allegiance, takes place, when the subjects of
Siam, through their representatives, and the princes and high
officers of state, renew or confirm their oath of allegiance. The
ceremony consists of drinking water sanctified by the priests, and
occurs twice a year—on the third day of the waxing of the Siamese
fifth month (i. e., the 1st of April), and on the thirteenth day of the
waning of the Siamese tenth month (i. e., the 21st of September).
The foregoing description gives but a faint idea of this sacred
and historic edifice, which will henceforth be regarded as a symbol of
the rule of the present Siamese dynasty, and the completion of which
will mark an epoch in Siamese history.

Temples and Elephants (London, 1884).


THE CATHEDRAL OF TOLEDO.
THÉOPHILE GAUTIER.

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.

Opposite to the high altar is placed the choir, or silleria,


according to the Spanish custom; it is composed of three rows of
stalls in sculptured wood, hollowed and carved in a marvellous
manner with historical, allegorical, and sacred bas-reliefs. Gothic Art,
on the borderland of the Renaissance, has never produced anything
more pure, more perfect, or better drawn. This work, the details of
which are appalling, has been attributed to the patient chisels of
Philippe de Bourgogne and Berruguete. The archbishop’s stall,
which is higher than the rest, is shaped like a throne and marks the
centre of the choir; this prodigious carpentry is crowned by gleaming
columns of brown jasper, and on the entablature stand alabaster
figures, also by Philippe de Bourgogne and Berruguete, but in a freer
and more supple style, elegant and admirable in effect. Enormous
bronze reading-desks supporting gigantic missals, large spartium
mats, and two colossal organs placed opposite to each other, one to
the right and one to the left, complete the decorations....
The Mozarabic Chapel, which is still in existence, is adorned with
Gothic frescoes of the highest interest: the subjects are the combats
between the Toledans and the Moors; they are in a state of perfect
preservation, their colours are as bright as if they had been laid on
yesterday, and by means of them an archæologist would gain a vast
amount of information regarding arms, costumes, accoutrements,
and architecture, for the principal fresco represents a view of old
Toledo, which is, doubtless, very accurate. In the lateral frescoes the
ships which brought the Arabs to Spain are painted in detail; a
seaman might gather much useful information from them regarding
the obscure history of the mediæval navy. The arms of Toledo—five
stars, sable on a field, argent—are repeated in several places in this
low-vaulted chapel, which, according to the Spanish fashion, is
enclosed by a grille of beautiful workmanship.
The Chapel of the Virgin, which is entirely faced with beautifully
polished porphyry, jasper, and yellow and violet breccia, is of a
richness surpassing the splendours of the Thousand and One
Nights; many relics are preserved here, among them a reliquary
presented by Saint Louis, which contains a piece of the True Cross.
To recover our breath, let us make, if you please, the tour of the
cloisters, whose severe yet elegant arcades surround beautiful
masses of verdure, kept green, notwithstanding the devouring heat
of this season, by the shadow of the Cathedral; the walls of this
cloister are covered with frescoes in the style of Vanloo, by a painter
named Bayeu. These compositions are simple and pleasing in
colour, but they do not harmonize with the style of the building, and
probably supplant ancient works damaged by centuries, or found too
Gothic for the people of good taste in that time. It is very fitting to
place a cloister near a church; it affords a happy transition from the
tranquillity of the sanctuary to the turmoil of the city. You can go to it
to walk about, to dream, or to reflect, without being forced to join in
the prayers and ceremonies of a cult; Catholics go to the temple,
Christians remain more frequently in the cloisters. This attitude of
mind has been perfectly understood by that marvellous psychologist
the Catholic Church. In religious countries the Cathedral is always
the most ornamented, richest, most gilded, and most florid of all
buildings in the town; it is there that one finds the coolest shade and
the deepest peace; the music there is better than in the theatre; and
it has no rival in pomp of display. It is the central point, the magnetic
spot, like the Opéra in Paris. We Catholics of the North, with our
Voltairean temples, have no idea of the luxury, elegance, and
comfort of the Spanish cathedrals; these churches are furnished and
animated, and have nothing of that glacial, desert-like appearance of
ours; the faithful can live in them on familiar terms with their God.
The sacristies and rooms of the Chapter in the Cathedral of
Toledo have a more than royal magnificence; nothing could be more
noble and picturesque than these vast halls decorated with that solid
and severe luxury of which the Church alone has the secret. Here
are rare carpentry-work in carved walnut or black oak, portières of
tapestry or Indian damask, curtains of brocatelle, with sumptuous
folds, figured brocades, Persian carpets, and paintings of fresco. We
will not try to describe them in detail; we will only speak of one room
ornamented with admirable frescoes depicting religious subjects in
the German style of which the Spaniards have made such
successful imitations, and which have been attributed to
Berruguete’s nephew, if not to Berruguete himself, for these
prodigious geniuses followed simultaneously three branches of art.
We will also mention an enormous ceiling by Luca Giordano, where
is collected a whole world of angels and allegorical figures in the
most rapidly executed foreshortening which produce a singular
optical illusion. From the middle of the roof springs a ray of light so
wonderfully painted on the flat surface that it seems to fall
perpendicularly on your head, no matter from which side you view it.
It is here that they keep the treasure, that is to say the beautiful
copes of brocade, cloth of gold and silver damask, the marvellous
laces, the silver-gilt reliquaries, the monstrances of diamonds, the
gigantic silver candlesticks, the embroidered banners,—all the
material and accessories for the representation of that sublime
Catholic drama which we called the Mass.
In the cupboards in one of the rooms is preserved the wardrobe
of the Holy Virgin, for cold, naked statues of marble or alabaster do
not suffice for the passionate piety of the Southern race; in their
devout transport they load the object of their worship with ornaments
of extravagant richness; nothing is good enough, brilliant enough, or
costly enough for them; under this shower of precious stones, the
form and material of the figure disappear: nobody cares about that.
The main thing is that it should be an impossibility to hang another
pearl in the ears of the marble idol, to insert another diamond in its
golden crown, or to trace another leaf of gems in the brocade of its
dress.
Never did an ancient queen,—not even Cleopatra who drank
pearls,—never did an empress of the Lower Empire, never did a
Venetian courtesan in the time of Titian, possess more brilliant jewels
nor a richer wardrobe than Our Lady of Toledo. They showed us
some of her robes: one of them left you no idea as to the material of
which it was made, so entirely was it covered with flowers and
arabesques of seed-pearls, among which there were others of a size
beyond all price and several rows of black pearls, which are of
almost unheard-of rarity; suns and stars of jewels also constellate
this precious gown, which is so brilliant that the eye can scarcely
bear its splendour, and which is worth many millions of francs.
We ended our visit by ascending the bell-tower, the summit of
which is reached by a succession of ladders, sufficiently steep and
not very reassuring. About half way up, in a kind of store-room,
through which you pass, we saw a row of gigantic marionettes,
coloured and dressed in the fashion of the last century, and used in I
don’t know what kind of a procession similar to that of Tarascon.
The magnificent view which is seen from the tall spire amply
repays you for all the fatigue of the ascent. The whole town is
presented before you with all the sharpness and precision of M.
Pelet’s cork-models, so much admired at the last Exposition de
l’industrie. This comparison is doubtless very prosaic and
unpicturesque; but really I cannot find a better, nor a more accurate
one. The dwarfed and misshapen rocks of blue granite, which
encase the Tagus and encircle the horizon of Toledo on one side,
add still more to the singularity of the landscape, inundated and
dominated by crude, pitiless, blinding light, which no reflections
temper and which is increased by the cloudless and vapourless sky
quivering with white heat like iron in a furnace.

Voyage en Espagne (Paris, new ed. 1865).


THE CHÂTEAU DE CHAMBORD.
JULES LOISELEUR.

CHAMBORD is the Versailles of the feudal monarchy; it was to the


Château de Blois, that central residence of the Valois, what
Versailles was to the Tuileries; it was the country-seat of Royalty.
Tapestries from Arras, Venetian mirrors, curiously sculptured chests,
crystal chandeliers, massive silver furniture, and miracles of all the
arts, amassed in this palace during eight reigns and dispersed in a
single day by the breath of the Revolution, can never be collected
again save under one condition: that there should be a sovereign
sufficiently powerful and sufficiently artistic, sufficiently concerned
about the glory and the memories of the ancient monarchy to make
of Chambord what has been made out of the Louvre and Versailles
—a museum consecrated to all the intimate marvels, to all the
curiosities of the Arts of the Renaissance, at least to all those with
which the sovereigns were surrounded, something like the way the
Hôtel de Cluny exhibits royal life.
It has often been asked why François I., to whom the banks of
the Loire presented many marvellous sites, selected a wild and
forsaken spot in the midst of arid plains for the erection of the
strange building which he planned. This peculiar choice has been
attributed to that prince’s passion for the chase and in memory of his
amours with the beautiful Comtesse de Thoury, châtelaine in that
neighbourhood, before he ascended the throne.
Independently of these motives, which doubtless counted greatly
in his selection, perhaps the very wildness of this place, this distance
from the Loire, which reminded him too much of the cares of Royalty,
was a determining reason. Kings, like private individuals, and even
more than they, experience the need at times of burying themselves,
and therefore make a hidden and far-away nest where they may be
their own masters and live to please themselves. Moreover,
Chambord, with its countless rooms, its secret stairways, and its
subterranean passages, seems to have been built for a love which
seeks shadow and mystery. At the same time that he hid Chambord
in the heart of the uncultivated plains of the Sologne, François I. built
in the midst of the Bois de Boulogne a château, where, from time to
time, he shut himself up with learned men and artists, and to which
the courtiers, who were positively forbidden there, gave the name of
Madrid, in memory of the prison in which their master had suffered.
Chambord, like Madrid, was not a prison: it was a retreat.
That sentiment of peculiar charm which is attached to the
situation of Chambord will be felt by every artist who visits this
strange realization of an Oriental dream. At the end of a long avenue
of poplars breaking through thin underbrush which bears an
illustrious name, like all the roads to this residence, you see, little by
little, peeping and mounting upward from the earth, a fairy building,
which, rising in the midst of arid sand and heath, produces the most
striking and unexpected effect. A genie of the Orient, a poet has
said, must have stolen it from the country of sunshine to hide it in the
country of fog for the amours of a handsome prince. At the summit of
an imposing mass of battlements, of which the first glance discerns
neither the style nor the order, above terraces with ornamental
balustrades, springs up, as if from a fertile and inexhaustible soil, an
incredible vegetation of sculptured stone, worked in a thousand
different ways. It is a forest of campaniles, chimneys, sky-lights,
domes, and towers, in lace-work and open-work, twisted according
to a caprice which excludes neither harmony nor unity, and which
ornaments with the Gothic F the salamanders and also the mosaics
of slate imitating marble,—a singular poverty in the midst of so much
wealth. The beautiful open-worked tower of the large staircase
dominates the entire mass of pinnacles and steeples, and bathes in
the blue sky its colossal fleur-de-lis, the last point of the highest
pinnacle among pinnacles, the highest crown among all crowns....
We must take Chambord for what it is, an ancient Gothic
château dressed out in great measure according to the fashion of the
Renaissance.

THE CHÂTEAU DE CHAMBORD.

In no other place is the transition from one style to another


revealed in a way so impressive and naïve; nowhere else does the
brilliant butterfly of the Renaissance show itself more deeply
imprisoned in the heavy Gothic chrysalis. If Chambord, by its plan
which is essentially French and feudal, by its enclosure flanked with
towers, and by the breadth of its heavy mass, slavishly recalls the
mediæval manoirs, by its lavish profusion of ornamentation it
suggests the creations of the Sixteenth Century as far as the
beginning of the roofs; it is Gothic as far as the platform; and it
belongs to the Renaissance when it comes to the roof itself. It may
be compared to a rude French knight of the Fourteenth Century, who
is wearing on his cuirass some fine Italian embroideries, and on his
head the plumed felt of François I.,—assuredly an incongruous
costume, but not without character....
The château should be entered by one of the four doors which
open in the centre of the donjon. Nothing is more fantastic, and, at
the same time, magnificent than the spectacle which greets the eye.
It seems more like one of those fairy palaces which we see at the
Opera, than a real building. Neglect and nakedness give it an
additional value and double its immensity. On entering this vast
solitude of stone, we are seized with that respectful silence which
involuntarily strikes us under high and solitary vaults. In the centre of
the vast Salle des Gardes, which occupies the entire ground-floor,
and to which the four towers of the donjon give the form of the Greek
cross, rises a monumental stairway which divides this hall into four
equal parts, each being fifty feet long and thirty feet broad. This bold
conception justifies its celebrity: the stairway at Chambord is in itself
a monument. The staircase, completely isolated and open-worked, is
composed of posts which follow the winding. Two flights of stairs,
one above the other, unfold in helices and pass alternately one over
the other without meeting. This will explain how two persons could
ascend at the same time without meeting, yet perceiving each other
at intervals. Even while looking at this, it is difficult to conceive this
arrangement. These two helices, which are placed above each other
and which turn over and over each other without ever uniting, have
exactly the curve of a double corkscrew. I believe that no other
comparison can give a more exact idea of this celebrated work which
has exhausted the admiration and the eulogy of all the connaisseurs.
“What merits the greatest praise,” writes Blondel in his Leçons
d’architecture, “is the ingenious disposition of that staircase of
double flights, crossing each other and both common to the same
newel. One cannot admire too greatly the lightness of its
arrangement, the boldness of its execution, and the delicacy of its
ornaments,—perfection which astonishes and makes it difficult to
conceive how any one could imagine a design so picturesque and
how it could be put into execution.” The author of Cinq Mars taking
up this same idea says: “It is difficult to conceive how the plan was
drawn and how the orders were given to the workmen: it seems a
fugitive thought, a brilliant idea which must have taken material form
suddenly—a realized dream.”...

You might also like