Table Of ContentVISUAL QUICKStArt GUIDE
unix and linux
Fourth Edition
Deborah S. ray and Eric J. ray
Peachpit Press
Visual QuickStart Guide
Unix and Linux, Fourth Edition
Deborah S. Ray and Eric J. Ray
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 (fax)
Find us on the Web at: www.peachpit.com
To report errors, please send a note to: errata@peachpit.com
Peachpit Press is a division of Pearson Education.
Copyright © 2009 by Deborah Ray and Eric Ray
Editor: Rebecca Gulick
Copy Editor: Liz Welch
Proofreader: Elle Yoko Suzuki
Production Coordinator: Myrna Vladic
Compositor: Debbie Roberti
Technical Reviewer: Stephen Talley
Indexer: James Minkin
Cover design: Peachpit Press
Notice of rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic,
mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For
information on getting permission for reprints and excerpts, contact permissions@peachpit.com.
Notice of Liability
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has
been taken in the preparation of the book, neither the authors nor Peachpit Press 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
instructions contained in this book or by the computer software and hardware products described in it.
trademarks
Visual QuickStart Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Other
product names used in this book may be trademarks of their own respective owners. Images of Web sites in
this book are copyrighted by the original holders and are used with their kind permission. This book is not
officially endorsed by nor affiliated with any of the above companies.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim,
the designations appear as requested by the owner of the trademark. All other product names and services
identified throughout this book are used in editorial fashion only and for the benefit of such companies with
no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to
convey endorsement or other affiliation with this book.
ISBN 13: 978-0-321-63678-2
ISBN 10: 0-321-63678-3
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
Dedication
To each other, Ashleigh, and Alex.
Acknowledgments
This book came together with the invaluable assistance of a number
of very talented and supportive people. Thanks to Clifford Colby for
his continued confidence and support. Rebecca Gulick was a delight
to work with and helped tremendously in pulling the various pieces
together. Elle Yoko Suzuki was not only great as a proofreader, but pro-
vided super technical feedback as well. Liz Welch was really helpful as
copy editor. Myrna Vladic and Deb Roberti did a great job in production,
even with our special needs. And, yet again, Steve Talley’s careful atten-
tion to detail and deep knowledge of the idiosyncrasies of Unix helped
iron out technical rough spots. Thanks, all!
Table of Contents
Introduction xi
Chapter 1: Getting Started with Unix 1
Accessing a Unix System .........................3
Connecting to the Unix System ..................7
Logging In ......................................10
Changing Your Password with passwd ...........11
T
Listing Directories and Files with ls .............13 a
b
Changing Directories with cd ...................15 l
e
Finding Yourself with pwd .......................17 o
f
Piping Input and Output ........................18 C
Redirecting Output .............................19 o
n
Using Wildcards ................................21 t
e
Viewing File Contents with more ................22 n
t
Displaying File Contents with cat ...............23 s
Exploring the System ...........................25
Getting Help with man ...........................26
Logging Out ....................................28
Chapter 2: Using Directories and Files 29
Creating Directories with mkdir .................30
Creating Files with touch .......................32
Copying Directories and Files with cp ...........34
Listing Directories and Files with ls
(More Goodies) ............................36
Moving Files with mv ............................38
Removing Files with rm .........................39
Removing Directories with rmdir ...............42
Finding Forgotten Files with find ...............44
Locating Lost Files with locate .................46
Linking with ln (Hard Links) ....................47
Linking with ln -s (Soft Links) .................49
v
Table of Contents
Chapter 3: Working with Your Shell 51
Discovering Which Shell You’re Using ...........52
Understanding Shells and Options ..............53
Changing Your Shell with chsh ..................55
Changing Your Shell Temporarily ................57
Using Completion in the bash Shell .............59
Viewing Session History in the bash Shell .......60
Using Completion in the zsh Shell ...............62
Viewing Session History in the zsh Shell .........63
Changing Your Identity with su .................65
Fixing Terminal Settings with stty ..............67
Exiting the Shell ................................68
Chapter 4: Creating and Editing Files 69
Choosing an Editor .............................70
Starting pico and Dabbling with It ..............73
s
t Saving in pico ..................................74
n
e Cutting and Pasting Text Blocks in pico .........75
t
n Checking Spelling in pico .......................76
o Getting Help in pico ............................77
C
f Exiting pico ....................................78
o
e Starting vi and Dabbling with It .................79
l Saving in vi .....................................81
b
a Adding and Deleting Text in vi ..................82
T
Importing Files into vi ..........................83
Searching and Replacing in vi ...................84
Exiting vi .......................................86
Starting emacs and Dabbling with It .............87
Using emacs Menus to Spell-Check ..............89
Saving in emacs .................................90
Exiting emacs ...................................91
Chapter 5: Controlling Ownership
and Permissions 93
Understanding File Ownership
and Permissions ............................94
Finding Out Who Owns What ...................95
Finding Out Which Group You’re In .............97
Changing the Group Association of Files
and Directories with chgrp .................99
Changing Ownership of Files and
Directories with chown ....................101
Changing Permissions with chmod ..............103
Translating Mnemonic Permissions
to Numeric Permissions ...................106
Changing Permission Defaults with umask ......107
vi
Table of Contents
Chapter 6: Manipulating Files 109
Counting Files and Their Contents with wc .....110
Viewing File Beginnings with head .............111
Viewing File Endings with tail ................112
Finding Text with grep .........................113
Using Regular Expressions with grep ...........114
Using Other Examples of
Regular Expressions .......................116
Making Global Changes with sed ...............117
Changing Files with awk ........................118
Comparing Files with cmp ......................120
Finding Differences in Files with diff ..........121
Finding Differences in Files with sdiff .........122
Sorting Files with sort .........................123
Eliminating Duplicates with uniq ..............125
Redirecting to Multiple Locations with tee .....126 T
a
Changing with tr ..............................127 b
l
Formatting with fmt ...........................129 e
Splitting Files with split ......................131 o
f
C
Chapter 7: Getting Information o
n
About the System 133 t
e
Getting System Information with uname ........134 n
t
Viewing File Systems with df ...................135 s
Determining Disk Usage with du ...............138
Finding Out File Types with file ...............139
Finding Out About Users with finger ..........140
Learning Who Else Is Logged in with who .......143
Learning Who Else Is Logged in with w .........144
Getting Information About Your
Userid with id .............................146
Chapter 8: Configuring Your Unix Environment 147
Understanding Your Unix Environment ........148
Discovering Your Current Environment ........150
Adding or Changing Variables ..................152
Looking at Your zsh Configuration Files ........154
Adding to Your zsh Path .......................158
Changing Your zsh Prompt ....................160
Looking at Your bash Configuration Files .......163
Adding to Your bash Path ......................166
Changing Your bash Prompt ...................168
Setting Aliases with alias .....................170
vii
Table of Contents
Chapter 9: running Scripts and Programs 173
Running a Command ..........................174
Scheduling Onetime Jobs with at ..............175
Scheduling Regularly Occurring
Jobs with cron ............................178
Suspending Jobs ...............................180
Checking Job Status with jobs .................181
Running Jobs in the Background with bg .......182
Running Jobs in the Foreground with fg ........183
Controlling Job Priority with nice ..............184
Timing Jobs with time .........................185
Finding Out What Processes Are
Running with ps ...........................187
Deleting Processes with kill ..................189
Chapter 10: Writing Basic Scripts 191
s
t Creating a Shell Script .........................192
n
e Running a Shell Script .........................194
t
n Making a Script Executable ....................195
o
C Getting a Head Start on Scripts
f with history ..............................197
o
e Embedding Commands ........................198
bl Looping Your Scripts ...........................200
a
T Creating If-Then Statements ...................202
Accepting Command-Line Arguments
in Your Scripts ............................205
Accepting Input While a Script Is Running .....206
Debugging Scripts .............................208
Chapter 11: Sending and reading E-mail 209
Choosing an E-mail Program
and Getting Started .......................210
Reading E-mail with pine ......................212
Sending E-mail with pine ......................214
Customizing pine .............................216
Reading E-mail with mutt ......................218
Sending E-mail with mutt ......................220
Reading E-mail with mail ......................222
Sending E-mail with mail ......................223
Creating a Signature File .......................225
Automatically Forwarding
Incoming Messages .......................227
Announcing an Absence with vacation ........228
Configuring procmail .........................230
Managing E-mail with procmail ...............232
viii
Table of Contents
Chapter 12: Accessing the Internet 235
Getting Familiar with Unix Internet Lingo ......236
Logging in to Remote Systems with ssh ........238
Logging in to Remote Systems with telnet .....239
Communicating with Others Using write ......241
Communicating with Others Using talk .......242
Getting Files from the Internet with ftp ........243
Sharing Files on the Internet with ftp ..........247
Surfing the Web with links ....................249
Surfing the Web with lynx .....................251
Downloading Web Sites with wget .............253
Checking Connections with ping ..............254
Tracing Connections with traceroute .........255
Matching Domain Names with IP Addresses ...257
Chapter 13: Working with Encoded
T
and Compressed Files 259 a
b
Encoding Files with uuencode ..................260 le
Decoding Files with uudecode ..................263 o
f
Archiving with tar .............................264 C
Unarchiving Files with tar .....................266 o
n
Compressing Files with compress ..............267 t
e
Uncompressing Files with uncompress .........268 n
t
Zipping a File or Directory with gzip ...........269 s
Unzipping a gzip File with gunzip .............270
Zipping Files and Directories with zip .........271
Unzipping Zipped Files with unzip .............272
Combining Commands ........................273
Chapter 14: Using Handy Utilities 275
Calendaring with cal ..........................276
Calculating with bc ............................279
Evaluating Expressions with expr ..............280
Converting with units .........................281
Looking It Up with look. . . . . . . . . . . . . . . . . . . . . . . .282
Keeping a Record of Your Session
with script ...............................283
ix