www.it-ebooks.info www.it-ebooks.info Dave Taylor Learning Unix for OS X Mountain Lion www.it-ebooks.info ISBN: 978-1-449-33231-0 [LSI] Learning Unix for OS X Mountain Lion by Dave Taylor Copyright © 2012 Dave Taylor. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Brian Jepson and Meghan Blanchette Production Editor: Kristen Borg Copyeditor: Rachel Head Proofreader: BIM Publishing Services Indexer: BIM Publishing Services Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano October 2012: First Edition Revision History for the First Edition: 2012-09-19 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449332310 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning Unix for OS X Mountain Lion, the image of a mountain lion, and related trade dress are trademarks of O’Reilly Media, Inc. 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 O’Reilly Media, Inc., was aware of a trade mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Why Use Unix?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Power of Unix 1 Batch Renames and Extracting File Lists 4 Finding Hidden Files 5 Folders or Directories? 6 Thousands of Free Applications 7 Power Internet Connections 7 Commands Included with Unix 8 Displaying All Unix Commands 9 The 10 Most Common Unix Commands 9 A Simple Guided (Unix) Tour 10 2. Using the Terminal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Launching the Terminal 13 Syntax of a Unix Command 14 Exercise: Entering a Few Commands 16 Types of Commands 17 Changing the Terminal’s Preferences 18 Features of the Terminal 22 Customizing Your Terminal Session 23 Setting the Terminal’s Title 23 Using AppleScript to Manipulate the Terminal 24 Working with .terminal Files 25 Working with the Terminal 25 The Shell Prompt 27 Entering a Command 27 Recalling Previous Commands 28 Completing File and Directory Names 29 iii www.it-ebooks.info Running Multiple Commands on the Command Line 30 Correcting a Command 30 Ending Your Session 31 Problem Checklist 31 Customizing the Shell Environment 32 Picking a Login Shell 32 Changing the Command Prompt 33 Advanced Shell Customization 35 Shell Configuration Settings 35 Creating Aliases 38 The Unresponsive Terminal 39 3. Exploring the Filesystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 The OS X Filesystem 41 Your Home Directory 42 Your Working Directory 42 The Directory Tree 43 Absolute Pathnames 44 Relative Pathnames 45 Changing Your Working Directory 47 Files in the Directory Tree 49 Listing Files and Directories 51 The All-Powerful ls Command 51 Trying Out the ls Command 52 Using the -l Option 55 File Permissions 56 Calculating File Size and Disk Space 59 Calculating Available Disk Space 62 Exercise: Exploring the Filesystem 63 Protecting and Sharing Files 64 File Access Permissions 66 Setting Permissions with chmod 67 Changing the Group and Owner 70 Changing Your Password 71 Superuser Privileges with sudo 72 Exploring External Volumes 74 4. File Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 File and Directory Names 77 File and Directory Wildcards 80 Looking Inside Files 82 cat 82 iv | Table of Contents www.it-ebooks.info less 83 grep 85 Creating and Editing Files 86 Text Editors and Word Processors 87 The vi Text Editor 89 vi Basics 90 A Simpler vi Alternative: Pico 96 The More Complex Option: Emacs 97 Managing Files 98 Creating Directories with mkdir 99 Copying Files 100 Renaming and Moving Files with mv 102 Removing Files and Directories 103 Working with Links 105 Compressing and Archiving Files 107 Files on Other Operating Systems 110 5. Finding Files and Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 The Oddly Named grep Command 111 Useful grep Options 112 Working with Regular Expressions 114 Finding Files with locate 118 Building the locate Database 118 Using locate 119 Using find to Explore Your Filesystem 120 Matching by File Size 121 Exploring find Permission Strings 122 Using find to Identify Recently Changed Files 124 find’s Faithful Sidekick: xargs 125 Further Refinements to find 127 Shining a Light on Spotlight 128 Listing Spotlight Metadata with mdls 128 Finding Files with mdfind 132 Making Spotlight Useful 133 6. Redirecting I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Standard Input and Standard Output 135 Putting Text in a File 137 Pipes and Filters 141 wc 142 tr 143 grep 144 Table of Contents | v www.it-ebooks.info head and tail 145 sort 145 uniq 147 Piping Output to a Pager 147 Printing 149 The Unix Way 149 7. Multitasking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Running a Command in the Background 154 Checking on a Process 155 ps 155 top 157 Other Options for Monitoring Processes 160 Canceling a Process 162 kill 162 killall 163 Launching GUI Applications 164 open 165 Useful Starting Options for Use with open 166 Making open More Useful 167 8. Taking Unix Online. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Remote Logins 169 Web Access 173 Remote Access to Other Unix Systems 173 Transferring Files 176 scp and rcp 176 FTP 177 Easy Shortcuts with New Remote Connection 183 9. Of Windows and X11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 X11 186 Using X11 188 Differences Between OS X and X11 190 Customizing X11 191 GIMP, the X11 Graphics Editor 193 10. Where to Go from Here. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Documentation 195 The Terminal’s Help Menu 195 The man Command 196 Documentation on the Internet 199 vi | Table of Contents www.it-ebooks.info Books 200 Customizing Your Unix Experience 200 Shell Aliases and Functions 200 Programming 201 Perl, Python, and Ruby 202 C and C++ 203 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Table of Contents | vii www.it-ebooks.info www.it-ebooks.info [...]... Crumlish for his early back-room assistance, and to Tim O’Reilly for the opportunity to help revise the popular Learning the Unix Operating System book for the exciting world of OS X, all those years ago xviii | Preface www.it-ebooks.info CHAPTER 1 Why Use Unix? Why would any sane person want to type in a bunch of funny-looking Unix commands when you can just use the mouse? After all, OS X has one... use the Unix command line? That’s a tough sell, but you can boil it down to just one word: power Lying underneath the OS X interface is a powerful Unix system, ready to leap into action at a moment’s notice All you have to do is command Unix to take action One of the greatest pleasures of using Unix within OS X is that you get the benefit of a truly wongreatest pleasures of using Unix within OS X is that... the Unix environment When you log into your OS X system, you’re automatically logged into your Unix acWhen you log into your OS X system, you’re automatically logged into your Unix ac count as well In fact, your Desktop and other customized features of your OS X envicount as well In fact, your Desktop and other customized features of your OS X envi ronment have corresponding features in the Unix environment... amount of power for intermediate and ad), provides an enormous amount of power for intermediate and ad vanced users What’s more, once you’ve learned to use Unix in OS X, you’ll also be able to use the command line in other versions of Unix, such as FreeBSD (from which OS X derives its Unix core) or even the hugely popular Linux This book is designed to teach Mac users the basics of Unix You’ll learn... different versions of Unix and you can use them all without paying attention to their origins From time to time, I explain features of Unix on other systems Knowing the differences can help you if you ever want to use another type of Unix system When I write Unix in this book, I mean Unix and its versions,” unless I specifically mention a particular version Interfaces to Unix Unix can be used as it... to use Unix Understanding it will let you work on any Unix system, with or without windows A great resource for general OS X information (the GUI you’re probably used to) is OS X Mountain Lion: The Missing Manual, by David Pogue (Pogue Press/O’Reilly) How This Book Is Organized This book will help you learn Unix on your Mac fast It is organized in a way that gets you started quickly and then expands... server using an encrypted connection, OS X s Unix command line is quite powerful But don’t take my word for it—Chapter 8 takes you on a detailed tour of command-line Internet utilities Commands Included with Unix While this book covers only about 50 of the most basic Unix commands, there are over a thousand Unix commands included with OS X and you can’t see most of them without accessing the command... landmark release of OS X that true multitasking arrived in the Macintosh world With OS X, Macintosh applications run in separate memory areas; the Mac is a true multiuser system that also includes proper file-level security To accomplish these improvements, OS X made the jump from a proprietary underlying operating environment to Unix OS X is built on top of Darwin, a version of Unix based on BSD 4.4... to learn new commands for each one Versions of Unix There are several versions of Unix Some past and present commercial versions include Solaris, AIX, and HP/UX Freely available versions include Linux, NetBSD, OpenBSD, and FreeBSD Darwin, the free Unix version underneath OS X, was built by grafting an advanced version called Mach onto BSD, with a light sprinkling of Apple magic for the Aqua interface... differ among Unix systems, you should be able to use much of what you learn from this introductory handbook on any system Don’t Preface www.it-ebooks.info | xi worry too much about what’s from what version of Unix Just as English borrows words from French, German, Japanese, Italian, and even Hebrew, OS X s Unix borrows comfrom French, German, Japanese, Italian, and even Hebrew, OS X s Unix borrows com . www.it-ebooks.info www.it-ebooks.info Dave Taylor Learning Unix for OS X Mountain Lion www.it-ebooks.info ISBN: 978-1-449-33231-0 [LSI] Learning Unix for OS X Mountain Lion by Dave Taylor Copyright. registered trademarks of O’Reilly Media, Inc. Learning Unix for OS X Mountain Lion, the image of a mountain lion, and related trade dress are trademarks