Learning Unix for OS X Mountain Lion
Total Page:16
File Type:pdf, Size:1020Kb
Learning Unix for OS X Mountain Lion Dave Taylor 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 [email protected]. Editors: Brian Jepson and Meghan Blanchette Proofreader: BIM Publishing Services Production Editor: Kristen Borg Indexer: BIM Publishing Services Copyeditor: Rachel Head 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. ISBN: 978-1-449-33231-0 [LSI] 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 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 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 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 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 Preface It’s been a long time since we went through the dramatic transition from Mac OS 9 to the more complicated and graphically rich world of OS X. Many of you reading this have never known a Mac interface that wasn’t actually OS X (pronounce that “oh-ess ten” to sound cool). The biggest change when Apple switched operating systems was that every machine then gained multitasking and multiuser capabilities. What you might not have realized, however, is that it was the underlying operating system itself that changed in the update to OS X, and that you now have a tremendously powerful OS that can run thousands of open source applications downloaded free from the Net, along with a command-line interface that makes even the most complex tasks a breeze. If you want to learn the key phrases, beneath OS X lies an operating system called Unix (pronounced “you-nicks”): specifically, UC Berkeley’s BSD Unix and the Mach kernel, a multiuser, multitasking operating system. Being multiuser means OS X allows multiple users to share the same system, each with their own settings, preferences, and separate area in the filesystems, secured from other users’ prying eyes. Being multitasking means OS X can easily run many different applications at the same time, and if one of those applications crashes or hangs, the entire system doesn’t need to be rebooted. Instead, you just force quit the application that’s causing the “Spinning Beach Ball of Death” (you know, when the mouse pointer turns into a spinning color wheel that just won’t stop rotating) and either relaunch it or proceed with your work in other apps. ix The fact that OS X has Unix under the hood doesn’t matter to users who simply want to use its slick graphical interface to run their applications or manage their files. But it opens up a world of possibilities for users who want to dig a little deeper. The Unix command-line interface, which is accessible through the Terminal application (/Appli cations/Utilities), 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 how to use the command line (which Unix users refer to as the shell) and the filesystem, as well as some of Unix’s most useful commands. I’ll also give you a tour of some useful Unix commands that Apple’s team have written and include with every Mac system—utilities that let you really gain control over your system. Unix is a complex and powerful system, so I can only scratch the surface, but I’ll also tell you how to deepen your Unix knowledge once you’re ready for more. Who This Book Is For This book is for savvy Mac users who are comfortable in their current world (the Finder and other GUI applications) but also want to learn more about the “Power of Unix” that Apple keeps talking about. Here, you’ll learn all the basic commands you need to get started with Unix. Rather than weighing you down with lots of details, I want to help you get comfortable in the Unix environment as soon as possible. So, I cover each com mand’s most useful features instead of describing all its options in detail.