Introduction
Why Use The Command Line?
What This Book Is About
Who Should Read This Book
What's In This Book
How To Read This Book
Prerequisites
Acknowledgments
Your Feedback Is Needed!
What's New In The Second Internet Edition
Further Reading
Colophon
Part 1 – Learning The Shell
1 – What Is The Shell?
Terminal Emulators
Your First Keystrokes
Command History
Cursor Movement
Try Some Simple Commands
Ending A Terminal Session
Summing Up
Further Reading
2 – Navigation
Understanding The File System Tree
The Current Working Directory
Listing The Contents Of A Directory
Changing The Current Working Directory
Absolute Pathnames
Relative Pathnames
Some Helpful Shortcuts
Summing Up
3 – Exploring The System
More Fun With ls
Options And Arguments
A Longer Look At Long Format
Determining A File's Type With file
Viewing File Contents With less
A Guided Tour
Symbolic Links
Hard Links
Summing Up
Further Reading
4 – Manipulating Files And Directories
Wildcards
mkdir – Create Directories
cp – Copy Files And Directories
Useful Options And Examples
mv – Move And Rename Files
Useful Options And Examples
rm – Remove Files And Directories
Useful Options And Examples
ln – Create Links
Hard Links
Symbolic Links
Let's Build A Playground
Creating Directories
Copying Files
Moving And Renaming Files
Creating Hard Links
Creating Symbolic Links
Removing Files And Directories
Summing Up
Further Reading
5 – Working With Commands
What Exactly Are Commands?
Identifying Commands
type – Display A Command's Type
which – Display An Executable's Location
Getting A Command's Documentation
help – Get Help For Shell Builtins
--help – Display Usage Information
man – Display A Program's Manual Page
apropos – Display Appropriate Commands
whatis – Display A Very Brief Description Of A Command
info – Display A Program's Info Entry
README And Other Program Documentation Files
Creating Your Own Commands With alias
Summing Up
Further Reading
6 – Redirection
Standard Input, Output, And Error
Redirecting Standard Output
Redirecting Standard Error
Redirecting Standard Output And Standard Error To One File
Disposing Of Unwanted Output
Redirecting Standard Input
cat – Concatenate Files
Pipelines
Filters
uniq - Report Or Omit Repeated Lines
wc – Print Line, Word, And Byte Counts
grep – Print Lines Matching A Pattern
head / tail – Print First / Last Part Of Files
tee – Read From Stdin And Output To Stdout And Files
Summing Up
7 – Seeing The World As The Shell Sees It
Expansion
Pathname Expansion
Tilde Expansion
Arithmetic Expansion
Brace Expansion
Parameter Expansion
Command Substitution
Quoting
Double Quotes
Single Quotes
Escaping Characters
Summing Up
Further Reading
8 – Advanced Keyboard Tricks
Command Line Editing
Cursor Movement
Modifying Text
Cutting And Pasting (Killing And Yanking) Text
Completion
Using History
Searching History
History Expansion
Summing Up
Further Reading
9 – Permissions
Owners, Group Members, And Everybody Else
Reading, Writing, And Executing
chmod – Change File Mode
Setting File Mode With The GUI
umask – Set Default Permissions
Changing Identities
su – Run A Shell With Substitute User And Group IDs
sudo – Execute A Command As Another User
chown – Change File Owner And Group
chgrp – Change Group Ownership
Exercising Our Privileges
Changing Your Password
Summing Up
Further Reading
10 – Processes
How A Process Works
Viewing Processes
Viewing Processes Dynamically With top
Controlling Processes
Interrupting A Process
Putting A Process In The Background
Returning A Process To The Foreground
Stopping (Pausing) A Process
Signals
Sending Signals To Processes With kill
Sending Signals To Multiple Processes With killall
More Process Related Commands
Summing Up
Part 2 – Configuration And The Environment
11 – The Environment
What Is Stored In The Environment?
Examining The Environment
Some Interesting Variables
How Is The Environment Established?
What's In A Startup File?
Modifying The Environment
Which Files Should We Modify?
Text Editors
Using A Text Editor
Activating Our Changes
Summing Up
Further Reading
12 – A Gentle Introduction To vi
Why We Should Learn vi
A Little Background
Starting And Stopping vi
Editing Modes
Entering Insert Mode
Saving Our Work
Moving The Cursor Around
Basic Editing
Appending Text
Opening A Line
Deleting Text
Cutting, Copying, And Pasting Text
Joining Lines
Search-And-Replace
Searching Within A Line
Searching The Entire File
Global Search-And-Replace
Editing Multiple Files
Switching Between Files
Opening Additional Files For Editing
Copying Content From One File Into Another
Inserting An Entire File Into Another
Saving Our Work
Summing Up
Further Reading
13 – Customizing The Prompt
Anatomy Of A Prompt
Trying Some Alternative Prompt Designs
Adding Color
Moving The Cursor
Saving The Prompt
Summing Up
Further Reading
Part 3 – Common Tasks And Essential Tools
14 – Package Management
Packaging Systems
How A Package System Works
Package Files
Repositories
Dependencies
High And Low-level Package Tools
Common Package Management Tasks
Finding A Package In A Repository
Installing A Package From A Repository
Installing A Package From A Package File
Removing A Package
Updating Packages From A Repository
Upgrading A Package From A Package File
Listing Installed Packages
Determining If A Package Is Installed
Displaying Info About An Installed Package
Finding Which Package Installed A File
Summing Up
Further Reading
15 – Storage Media
Mounting And Unmounting Storage Devices
Viewing A List Of Mounted File Systems
Determining Device Names
Creating New File Systems
Manipulating Partitions With fdisk
Creating A New File System With mkfs
Testing And Repairing File Systems
Formatting Floppy Disks
Moving Data Directly To/From Devices
Creating CD-ROM Images
Creating An Image Copy Of A CD-ROM
Creating An Image From A Collection Of Files
Writing CD-ROM Images
Mounting An ISO Image Directly
Blanking A Re-Writable CD-ROM
Writing An Image
Summing Up
Further Reading
Extra Credit
16 – Networking
Examining And Monitoring A Network
ping
traceroute
netstat
Transporting Files Over A Network
ftp
lftp – A Better ftp
wget
Secure Communication With Remote Hosts
ssh
scp And sftp
Summing Up
Further Reading
17 – Searching For Files
locate – Find Files The Easy Way
find – Find Files The Hard Way
Tests
Operators
Predefined Actions
User-Defined Actions
Improving Efficiency
xargs
A Return To The Playground
Options
Summing Up
Further Reading
18 – Archiving And Backup
Compressing Files
gzip
bzip2
Archiving Files
tar
zip
Synchronizing Files And Directories
Using rsync Over A Network
Summing Up
Further Reading
19 – Regular Expressions
What Are Regular Expressions?
grep
Metacharacters And Literals
The Any Character
Anchors
Bracket Expressions And Character Classes
Negation
Traditional Character Ranges
POSIX Character Classes
POSIX Basic Vs. Extended Regular Expressions
Alternation
Quantifiers
? - Match An Element Zero Or One Time
* - Match An Element Zero Or More Times
+ - Match An Element One Or More Times
{ } - Match An Element A Specific Number Of Times
Putting Regular Expressions To Work
Validating A Phone List With grep
Finding Ugly Filenames With find
Searching For Files With locate
Searching For Text With less And vim
Summing Up
Further Reading
20 – Text Processing
Applications Of Text
Documents
Web Pages
Email
Printer Output
Program Source Code
Revisiting Some Old Friends
cat
sort
uniq
Slicing And Dicing
cut
paste
join
Comparing Text
comm
diff
patch
Editing On The Fly
tr
sed
aspell
Summing Up
Further Reading
Extra Credit
21 – Formatting Output
Simple Formatting Tools
nl – Number Lines
fold – Wrap Each Line To A Specified Length
fmt – A Simple Text Formatter
pr – Format Text For Printing
printf – Format And Print Data
Document Formatting Systems
groff
Summing Up
Further Reading
22 – Printing
A Brief History Of Printing
Printing In The Dim Times
Character-based Printers
Graphical Printers
Printing With Linux
Preparing Files For Printing
pr – Convert Text Files For Printing
Sending A Print Job To A Printer
lpr – Print Files (Berkeley Style)
lp – Print Files (System V Style)
Another Option: a2ps
Monitoring And Controlling Print Jobs
lpstat – Display Print System Status
lpq – Display Printer Queue Status
lprm / cancel – Cancel Print Jobs
Summing Up
Further Reading
23 – Compiling Programs
What Is Compiling?
Are All Programs Compiled?
Compiling A C Program
Obtaining The Source Code
Examining The Source Tree
Building The Program
Installing The Program
Summing Up
Further Reading
Part 4 – Writing Shell Scripts
24 – Writing Your First Script
What Are Shell Scripts?
How To Write A Shell Script
Script File Format
Executable Permissions
Script File Location
Good Locations For Scripts
More Formatting Tricks
Long Option Names
Indentation And line-continuation
Summing Up
Further Reading
25 – Starting A Project
First Stage: Minimal Document
Second Stage: Adding A Little Data
Variables And Constants
Assigning Values To Variables And Constants
Here Documents
Summing Up
Further Reading
26 – Top-Down Design
Shell Functions
Local Variables
Keep Scripts Running
Summing Up
Further Reading
27 – Flow Control: Branching With if
if
Exit Status
test
File Expressions
String Expressions
Integer Expressions
A More Modern Version Of test
(( )) - Designed For Integers
Combining Expressions
Control Operators: Another Way To Branch
Summing Up
Further Reading
28 – Reading Keyboard Input
read – Read Values From Standard Input
Options
IFS
Validating Input
Menus
Summing Up
Extra Credit
Further Reading
29 – Flow Control: Looping With while / until
Looping
while
Breaking Out Of A Loop
until
Reading Files With Loops
Summing Up
Further Reading
30 – Troubleshooting
Syntactic Errors
Missing Quotes
Missing Or Unexpected Tokens
Unanticipated Expansions
Logical Errors
Defensive Programming
Verifying Input
Testing
Test Cases
Debugging
Finding The Problem Area
Tracing
Examining Values During Execution
Summing Up
Further Reading
31 – Flow Control: Branching With case
case
Patterns
Performing Multiple Actions
Summing Up
Further Reading
32 – Positional Parameters
Accessing The Command Line
Determining The Number of Arguments
shift – Getting Access To Many Arguments
Simple Applications
Using Positional Parameters With Shell Functions
Handling Positional Parameters En Masse
A More Complete Application
Summing Up
Further Reading
33 – Flow Control: Looping With for
for: Traditional Shell Form
for: C Language Form
Summing Up
Further Reading
34 – Strings And Numbers
Parameter Expansion
Basic Parameters
Expansions To Manage Empty Variables
Expansions That Return Variable Names
String Operations
Case Conversion
Arithmetic Evaluation And Expansion
Number Bases
Unary Operators
Simple Arithmetic
Assignment
Bit Operations
Logic
bc – An Arbitrary Precision Calculator Language
Using bc
An Example Script
Summing Up
Extra Credit
Further Reading
35 – Arrays
What Are Arrays?
Creating An Array
Assigning Values To An Array
Accessing Array Elements
Array Operations
Outputting The Entire Contents Of An Array
Determining The Number Of Array Elements
Finding The Subscripts Used By An Array
Adding Elements To The End Of An Array
Sorting An Array
Deleting An Array
Associative Arrays
Summing Up
Further Reading
36 – Exotica
Group Commands And Subshells
Process Substitution
Traps
Asynchronous Execution
wait
Named Pipes
Setting Up A Named Pipe
Using Named Pipes
Summing Up
Further Reading
Index