
2nd Edition Covers El Capitan Learning Unix for OS X GOING DEEP WITH THE TERMINAL AND SHELL Dave Taylor SECOND EDITION Learning Unix for OS X Dave Taylor Boston Learning Unix for OS X by Dave Taylor Copyright © 2016 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://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editor: Rachel Roumeliotis Indexer: Judy McConville Production Editor: Nicole Shelby Interior Designer: David Futato Copyeditor: Sonia Saruba Cover Designer: Randy Comer Proofreader: Rachel Head Illustrator: Rebecca Demarest January 2016: Second Edition September 2012: First Edition Revision History for the Second Edition 2016-01-19: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491939987 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Unix for OS X, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-93998-7 [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 9 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 21 Customizing Your Terminal Session 23 Setting the Terminal’s Title 23 Using AppleScript to Manipulate the Terminal 24 Working with .terminal Files 24 Working with the Terminal 25 The Shell Prompt 27 Entering a Command 27 iii Recalling Previous Commands 28 Completing File and Directory Names 29 Running Multiple Commands on the Command Line 29 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 57 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 73 4. File Management. 75 File and Directory Names 75 File and Directory Wildcards 78 iv | Table of Contents Looking Inside Files 80 cat 80 less 81 grep 83 Creating and Editing Files 85 Text Editors and Word Processors 85 The vi Text Editor 87 vi Basics 89 A Simpler vi Alternative: Pico 95 The More Complex Option: Emacs 95 Managing Files 96 Creating Directories with mkdir 97 Copying Files 98 Renaming and Moving Files with mv 100 Removing Files and Directories 101 Working with Links 103 Compressing and Archiving Files 105 Files on Other Operating Systems 107 5. Finding Files and Information. 109 Searching Inside Files with the grep Command 109 Useful grep Options 110 Working with Regular Expressions 112 Finding Files with locate 116 Fast Filename Search with locate 116 Using find to Explore Your Filesystem 117 Matching by File Size 118 Exploring find Permission Strings 121 Using find to Identify Recently Changed Files 122 find’s Faithful Sidekick: xargs 124 Further Refinements to find 126 Shining a Light on Spotlight 126 Listing Spotlight Metadata with mdls 127 Finding Files with mdfind 131 Making Spotlight Useful 132 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 Table of Contents | v grep 144 head and tail 145 sort 145 uniq 147 Piping Output to a Pager 148 Printing 149 The Unix Way 149 7. Multitasking. 153 Running a Command in the Background 154 Checking on a Process 155 ps 155 top 158 Canceling a Process 160 kill 161 killall 162 Launching GUI Applications 163 open 163 Useful Starting Options for Use with open 165 Making open More Useful 166 8. Taking Unix Online. 169 Remote Logins 169 Web Access 172 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. 187 X11 188 Using X11 189 Differences Between OS X and X11 192 Customizing X11 193 GIMP, the X11 Graphics Editor 195 10. Where to Go from Here. 197 Documentation 197 The man Command 197 Documentation on the Internet 200 Books 202 vi | Table of Contents Customizing Your Unix Experience 202 Shell Aliases and Functions 202 Programming 203 Perl, Python, and Ruby 205 C and C++ 205 Index. 207 Table of Contents | vii Preface Fifteen years ago, when Apple jumped from Mac OS 9 to Mac OS X (pronounce that “oh-ess ten” to sound cool), the entire experience of using a Mac system changed dra‐ matically. Heck, many of you reading this have never known a non–OS X Mac inter‐ face! There were a lot of interface changes, but the biggest update when Apple switched operating systems was that every machine gained multitasking and multi‐ user capabilities. Beneath the shiny graphical interface of OS X lies an operating system called Unix (pronounced “you-nicks”): specifically, UC Berkeley’s BSD Unix and the Mach ker‐ nel, 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 areas in the filesystem, 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. Other than the aforementioned advantages, 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 (you can find this app in /Applications/ Utilities), provides an enormous amount of power for intermediate and advanced 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 ix as some of Unix’s most useful commands. I’ll also give you a tour of some useful Unix commands that Apple’s team has written and are included 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.” Here, you’ll learn all the basic commands you need to get started with Unix. Rather than weighing you down with lots of details, however, I want to help you get comfortable in the Unix environment as soon as possible. So, I cover each command’s most useful features instead of describing all its options in detail. And let me tell you, Unix has thousands of commands with millions of options. It’s very powerful! Fortu‐ nately, though, it’s just as powerful and helpful even if you just focus on a subset of commands and gradually learn more as you need additional power and capabilities.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages238 Page
-
File Size-