A Practical Guide to Linux® Commands,. Editors, and Shell Programming.
SECOND EDITION. Mark G. Sobell. • •. PRENTICE. HALL. Upper Saddle River,
NJ ...
A Practical Guide to Linux®
Editors,
and
Commands,
Shell Programming
SECOND EDITION
Mark G. Sobell
• • PRENTICE HALL
Upper Saddle River, NJ New York
Capetown
Toronto
• •
Sydney
• •
•
Boston
Montreal
Tokyo
•
•
•
Indianapolis
London
Singapore
•
San Francisco
•
Munich
•
Mexico
•
Paris
City
•
Madrid
Contents xxxi
Preface
Chapter l: Welcome to Linux and Mac OS X The
History of UNIX and GNU-Linux The Heritage of Linux: UNIX Fade to 1983 3 Next
2
2
4
Scene, 1991
The Code Is Free Have Fun!
5
5
What Is So Good About Linux?
6
Why Linux Is Popular with Hardware Companies Linux Is Portable
The C
Linux Can
Linux Can Run
Many Tasks
Linux Provides
a
11
12
Secure Hierarchical
Filesystem
12
The Shell: Command Interpreter and Programming A Large Collection of Useful Utilities 15
Interprocess Communication System Administration 16 Additional Features of Linux GUIs:
Language
13
15
16
Graphical User Interfaces (Inter)Networking Utilities 17 Software Development 17 Exercises
9
10
Kernel
Chapter Summary
Developers
11
Programming Interface Support Many Users 12
a
and
9
Programming Language
Overview of Linux
Linux Has
1
16
18
18
xiii
xiv
Contents
PART I
The Linux and Mac OS X
Operating Systems
21
Chapter 2: Getting Started Conventions Used in This Book
Logging
In from
a
Terminal
Working with the Shell Which Shell
or
23
24
Terminal Emulator
28
Are You
28
Running?
Correcting Mistakes 29 Repeating/Editing Command Lines su/sudo:
Curbing
26
Your Power (root
Where to Find Documentation
31
Privileges)
31
33
The
—help Option 33 33 man: Displays the System Manual for a Searches 35 Keyword apropos:
Displays Information About
info:
HOWTOs:
Utilities
Out How
Finding Things Getting Help with the System 38 More About
Chapter Summary
41
41
43
44
Advanced Exercises
44
Chapter 3: The Utilities Special Characters Basic Utilities Is: Lists
45
46
47
the Names of Files Text File
cat:
Displays
a
rm:
Deletes
File
a
47
48
48
less Is more:
hostname:
Display a Text File One Screen Displays the System Name 49
Working with Files 49 49 cp: Copies a File mv:
Ipr:
38
41
Logging Changing Your Password Exercises
36
40
Logging In
Using Virtual Consoles 40 What to Do If You Cannot Log In Out
Work
Changes Prints a
the Name of
File
a
File
50
51
grep: Searches for a String 52 head: Displays the Beginning of
a
File
52
at a
Time
48
Contents
Displays the End of a File 53 Displays a File in Order 54 uniq: Removes Duplicate Lines from 54 diff: Compares Two Files
xv
tail:
sort:
file: Identifies the Contents of
I
(Pipe):
a
a
File
File
56
Communicates Between Processes
Four More Utilities echo: date:
54
56
57
Displays Text 57 Displays the Time and Date
58
58 script: Records a Shell Session todos/unix2dos: Converts Linux and Mac OS X Files
Compressing and Archiving Files 60 bzip2: Compresses a File 60 bunzip2 and bzcat: Decompress a File 61 gzip: Compresses a File 62 62 tar: Packs and Unpacks Archives Locating Commands
65
which and whereis: Locate siocate/locate: Searches for
a
Utility
a
File
65 66
Obtaining User and System Information 67 who: Lists Users on the System
67
68 finger: Lists Users on the System 69 w: Lists Users on the System
Communicating with Other Users 70 write: Sends a Message 70 Denies or Accepts Messages mesg: Email
71
72
Chapter Summary Exercises
72
75
Advanced Exercises
75
Chapter 4: The Filesystem The Hierarchical
Filesystem
78
Directory Files and Ordinary Files Filenames The
77
78
79
Working Directory 82 Directory 82
Your Home Pathnames
83
Absolute Pathnames Relative Pathnames
83 84
Working with Directories 85 86 mkdir: Creates a Directory cd: Changes to Another Working Directory
87
to
Windows Format
59
xvi
Contents
rmdir: Deletes
Using
mv, cp: Move Moves
mv:
a
88
Directory
a
89
Pathnames
or Copy Files Directory 90
90
Important Standard Directories and Files Is -1:
Displays Permissions
93 94
Access Permissions
Changes Setgid Permissions Access Permissions Directory chmod:
Setuid and
ACLs: Access Control Lists
96 98
99
Enabling ACLs 100 "Working with Access Rules 100 Setting Default Rules for a Directory Links
103
104
Hard Links
106 108
Symbolic Links rm:
91
93
Access Permissions
Removes
a
Link 111
Chapter Summary Exercises
110
112
Advanced Exercises
114
Chapter 5: The Shell The Command Line
117
118
Syntax 118 Processing the Command
Line
120
Executing the Command Line 123 Editing the Command Line 123 Standard Input and Standard Output The Screen The
as a
File
Keyboard and Screen
Redirection
123
124 as
Standard
Input and Standard Output
126
Pipes 131 Running a Command in the Background Filename Generation/Pathname
134
Expansion
136
The ? Special Character 137 The f" Special Character 138 The Builtins
[ ] Special Characters
139
141
Chapter Summary
142
Utilities and Builtins Introduced in This Exercises
143
Advanced Exercises
144
Chapter
142
125
PART II
The Editors
147
Chapter 6: The vim Editor History
149
150
Tutorial: Using vim
Starting
vim
Create and Edit
to
a
File
151
151
Command and Input Modes 153 Entering Text 154 Getting Help 155 Ending the Editing Session 158 The
compatible Parameter
Introduction Online
to vim Features
Help
158
Terminology
159
158 158
Modes of
Operation 159 The Display 160 Correcting Text as You Insert Work Buffer Line
Length and File Size
Windows File Locks
Text After
a
an
Editing Session
Crash
by
Cursor to
Characters a
162
163
Moving the Cursor
the Cursor
Moving the
161
161
Command Mode:
Moving
160
161
Abnormal Termination of Recovering
It
161
164 165
Specific Character
165
Moving the Cursor by Words 166 Moving the Cursor by Lines 166 Moving the Cursor by Sentences and Paragraphs Moving the Cursor Within the Screen 167 Viewing Different Parts of the Work Buffer
Input Mode 168 Inserting Text 168 Appending Text 168 Opening a Line for Text 168 Replacing Text 169 Quoting Special Characters in Input Mode Command Mode: Deleting and Changing Text Undoing Changes 169 Deleting Characters 170 Deleting Text 170
Changing Text
171
Replacing Text Changing Case
172
173
167
169 169
167
xviii
Contents
Searching and Substituting 173 Searching for a Character 173 Searching for a String 174 Substituting One String for Another Miscellaneous Commands
Join
180
180
Status .
176
180
180
(Period)
Deleting Text 181 The General-Purpose Buffer
Copyingj Moving, and
180
Named Buffers 182 Numbered Buffers 182 Reading and Writing Files
183
Reading Files 183 Writing Files 183 Identifying the Current File Setting
Parameters
Setting Setting
Parameters from Within vim
184
Parameters in
185
The .vimrc
a
185
185
Advanced Editing
Techniques
Using Markers
190
Macros and Shortcuts
190
Shell Commands from Within vim
Units of Measure
Character
191
193
193
193
Blank-Delimited Word Line
194
194
Sentence
194
Paragraph 195 Screen (Window)
196
Factor
196
Chapter Summary
196
Repeat Exercises
201
Advanced Exercises
202
Chapter 7: The History
189
189
Editing Other Files Executing
File
Startup
Startup File
Parameters
Word
184
184
emacs
Editor
205
206
Evolution emacs
206
Versus vim
Command-Line
207
emacs
Versus
Graphical
emacs
208
Contents
Tutorial: Getting Started with
208
emacs
Starting emacs 208 Exiting 210 Inserting Text 210 Deleting Characters 210 Moving the Cursor 211 Editing at the Cursor Position 214 Saving and Retrieving the Buffer 214 The
emacs
Basic
GUI
Editing
215
Commands
216
Notation and Use
216 Keys: Key Sequences and Commands 217 MFTA-x: Running a Command Without a Key Binding Numeric Arguments 218 Point and the Cursor 218 Scrolling Through a Buffer Text
218
219
Erasing Searching for Text 219 Using the Menubar from the Keyboard Online
Help
221
223
Advanced
Editing 225 Undoing Changes 225 Point, Mark, and Region Cut
and Paste:
226
Yanking Killed
Inserting Special Characters Global Buffer Commands
Visiting and Saving Files Buffers 235 Windows
Text
228
230 230
232
236
Foreground Shell Commands
238
Shell Commands
239
Background
Major Modes: Language-Sensitive Editing Selecting a Major Mode 240 Human-Language Modes 240 CMode
243
Customizing Indention Comments
Special-Purpose Customizing
246
247
emacs
Modes
247
249
The .emacs Startup File 250 Remapping Keys 251 A Sample .emacs File 253 More Information Access to
emacs
Chapter Summary Exercises
Advanced
254 254 254
262 Exercises
264
239
217
xix
xx
Contents
PART III
The Shells
267
Chapter 8: The Bourne Again Shell
269
270
Background Shell Basics
271
Startup Files
271
Commands That Are
Symbols 275 Redirecting Standard Error 275 Writing a Simple Shell Script 278 Separating and Grouping Commands Job Control
281
285
Manipulating the Directory Stack Parameters and Variables
288
290
User-Created Variables 292 Variable Attributes 295 Keyword Variables 296
Special Characters Processes
304
306
Process
Structure
306
Process Identification
Executing History
a
306
Command
308
308
Variables That Control
History 308 Reexecuting and Editing Commands 310 The Readline Libra ry Aliases
318
324
Single Versus Double Quotation Marks Examples of Aliases 326 Functions
327
Controlling
bash: Features and
Command-Line
Options
Shell Features
330
Options
in
330
330
Processing the Command Line 334 History Expansion 334 Alias Substitution 334 Parsing and Scanning the Command Line Command-Line Expansion
Chapter Summary
343
Aliases
335
334
325
Contents
Exercises
345
Advanced Exercises
347
Chapter 9: The TC Shell Shell
350
Scripts and
Entering
Startup
349
Leaving the
Files
TC Shell
351
352
Features Common
Again and TC Shells
the Bourne
to
Command-Line Expansion (Substitution)
Job Control
358
Filename Substitution
Manipulating
the
358
Directory Stack
Command Substitution
359
Redirecting Standard Error
359
"Working with the Command Line Word
Completion
360
Editing the Command Line Correcting Spelling 364 Variables
360 363
365
Variable Substitution
366
String Variables Arrays of String Variables 366
Numeric Variables
Braces
370
Special Variable Forms Shell Variables
371
Control Structures
378
if
371
378
381
goto
In terrupt
Handling
if...then...else foreach while
381
382
383 385
break and continue switch Builtins
3 86
387
Chapter Summary Exercises
367
368
391
392
Advanced Exercises
394
385
359
354
353
xxi
xxii
Contents
PART IV
Programming Tools
395
Chapter 10: Programming the Bourne Again Shell 398
Control Structures iL.then
398
iL.then...else
402
iL.then...elif
405
for...in
for
411
412
while until
414 418
break and continue 427
select
Here Document File
420
421
case
Descriptors
429
431
Parameters and Variables
Array Variables
434
434
Locality of Variables 436 Special Parameters 438 Positional Parameters
440
Expanding Null and Unset Variables Builtin Commands type:
Displays Information
About
read:
Accepts User Input
447
exec:
Executes
trap: Catches kill: Aborts
a
a
Command
a
Process
or
a
Command
Redirects File
Descriptors
456
A Partial List of Builtins
456 459
460
Arithmetic Evaluation
460
Logical Evaluation (Conditional Expressions) String Pattern Matching 462 Operators 463 Shell
Programs
468
A Recursive Shell The
Script 469 quiz Shell Script 472
Chapter Summary Exercises
447
453
Signal
getopts: Parses Options
Expressions
445
446
478
480
Advanced Exercises
482
461
450
397
Contents
Chapter 11: The Perl Scripting Language Introduction
to
Perl
485
486
More Information
486
487
Help
perldoc
487
Terminology 489 Running a Perl Program Variables
490
491
Syntax
493
Scalar Variables
495
Variables
497
Array
Hash Variables
500
Control Structures
if/unless
501
501
if...else
503
if...elsif...else
504
foreach/for last and
505
506
next
while/until
508
Working with Files Sort
xxiil
510
513
Subroutines
515
517
Regular Expressions Syntax and the CPAN Modules
=~
Operator
518
523
525
Examples
Chapter Summary Exercises
529
529
Advanced Exercises
530
Chapter 12: The AWK Pattern Processing Language Syntax
532
Arguments Options Notes
532
533 534
Language Basics Patterns Actions
534
534 535
Comments Variables Functions Arithmetic
535
535 536
Operators
537
531
xxlv
Contents
538
Associative Arrays 538
printf
Control Structures
Examples
539
541
gawk Programming
Advanced
getline: Controlling Input Coprocess: Two-Way I/O Getting Input from
558 558
560
Network
a
562
563
Chapter Summary 563
Exercises
Advanced Exercises
564
Chapter 13: Thesed Editor Syntax
565
566 566
Arguments
566
Options
Editor Basics
567
Addresses
567
Instructions
568
Control Structures The Hold
Examples
569 570
Space
570
Chapter Summary Exercises
581
581
Chapter 14: The rsync Secure Copy Utility Syntax
584
Arguments
Options Notes
584
584
586
More Information
586
Examples Using a Trailing Slash (/) on source-file 587 Removing Files 588 Copying Files to and from a Remote System Mirroring a Directory 590 Making Backups 591 587
Chapter Summary Exercises
594
594
590
583
Contents
PARTV Standard
Command Reference
Multiplicative Suffixes
Common
Options Utility
The sample
597
602
603 604
sample Brief description of what the utility does 607 aspell Checks a file for spelling errors at
Executes commands
at a
specified time
bzip2 Compresses or decompresses files 617 cal Displays a calendar cat
cd
chgrp chmod chown
Joins and displays files another
605 611
615
618
working directory
620
Changes
to
Changes
the group associated with a file 622 the access mode (permissions) of a file
Changes Changes the
owner
of
associated with
a
626
file and/or the group the file is
631
Compares two files 634 Compares sorted files 636 configure Configures source code automatically 640 cp Copies files cmp
comm
cpio Creates
archive,
an
directory hierarchy crontab cut date dd
df diff
or
archive,
an
input
lines
or sets the system time and date 658 Converts and copies a file
Displays
copies
a
649
fields from
652
655
Displays
Displays disk
or
644
Maintains crontab files
Selects characters
files from
restores
638
661
space usage
the differences between
two text
files
663
Checks, modifies, and repairs local volumes C3© 668 671 ditto Copies files and creates and unpacks archives BD 673 kernel dmesg Displays messages dscl Displays and manages Directory Service information GED
diskutil
du
Displays information and/or file
echo expr file find
Displays
a
Evaluates
Displays
on
680
expression
682
the classification of
Finds files based
on
criteria
a
file
Displays
information about users
fmt
Formats
text
Checks and
very
simply
686
688
finger fsck
by directory hierarchy
677
message
an
disk usage
695
697
repairs a filesystem
699
ftp Transfers files over a network 704 gawk Searches for and processes patterns in gcc
Compiles
C and C++ programs
712
a
file
711
674
xxv
xxvi
Contents
GetFilelnfo
Displays file
attributes 6E©
grep
Searches for
a
717
pattern in files
719 files
gzip Compresses or decompresses head Displays the beginning of a file kill
Terminates
killall
Terminates
launchctl less In
make man
Displays text files,
740
printers
742
Displays information
about
link
a
Keeps
a set
Displays
mkfs
Creates
a
nohup od open otool
time
one or more
plutil pr ps rep renice
rlogin rm
files
745
753
current
759
Renames
a
Dumps
file
or moves a
the
Changes Runs
763
priority of
command that the
keeps
of
contents
file
a
773
running after
you
Manipulates property
list files @©
Paginates files for printing
Displays process Copies
the
Changes Logs in on
a
files
Removes
file
a
priority
of
Removes directories
rsh
Executes commands
an
archive,
or
copies
system
800
792
a
to or
a
from
a remote
802
process 803
link)
804
806 on
a remote
system
807
rsync Copies files and directory hierarchies securely scp Securely copies one or more files to or from a SetFile
Edits
a
file
split Divides
a
ssh stat
network
remote
system
813
process that sleeps for 817 sort Sorts and/or merges files a
over a
812
noninteractively
Sets file attributes G3J Creates
sleep
a
794
remote system
(deletes
782
796
status
one or more
775
out
784
archive, restores files from directory hierarchy 786 an
rmdir
sed
log
776
Opens files, directories, and URLs