A Practical Guide to Linux@ Commands,. Editors, and Shell Programming.
THIRD EDITION. Mark G. Sobell. PRENTICE. HALL. Upper Saddle River, NJ •.
A Practical Guide to Linux@ Commands, Editors, and Shell Programming THIRD EDITION
Mark G. Sobell
PRENTICE
HALL
Upper
Saddle River, NJ
New York
•
Capetown
•
Toronto
Sydney
•
•
•
Boston
Montreal
Tokyo
•
•
Indianapolis
•
San Francisco
London
•
Munich
Singapore
•
Mexico City
•
•
Paris
•
Madrid
Brief Contents Contents Preface
1
xvii
xxxvii
Welcome
PART I
Linux
to
Getting Started
3
The Utilities
4
The Filesystem
5
The Shell
21
23
81
125
The Editors
The vim Editor
7
The
emacs
157
159
Editor
213
The Shells
8
The Bourne Again Shell
9
The TC Shell
PART IV
1
49
6
PART III
Mac OS X
The Linux and Mac OS X Operating Systems
2
PART II
and
(tcsh)
273
(bash)
275
369
Programming Tools
10
Programming the Bourne Again Shell
11
The Perl Scripting Language
415 (bash)
417
517
xv
xvi
Brief Contents
12
The Python Programming Language
563
13
The
14
The AWK Pattern Processing Language
15
The sed Editor
MySQL Database Management System
595 619
653
Secure Network Utilities
PART V 16
The rsync Secure Copy Utility
17
The OpenSSH Secure Communication Utilities
PART VI
PART VII A
673
Command Reference
Command Reference
713
Appendixes
Regular Expressions
B
Help
C
Keeping
D
Mac OS X Notes
1009
1011
1021
Glossary
the
1053
File Tree Index
UtilityIndex Main Index
System Up-to-Date
1105 1107
1111
1041
671
1027
711
687
Contents Preface
xxxvii
Chapter 1: Welcome to Linux and Mac OS X The
History of UNIX and GNU-Linux Heritage of Linux: UNIX 3
1
3
The
Fade
to
1983
3
Next Scene, 1991
4
The Code Is Free Have Fun!
5
6
What Is So Good About Linux?
Why
Linux Is
6
Popular with Hardware Companies and
Linux Is Portable
The C Programming Language Overview of Linux Linux Has
10
11
Programming Interface 12 Support Many Users 12 Can Run Many Tasks 12 Provides a Secure Hierarchical Filesystem a
Developers
10
Kernel
Linux Can Linux Linux
12
The Shell: Command Interpreter and Programming Language A Large Collection of Useful Utilities 15
Interprocess Communication
System Administration Additional Features of Linux GUIs:
Graphical
16
User Interfaces
Utilities
(Inter)Networking Software Development Chapter Summary Exercises
18
16
16
18
18
17
16
14
9
xviii
Contents
PART I
The Linux
Mac OSX
and
Operating Systems
21
Chapter 2: Getting Started Conventions Used in This Book In from
Logging
a
Terminal
24
(Emulator)
from the Command Line
Working
Which
29
29
Correcting Repeating/Editing Command Where
26
28
Shell Are You Running? Mistakes
su/sudo:
23
Lines
31
Curbing Your Power (root Privileges) 33
Find Documentation
to
man:
Displays
the
System
Manual
33
apropos: Searches for a Keyword 35 info: Displays Information About Utilities The
32
36
38
—help Option help Command Getting Help 39 The bash
39
More About
Logging In and Passwords 42 Log In 42 Logging In Remotely: Terminal Emulators, ssh, and Dial-Up Connections Using Virtual Consoles 43 Logging Out 43 Changing Your Password 44
What
to
Do If You Cannot
Chapter Summary Exercises
46
46
Advanced Exercises
47
Chapter 3: The Utilities Special Characters Basic Utilities Is: Lists
50
51
the Names of Files Text File
cat:
Displays
a
rm:
Deletes
File
less Is
a
52
Display a Text File One Screen Displays the System Name 53
Working with Files cp: Copies a File mv: Changes the Prints
grep:
52
52
more:
hostname:
Ipr:
49
a
53 53 Name of a File
File
Searches for
55 a
String
56
54
at a
Time
53
43
Contents
Displays the Beginning of a File 56 Displays the End of a File 57 58 sort: Displays a File in Order Removes Lines from a File Duplicate uniq: 58 diff: Compares Two Files
xix
head: tail:
file: Identifies the Contents of
I
(Pipeline): Communicates
Four More Utilities
date:
script: Records
File
60
Between Processes
a
Date
Shell Session
unix2dos: Converts Linux
Files
62
62 to
Windows and Macintosh OS X Format
Compressing and Archiving Files 63 bzip2: Compresses a File 64 bzcat and bunzip2: Decompress a File gzip: Compresses a File 65 66 tar: Packs and Unpacks Archives Locating Utilities
locate: Searches for
a
a
File
68
Utility 70
System Information Displaying 70 who: Lists Users on the System Users the 71 on Lists System finger: User and
uptime: Displays System Load and Lists Users
the
System Displays Memory Usage
free:
65
68
which and whereis: Locate
w.
on
70
Duration Information
73 Information 74
Communicating with Other Users 74 write: Sends a Message mesg: Denies or Accepts Messages Email
Chapter Summary
76
79
Advanced Exercises
80
Chapter 4: The Filesystem The Hierarchical
82
Filesystem
Directory Files and Ordinary Files Filenames The
75
76
Exercises
84
Working Directory Directory
Your Home
Pathnames
60
61
Displays Text 61 Displays the Time and
echo:
a
58
86 87
87
Absolute Pathnames Relative Pathnames
88 89
83
81
74
72
63
xx
Contents
Working with
Directories
mkdir: Creates cd:
Changes
a
90
Directory
Another Working
to
rmdir: Deletes
90
Directory
a
Using Pathnames 94 mv, cp: Move or Copy Files 95 mv: Moves a Directory
94
Important Standard Directories and Files Access Permissions
92
Directory
93
95
98
Is -1:
Displays Permissions 98 chmod: Changes Access Permissions Setuid and Setgid Permissions 101 Directory
Access Permissions
ACLs: Access Control Lists
99
103
104
Enabling ACLs 105 Working with Access Rules 105 Setting Default Rules for a Directory Links
Hard Links
Symbolic rm:
110
Links
Removes
a
112
Link
115
Dereferencing Symbolic Links Chapter Summary Exercises
119
122
Chapter 5: The Shell The Command Line
Simple
125
126
Command
126
126
Syntax
Simple Commands 130 Processing the Command Line Executing a Command 132
Editing Standard
the Command Line and Standard
Input
The Screen The
as a
File
Pipelines Lists
133
Output
133
as
Standard
Input and Standard Output
135
141
145 a
Command in the
Background
Filename Generation/Pathname The ?
Character
Expansion
148
Special Special Character 149 The [ ] Special Characters 151 The
130
134
Keyboard and Screen
Redirection
Running
115
120
Advanced Exercises
A
108
109
J>"
146 148
135
Builtins
153
Chapter Summary
153
Utilities and Builtins Introduced in This Exercises
PART II
156
The Editors
157
Chapter 6: The vim Editor
159
160
Tutorial:
Using vim to Create and Edit Starting vim 161 Command and Input Modes Entering Text 164
Getting Help
to
vim Features
Online
168
161
Work Buffer
171
168 168 168
Help Terminology 169 Modes of Operation 169 The Display 170 Correcting Text as You Insert Length and File Size
Windows
File
165
Ending Editing Session The compatible Parameter Introduction
a
163
the
Line
154
155
Advanced Exercises
History
Chapter
It
170
171
171
File Locks
171
Abnormal Termination of
Recovering
Text After
a
an
Editing Session
Crash
172
173
Command Mode: Moving the Cursor Moving the Cursor by Characters
174 175
Moving the Cursor to a Specific Character 175 Moving the Cursor by Words 176 Moving the Cursor by Lines 176 Moving the Cursor by Sentences and Paragraphs Moving the Cursor Within the Screen 177 Viewing Different Parts of the Work Buffer 177 Input Mode 178 Inserting Text
178
Text
178 Appending for Text Line Opening a
Replacing
Text
178
179
Quoting Special Characters
in
Input Mode
179
177
xxii
Contents
Command Mode:
Deleting and Changing Undoing Changes 179 Deleting Characters 180 Deleting Text 180
Changing Text Replacing Text Changing Case
Text
179
181 182 183
and
Searching Substituting 183 Searching for a Character 183 Searching for a String 184 Substituting One String for Another Miscellaneous Commands
190
190
Join Status .
190 190
(Period)
Copying, Moving, and Deleting Text The
186
General-Purpose
Buffer
190
191
Named Buffers 192 Numbered Buffers 192 and Writing Files Reading Files 193
Reading
193
Writing Files
193
Identifying the
Current File
Parameters
Setting Setting
Setting
194
194
Parameters from Within vim Parameters in
a
Startup
The .vimrc Startup File Parameters 195 Advanced
194 195
195
Editing Techniques Using Markers 199 Editing Other Files 200 Macros and Shortcuts
File
199
200
Executing Shell Commands from Within Units of Measure Character Word
203
203
203
Blank-Delimited Word Line
204
Sentence
Paragraph Screen
204 205
(Window) Factor
Repeat Chapter Summary Exercises
206
206 206
211
Advanced Exercises
212
204
vim
201
Contents
Chapter 7: The emacs Editor
213
214
History
Evolution emacs
Tutorial:
214
Versus
215
vim
Started with
Getting
Starting Exiting Inserting
218
Deleting
Characters
Text
218 218
Moving the Cursor 219 Editing at the Cursor Position
Saving
and
216
emacs
216
emacs
Retrieving
221
the Buffer
222
Basic
Editing Commands 223 Keys: Notation and Use 223 Key Sequences and Commands 224 META-x: Running a Command Without a Key Binding Numeric Arguments 225 Point and the Cursor
Scrolling Through Erasing
Text
a
225
Buffer
225
226
Searching for
Text
226
Using the Menubar from the Online
Help
228
Keyboard
229
Advanced
Editing 232 Undoing Changes 232 Point, Mark, and Region 233 Cut and Paste: Yanking Killed Text Inserting Special Characters 236 Global Buffer Commands
Visiting
and
Saving
Files
235
237 239
242
Buffers
243
Windows
Foreground Shell Commands Background Shell Commands
245
246
Major Modes: Language-Sensitive Editing Selecting a Major Mode 247 Human-Language Modes 247 C Mode
250
Customizing Indention Comments
Special-Purpose Modes Customizing The
emacs
.emacs
Sample
.emacs
254
256
Startup File
Remapping Keys A
253
253
257
258 File
259
246
224
xxiii
xxiv
Contents
More Information
Access
260 261
Chapter Summary Exercises
261
to emacs
269
Advanced Exercises
PART III
270
The Shells
273
Chapter 8: The Bourne Again Shell Files
278
Login Shells Interactive
278
Nonlogin Shells
Noninteractive Shells
(Dot)
source: Runs
or
Commands That Are
Redirecting
279 a
chmod: Makes
a
Startup File
Symbols
Standard Error
Writing and Executing
a
279
279
Setting Up Startup Files .
in the Current Shell
281
282
Simple Shell Script
File Executable
284
285
Specifies a Shell 287 Begins a Comment 288 Executing a Shell Script 288 #!
#
Control Operators: Separate and Group Commands 290 ; and NEWLINE Separate Commands
289
I and & Separate Commands and Do Something Else && and II Boolean Control Operators 291
( ) Groups Commands \ Continues
Job
Control
a
293
294
Suspending bg: Sends
292
Command
jobs: Lists Jobs fg: Brings a Job a
295 to
the
Foreground
295
296
Job Job to the Background a
296
Manipulating the Directory Stack 297 297 dirs: Displays the Stack pushd: Pushes a Directory on the Stack popd: Pops
a
Directory Off the Stack
Parameters and Variables
300
User-Created Variables Variable Attributes
305
Keyword Variables
307
Special Locale Time
Characters 316 320
275
276
Background Startup
(bash)
315
302
298 299
290
280
Contents
Processes
323
Process Structure
323
Process Identification
Executing
a
324
Command
325
326
History
Variables That Control
History 326 Reexecuting and Editing Commands The Readline Library 335 Aliases
328
342
Single Versus Double Quotation Marks in Aliases Examples of Aliases 344 Functions
346
Controlling
bash: Features and
bash Command-Line Shell Features
Options Options 349
Processing History Expansion Alias Substitution
Parsing
349
350
the Command Line
and
343
354
354 354
Scanning the Command Line Expansion 354
354
Command-Line
Chapter Summary Exercises
364
366
Advanced Exercises
368
Chapter 9: The TC Shell Shell Scripts
(tcsh)
Entering and Leaving the Startup Files 372
TC Shell
Features Common to the Bourne
Command-Line
Job Control
371
Again and TC Shells
Expansion (Substitution)
378
Filename Substitution
378
Manipulating the Directory Stack Command Substitution
379
Error
379
Redirecting Standard
Working
with the Command Line
Word
386
String Variables 386 Arrays of String Variables Numeric Variables
Special
383
385
Variable Substitution
Braces
380
380
Completion Editing the Command Line Correcting Spelling 384 Variables
369
370
387
388
390 Variable Forms
tcsh Variables
391
391
379
374
373
xxv
xxvi
Contents
Control Structures if
398
398 401
goto
Interrupt Handling foreach
401
402
if...then...else 403 405
while
405
break and continue 406
switch Builtins
407 411
Chapter Summary 412
Exercises
Advanced Exercises
414
Programming Tools
PART IV
415
Chapter 10: Programming the Bourne
Again Shell (bash) 418
Control Structures if...then
419
if...then...else
423
if...then...elif for...in for
425
431
433
while
435
until
439
break and continue
441
442
case
select
448
Here Document
File
417
Descriptors
450
452
File Descriptor 453 Duplicating a File Descriptor 453
Opening File
a
Descriptor Examples
Determining Whether Parameters
Special
453
File Descriptor Is Associated with the Terminal
458
Positional Parameters
Variables
a
Parameters
458
463
467
Shell Variables
467
Environment, Environment Variables, and Inheritance
Expanding Null and Unset Variables Array Variables 474 Variables in Functions
475
472
468
456
Contents
Builtin Commands
476 Information About
type: Displays read: Accepts User Input exec:
Executes
Command
a
trap: Catches a Signal kill: Aborts a Process
a
or
Redirects File
A Partial List of Builtins
a
Command Line
Evaluation
491
(Conditional Expressions)
Matching
Pattern
Arithmetic Operators
Implicit Command-Line Shell Programs
495
Continuation
Script
Shell
Script
Chapter Summary
510
quiz
493
494
499
500
A Recursive Shell
501
504
512
Advanced Exercises
514
Chapter 11: The Perl Scripting Language Introduction
Perl
to
518 519
More Information 519
Help
519
perldoc
Terminology Running Syntax Variables
521
Perl Program
a
526
528
Array Variables Hash Variables
529 532
Control Structures if/unless if...else
536
foreach/for last and
533
534 537
if...elsif...else
537 539
next
while/until
Sort
522
524
Scalar Variables
Working
487
492
Arithmetic Evaluation
Exercises
480
492
Expressions
The
Descriptors
486
Evaluates, and Executes getopts: Parses Options 488
String
477
483
eval: Scans,
Logical
Command
477
540
with Files
542
546
Subroutines
547
Regular Expressions Syntax and the
=~
550
Operator
550
517
xxvii
xxx
Contents
Language
Basics
Patterns
622
622
Actions
623
Comments
623
Variables
623
Functions
624
Arithmetic Operators Associative
625
Arrays
626
Control Structures
627
printf
626
629
Examples
gawk Programming getline: Controlling Input
Advanced
646 646
Coprocess: Two-Way I/O 648 Getting Input from a Network 650 651
Chapter Summary Exercises
651
Advanced Exercises
652
Chapter 15: The sed Editor Syntax
653
654 654
Arguments
654
Options
Editor Basics
655
Addresses
655
Instructions
656
Control Structures The Hold
657 658
Space
658
Examples
Chapter Summary Exercises
669
669
PART V
Secure Network Utilities
Chapter 16: The rsync Secure Copy Utility Syntax
674
Arguments Options Notes
674
675 676
More Information
677
671 673
Contents
Examples 677 Using a Trailing Slash (/) on source-file 678 Removing Files 678 Copying Files to and from a Remote System Mirroring a Directory 681 Making Backups 681 Restoring
a
File
Chapter Summary Exercises
680
684 684
685
Chapter 17: The OpenSSH Secure Communication Utilities
687
Introduction How Files
to
688
OpenSSH
OpenSSH Works
689
689
More Information
691
Running the ssh, scp, and sftp OpenSSH Clients Tutorial: Using ssh and scp to Connect to an Clients
691
OpenSSH Server
692
Configuring OpenSSH ssh: Logs In or Executes Commands on a Remote System 698 scp: Copies Files to and from a Remote System sftp:
A Secure FTP Client
~/.ssh/config Authorized
and
Keys:
ssh-agent: Holds
Forwarding Port
Automatic
706
Forwarding
707
Exercises
Login Keys
Your Private
XI1
Chapter Summary
700
/etc/ssh/ssh_config Configuration
Tunneling/Port Forwarding
Files
702 705
706
708
709
Advanced Exercises
PART VI Utilities That
Command Reference
Network Utilities Utilities That
709
Display
and
Manipulate
Programming Tools
Miscellaneous Utilities
716
Standard
Multiplicative
Suffixes
Common
Options
The sample
Utility
Files
714
Display and Alter Status
Utilities That Are
718 718
694
717
715 716
713
711
700
691
xxxi
xxxii
Contents
description of what the utility does 719 721 a file for spelling errors at Executes commands at a specified time 725 standard utilities 729 busybox Implements many bzip2 Compresses or decompresses files 732 sample aspell
Brief
Checks
cal
Displays
cat
Joins
calendar
a
and
734 735
files
displays
737 Changes with file the 739 a chgrp Changes group associated chmod Changes the access mode (permissions) of a file 741 chown Changes the owner of a file and/or the group the file is cd
another working directory
to
associated with cmp comm
Compares
two
cp
748
Compares sorted files
configure Configures cpio
746 files
Copies files Creates
an
cut
archive,
restores
date dd df diff diskutil
characters
Displays
or sets
Converts and
files from
an
archive,
du
or
input lines
the system time and date
copies
a
file
766 769
772
file
Displays
a
791 794
message
Converts TABs to SPACES and SPACES to TABs
file find
finger fmt fsck
ftp gawk
785
Displays and manages Directory Service information GSB 788 Displays information on disk usage by directory hierarchy
expand/unexpand expr
a
Displays disk space usage 775 Displays the differences between two text files 777 Checks, modifies, and repairs local volumes CED 782
and/or echo
copies
763
fields from
ditto Copies files and creates and unpacks archives 63D dmesg Displays kernel messages 787 dscl
or
758
Maintains crontab files Selects
752
automatically
754
directory hierarchy crontab
750
code
source
Evaluates
Displays
an
expression
the classification of
Finds files based
Displays
on
796
798 a
criteria
information about
file
802
804 users
810
Formats text very
simply 812 Checks and repairs a filesystem Transfers files
over a
network
814 819
Searches for and processes patterns in
a
file
825
Contents
gcc GetFilelnfo grep
Compiles C and C++ programs Displays file attributes ©15 831 Searches for
gzip Compresses head
Displays
join Joins
pattern in files
a
the
lines from
files based
two
Terminates
a
process
killall
Terminates
a
process
less In
make man mc
Displays
text
Makes
link
a
files,
856
printers
858
to a
to
Keeps
Displays
Manages files a
Creates
a
od
open otool
paste pax
Renames
Changes
in
a
textual environment
ps renice rm
files
or
on
moves a
the
a
(aka Midnight
priority
file of a
890 command
a
file
command that
887
device
a
892
894
keeps running after
you
Joins corresponding Creates
an
archive,
lines from files
restores
a
file (deletes
a
link)
an
archive,
@©
copies
917 925
926
Copies files and directory hierarchies securely
928 over a
929
Securely copies system
or
913
rsync
network
one or more
files
to or
from
930
Manages several textual windows
896
903
907
Paginates files for printing 915 Formats string and numeric data Displays process status 921 Changes the priority of a process Removes
out
905
files from
Removes directories
screen
log
Dumps the content of a file 897 Opens files, directories, and URLs 0® 901 Displays object, library, and executable files (33$
rmdir
scp
861
886
directory filesystem
directory hierarchy pr
852
879
Numbers lines from Runs
850 time
one or more
plutil Manipulates property list files printf
848
of programs current 869 documentation for utilities 875
Creates
nohup
843
846
screen at a
one
field
a set
mkfs
nl
on a common
name
Displays information about
mkdir
mv
by by
file
Commander)
nice
PID
Controls the launchd daemon