Mark G. Sobell - Practical Guide to Linux Commands, Editors and Shell Programming (2013, Prentice Ha

This preview shows page 1 out of 1169 pages.

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

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture