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 Cust
Trang 3Dave Taylor
Learning Unix for OS X
Mountain Lion
Trang 4ISBN: 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.
Trang 5Table 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
Trang 6Running 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
Trang 7less 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
Trang 8head 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
Trang 9Books 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
Trang 11It’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
Trang 12The 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 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! But fortunately, it’s just as powerful and helpful even if you just focus on a core subset and gradually learn more as you need additional power and capabilities
Who This Book Isn’t For
If you’re seeking a book that talks about how to build Mac software applications, this isn’t it (although it’s quite helpful for developers to have a firm grasp of Unix essentials, because you never know when you’re going to need them) And if you’re a complete beginner and are occasionally stymied by where the second mouse button has gone, you might be better off putting this book on the shelf until you’re more comfortable with your Macintosh
Finally, if you live and breathe Unix every day, this book is probably too basic for you
I also don’t cover either Unix system administration or Mac system administration from
the command line For example, if you already know what a PID is and how to kill a
program, this book is probably beneath your skill level But if you don’t know what those terms mean, or if you’re somewhere in between, you’ve found the right book!
x | Preface
Trang 13A Brief History of Unix
The Macintosh started out with a single-tasking operating system that allowed simple switching between applications through an application called the MultiFinder More recent versions of the Mac OS have supported multiple applications running simultaneously, but it wasn’t until the 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 Lite, FreeBSD, NetBSD, and the Mach microkernel
Unix itself was invented more than 40 years ago for scientific and professional users who wanted a very powerful and flexible OS It has evolved since then through a remarkably circuitous path, with stops at Bell Telephone Labs, UC Berkeley, and research centers in Australia and Europe, and also received some funding from the US Department of Defense Advanced Research Projects Agency (DARPA) Because Unix was designed by experts for experts (or “geeks,” if you prefer), it can be a bit overwhelming at first But after you get the basics (from this book!), you’ll start to appreciate some of the reasons
to use Unix For example:
• It comes with a huge number of powerful programs, and you can get many others for free on the Internet (The Fink project, available from SourceForge—http://
fink.sourceforge.net—brings many open source packages to OS X.) You can thus do
much more at a much lower cost
• Unix is pretty much the same, regardless of whether you’re using it on OS X, FreeBSD, or Linux, or even in tiny embedded systems or on a giant supercomputer After you read this book, you’ll not only know how to harness the power of Unix, but you’ll also be ready to use many other kinds of Unix-based computers without having 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
Although GUIs and advanced features 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 | xi
Trang 14worry 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 commands from many 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 was originally designed: on typewriter-like terminals, from a prompt on a command line Most versions of Unix also work with window systems, or graphical user interfaces (GUIs) These allow each user to have a single screen with multiple windows—including “terminal” windows that act like the original Unix interface
OS X includes a simple terminal application for accessing the command-line level of the system That application is called the Terminal and is closely examined in Chapter 2.While you can use your Mac quite efficiently without issuing commands in the Terminal, that’s where we’ll spend all of our time in this book Why?
• Every modern Macintosh has a command-line interface If you know how to use the command line, you’ll always be a power user
• If you become a more advanced Unix user, you’ll find that the command line is actually much more flexible than the graphical Mac interface Unix programs are designed to be used together from the command line—as “building blocks”—in an almost infinite number of combinations, to do an infinite number of tasks No window system I’ve seen has this tremendous power
• You can launch and close any Mac program from the command line
• Once you learn to use the command line, you can use those same techniques to
write scripts These little (or big!) programs automate jobs you’d have to do manually
and repetitively with a window system (unless you understand how to program a window system, which is usually a much harder job) See Chapter 10 for a brief introduction to scripting
• In general, text-based interfaces are much easier than graphical computing environments for visually impaired users
I’m not saying that the command-line interface is right for every situation For instance, using the Web—with its graphics and links—is usually easier with a GUI web browser
xii | Preface
Trang 15within OS X But the command line is the fundamental way 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 your Unix horizons, chapter by chapter, until you’re comfortable with the command line and with X11-based open source applications and able to push further into the world of Unix Specific commands, for example, may
be previewed in earlier chapters and then explained in detail in later chapters (with cross references so you don’t get lost) Here’s how it’s all laid out:
Chapter 1, Why Use Unix?
Graphical interfaces are useful, but when it’s time to become a power user—really forcing your Mac to do exactly what you want, when you want it—nothing beats the power and capability of the Unix command line You’ll see exactly why that’s the case in this first chapter
Chapter 2, Using the Terminal
It’s not the sexiest application included with OS X, but the Terminal, found in the / Applications/Utilities folder, opens up the world of Unix on your Mac and lets you
peek inside the inner workings This chapter explains how to best use it and customize it for your own requirements
Chapter 3, Exploring the Filesystem
Once you start using Unix, you’ll be amazed at how many more files and directories are on your Mac—information that’s hidden from the graphical interface user This chapter takes you on a journey through your Mac’s filesystem, showing you how to list files, change directories, and explore the hidden nooks and crannies of Mountain Lion
Chapter 4, File Management
Now that you can move around in your filesystem, it’s time to learn how to look into individual files; copy or move files around; and even create, delete, and rename directories This is your first introduction to some of the most powerful Unix com
mands, too, including the text-based vi editor.
Chapter 5, Finding Files and Information
If you’ve ever looked for a file with the Finder or Spotlight, you know that some types of searches are almost impossible Looking for a file that you created exactly
30 days ago? Searching for that file with the Finder will prove to be an exercise in
futility But that’s exactly the kind of search you can do with Unix’s find, locate, and grep commands, as well as Spotlight’s command-line utilities.
Preface | xiii
Trang 16Chapter 6, Redirecting I/O
One of the most powerful elements of the Unix command line is that you can easily combine multiple commands to create new and unique “super-commands” that perform exactly the task you seek You’ll learn exactly how you can save a command’s output to a file, use the content of files as the input to Unix commands, and even hook multiple commands together so that the output of one is the input of the next You’ll see that Unix is phenomenally powerful, and easy, too!
Chapter 7, Multitasking
As mentioned earlier, Unix is a multitasking operating system that allows you to
have lots of applications running at the same time In this chapter, you’ll see how you can manage these multiple tasks, stop programs, restart them, and modify how they work, all from the Unix command line
Chapter 8, Taking Unix Online
Much of the foundation of the Internet was created on Unix systems, and it’s no surprise that you can access remote servers, surf the Web, and interact with remote filesystems, all directly from the command line If you’ve always wanted more power when interacting with remote sites, this chapter dramatically expands your horizons
Chapter 9, Of Windows and X11
The graphical interface in OS X is the best in the industry Elegant and intuitive, it’s
a pleasure to use But it turns out that there’s another, Unix-based graphical interface lurking in your Mac system, called the X Window System, or X11 for short This chapter shows you how to install X11 and gives you a quick tour of some of the very best X11 applications available for free on the Internet
Chapter 10, Where to Go from Here
With all its commands and command-line combinations, and the addition of thousands of open source utilities free for the downloading, you can spend years learning how to best take advantage of the Unix environment In this final chapter, I offer you some directions for your further travels, including recommendations for favorite books, websites, and similar resources
Conventions Used in This Book
The following typographical conventions are used in this book:
Plain text
Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Control)
xiv | Preface
Trang 17Constant width bold
Shows commands or other text that should be typed literally by the user
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context
Menus/navigation
Menus and their options are referred to in the text as File→Open, Edit→Copy, etc Arrows are also used to signify a navigation path when using window options; for example, System Preferences→Screen Effects→Activation means you would launch System Preferences, click on the icon for the Screen Effects preferences panel, and select the Activation pane within that panel
Menu symbols
When looking at the menus for any application, you will see some symbols associated with keyboard shortcuts for a particular command For example, to open a document in Microsoft Word, you could go to the File menu and select Open (File→Open), or you could issue the keyboard shortcut ⌘-O
Figure P-1 shows the symbols used in the various menus to denote a keyboard shortcut
Rarely will you see the Control symbol used as a menu command option; it’s more often used in association with mouse clicks to emulate a right-click on a two-button mouse or for working with the bash shell.
Preface | xv
Trang 18Figure P-1 Keyboard accelerators for issuing commands
$,#
The dollar sign ($) is used in some examples to show the user prompt for the bash shell; the hash mark (#) is the prompt for the root user.
This icon signifies a tip, suggestion, or general note
This icon indicates a warning or caution
Using Code Examples
This book is here to help you get your job done In general, you may use the code in this book in your programs and documentation You do not need to contact us for permission unless you’re reproducing a significant portion of the code For example, writing a program that uses several chunks of code from this book does not require permission Selling or distributing a CD-ROM of examples from O’Reilly books does require permission Answering a question by citing this book and quoting example code does not require permission Incorporating a significant amount of example code from this book into your product’s documentation does require permission
We appreciate, but do not require, attribution An attribution usually includes the title,
author, publisher, and ISBN For example: “Learning Unix for OS X, by Dave Taylor
Copyright 2012 Dave Taylor, 978-1-4493-3231-0.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com
xvi | Preface
Trang 19A Word on OS X Versus Mac OS X
Apple has always referred to the latest generation of the Mac operating system as “Mac
OS X,” but with the introduction of Mountain Lion and the path toward a unified user experience across computers and more portable devices like the Apple iPad and Apple iPhone, the operating system has been rebranded simply “OS X.” We will endeavor to use this new name for the system throughout the book, unless it makes more sense in context to use the older version of the OS name
Safari® Books Online
Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training
Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more For more information about Safari Books Online, please visit us online
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc
1005 Gravenstein Highway North
Trang 20To comment or ask technical questions about this book, send email to bookques
tions@oreilly.com.
For more information about our books, courses, conferences, and news, see our website
at http://www.oreilly.com
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
The Evolution of This Book
This book is loosely based on the popular O’Reilly title Learning the Unix Operating
System, by Jerry Peek, Grace Todino, and John Strang (currently in its fifth edition)
There are lots of differences in this book to meet the needs of OS X users, but the fundamental layout and explanations are the same The Mountain Lion edition is the fifth OS X custom edition of this title As OS X keeps getting better, so does this little book
Acknowledgments
I’d like to acknowledge the work of Meghan Blanchette and Brian Jepson at O’Reilly Without their work constantly explaining the nuances of the version tracking system we’ve used, I would have given up and made a really long YouTube video about the command line instead I would also like to express my gratitude to Dave Kitabjian, who did an extraordinary job as our tech editor, helping uncover the many obsolete portions
of this manuscript as we updated a seven-year-old book for Mountain Lion Thanks also
to Christian 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
Trang 21CHAPTER 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 of—if not the—best looking
user interfaces out there, so what would compel you, a Mac user through and through,
to 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 wonderful graphical environment and the underlying power of the Unix command line There’s no denying it’s a match made in heaven Even Apple promotes OS X with the tagline, “The world’s most advanced desktop operating system.”
This chapter sets the stage for the rest of the book, answering the question: “Why use Unix when you have a perfectly good Mac graphical interface?” It’s an important question, but I think that if you give it a try, you’ll agree that joining the Unix world is really like learning you have a completely separate, and even more powerful, operating system lurking in your machine
The Power of Unix
It’s quite reasonable to question why you should have to remember commands and type them in If you’re a long-time Mac user who is familiar and happy with the capabilities
and logic of the Aqua interface, you might need some convincing that Unix is your friend Here’s why: dipping into the primarily text-based Unix tools on your OS X system
gives you more power and control over both your computer and your computing environment There are other reasons too, including:
1
Trang 22• There are thousands of open source and otherwise freely downloadable Unix-based applications Can’t afford Adobe Photoshop but still want a powerful graphics editor? The GNU Image Manipulation Program (GIMP) offers a viable solution (see Chapter 9).
• Want to search for files by when they were created, or by whom? Difficult in the Finder or Spotlight, but it’s a breeze with Unix (see Chapter 5)
• How about managing your files and file archives in an automated fashion? Tricky
to set up with the GUI tools, but in Unix, you can set up a cron job to handle this
at night while you sleep
Fundamentally, Unix is all about power and control As an example, consider the dif
ference between using Force Quit from the Apple menu and the Unix programs ps and kill While Force Quit is more attractive, as shown in Figure 1-1, notice that it lists only graphical applications
Figure 1-1 Force Quit doesn’t show all running applications
By contrast, the ps (process status) command used from within the Terminal application (/Applications/Utilities/Terminal) shows a complete and full list of every application,
utility, and system process running on your Mac, as shown here:
Trang 231583 ?? 0:24.08 Google Chrome Helper
Okay, now let’s go back and look at the output from running the ps -acx command First off, you’ll see that we added some options (or flags or switches) to the ps command; the options are the -acx bit These options tell ps to display all of the programs and processes
being run by all of the users (including you and the system, itself) on the system When
the Terminal displays the results of the ps -acx command, you’ll see that it adds a line
of “headers” to the output:
listed above (diskarbitrationd—the final “d” stands for daemon, an always-running
system-level task)
TIME
Tells you the amount of time it took to run that particular process, or how long that process has been running, in minutes and seconds For example, the 0:00.24 you see in the output above means that it took, roughly, a quarter of a second for the
diskarbitrationd process to start and run.
The Power of Unix | 3
Trang 24Great! So now you know what all that means, but you still don’t know how this relates
to Force Quit, right? Well, be patient, we’re getting there
Once you know a process’s PID number, you can then issue the Unix kill command to,
well, kill that process For example, let’s say that Microsoft Word decides to lock up on you and you’re stuck with the Spinning Beach Ball of Death (SBBoD) syndrome After
you finish screaming, you need to kill Microsoft Word, but in order to do so, you first need its process number For this, we’ll add the grep command, which is basically a Unix
search tool that you use to search for words or numbers in files, or in this case, the output
of a command:
$ ps -ax | grep Word
1634 ?? 0:02.50 /Applications/Microsoft Office 2011/Microsoft
Word.app/Contents/MacOS/Microsoft Word -psn_0_766139
1645 ttys002 0:00.00 grep Word
This tells us that Microsoft Word’s PID is 1634, as noted by the first number in the
command output Now all you need to do to kill Word is issue the following command:
$ kill 1634
After typing that and hitting the Return key (an activity known as “entering a command”), Microsoft Word promptly quits, closing all its windows It won’t save anything you’ve done, since your last save, but since Word was locked in a deep freeze you wouldn’t have been able to save your changes anyway, right? And if you had used the Force Quit window, you wouldn’t have been able to save changes there, either
Batch Renames and Extracting File Lists
Here’s another example Suppose you just received a data CD from a client with hundreds
of files in a single folder Now let’s say that you only need those files that have the
sequence -nt- or -dt- as part of their filenames, and that you want to copy them from
the CD to your home directory Within the Finder, you’d be doomed to going through the list manually, a tedious and error-prone process But on the Unix command line, this becomes a breeze:
$ cd /Volumes/MyCDROM
$ cp *-dt-* *-nt-* ~
The first command, cd /Volumes/MyCDROM, takes you to the Volumes directory, which
is where the CD (named MyCDROM) is actually mounted on your Mac’s filesystem The second command, cp *-dt-* *-nt-* ~, breaks down as follows:
4 | Chapter 1: Why Use Unix?
Trang 25Fast, easy, and doable by any and all OS X users.
There are a million reasons why it’s helpful to know Unix as an OS X power user, and you’ll see them demonstrated time and again throughout this book
Finding Hidden Files
You might not realize it if you only work in the Finder, but your system has thousands
of additional files and directories that are hidden from view, but easily found on the
command line These hidden files are known in the Unix world as dot files, because each
file or directory has a period (.) as the first character of its name For example, in your
home directory you probably have a file called profile that contains specific instructions
on how you want your command shell set up when it’s launched But when you view your home folder in the Finder, this file is hidden, as shown in Figure 1-2 Instead, all
you see are the default set of folders (Desktop, Documents, Library, Movies, Music, Pic tures, Public, and Sites) and a file called myopen.
To view the dot files in the Terminal, type the file listing command (ls), along with its -a option (for list all, which shows the hidden dot files) Suddenly you’ll see that there
are lots more files in that directory:
$ ls -a
./ .dropbox/ vuescanrc Pictures/
/ dvdcss/ Desktop/ Presentations/
.CFUserTextEncoding lesshst Documents/ Public/
.DS_Store nchsoftware/ Downloads/ Sites/
.Trash/ profile Dropbox/ bin/
.android/ ssh/ Library/
.bash_history .subversion/ Movies/
.cups/ .viminfo Music/
The Power of Unix | 5
Trang 26Figure 1-2 The Finder doesn’t show hidden files that you can see in the Terminal with standard Unix commands
Personally, though I don’t always need the power, I like knowing that I can get to, view, and even edit every file on my computer if I need to All I need to do is launch the Terminal application (which I actually have permanently available on my Dock, so it’s always just one click away), type in a few simple commands, and I’m on my way
Folders or Directories?
If you’re new to the whole Unix thing, you’re going to need to learn Unix-speak In the graphical world, such as with OS X or Windows, you’re used to working with a graphical user interface (GUI) that lets you see everything visually When you create a new file, it gets stored in a folder of some sort, even if you save the file to your Desktop (which is,
in its own right, a folder)
But in Unix-land, folders are referred to as directories That’s right, folders and directories
are one and the same It’s an odd sort of translation, but when Unix was first developed, there was no GUI; all you had was a text-based terminal to type into, and you were darn happy to have that, especially as you were walking 10 miles uphill in the snow to and from school each day But I digress!
Directories were set up as part of the hard drive’s filesystem, or the structure in which
directories and files are stored on the system And the way you get to a folder (er, di
rectory) in Unix is to enter its file path, using forward slashes between the directory
names For example, the file path to your home directory (again, think folder) is actually:
/Users/your_name
6 | Chapter 1: Why Use Unix?
Trang 27where your_name would be replaced by your short username Or use the power user shortcut ~, as shown earlier!
At the very top of your Mac’s filesystem, you have the root directory, denoted with a
single forward slash (/) As noted in the example above, to specify a particular directory
or file all you need to do is place the path after this leading slash
This takes a little getting used to, but once you get the hang of entering Unix file paths, you’ll find that it’s actually a faster way to get around (particularly if you can type faster than it takes you to move the mouse around in the graphical world)
Just remember: folders are directories, and when working on the Unix side of your Mac, we’ll refer to folders as directories throughout the book
Thousands of Free Applications
This should appeal to anyone who is a part of the Macintosh community: by warming
up to Unix and its command line, you are joining the much-lauded free software move
ment, since OS X is based on a free, open source Unix operating system called Dar win What’s excellent is that there are thousands of different applications available for
open source operating systems, including design, development, scientific, and business applications that compare quite favorably to expensive commercial alternatives And don’t make the mistake of assuming that all open source applications are command-line tools and utilities! Some of the very best applications, like the GIMP graphics editor (http://www.gimp.org) and the NeoOffice suite (http://www.neooffice.org), are designed
to work either within the X Window System (also known as X11), a standard Unix graphical interface that Apple includes with your OS X system, or directly in OS X’s Aqua GUI environment
Power Internet Connections
If you’re someone who uses the Internet daily, you already know that there are a bunch
of useful Mac OS X applications available to help you be more efficient Unfortunately, lots of them seem to have a price tag attached—even a simple FTP program like Fetch (http://www.fetchsoftworks.com) But why spend $29 on an application when you can
use OS X’s built-in ftp command-line utility for free?
For example, if you wanted to download the cover image for this book from O’Reilly’s
website, you could use the following commands (as noted in bold type):
$ ftp ftp.oreilly.com
Connected to ftp.oreilly.com.
220 ProFTPD 1.3.1rc2 Server (ftp.oreilly.com) [208.201.239.51]
Name (ftp.oreilly.com:taylor): anonymous
331 Anonymous login ok, send your complete email address as your password
Password:
230-Welcome to the O'Reilly Media, Inc FTP Archive.
Thousands of Free Applications | 7
Trang 28Local date and time: Mon Apr 23 23:04:14 2012
> Hello 71.237.2.63
> There are 2 users out of 100 allowed in your usage class
Check us out on the web at http://oreilly.com
230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /pub/graphics/book-covers/low-res
250 CWD command successful
ftp> get 0596009151.gif
local: 0596009151.gif remote: 0596009151.gif
229 Entering Extended Passive Mode (|||62244|)
150 Opening BINARY mode data connection for 0596009151.gif (267646 bytes)
That downloads the image file for the cover of this book to your Mac, which is nice, but
what if you want to look at it? Sure, you could go to the Finder, find the file, and then
double-click on the file’s icon to open it in Preview, but that’s a lot of work Instead, with
a little help from Unix, you can just type in the following command:
$ open 0596009151.gif
$
The open command, which is special to OS X, examines the file it’s supposed to open (0596009151.gif), detects which application should open it by default (something you
can see in a file’s Get Info window), and then opens the file in Preview—all in a fraction
of a second! See how much time Unix just saved you (not to mention the $29!)?From logging into your Mac from remote locations to transferring files from your system
to a 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 line From sophisticated software development environments to web browsers, file transfer utilities to encryption and compression utilities, almost everything you can do in the Aqua interface—and more—can be done with a few carefully chosen Unix commands
8 | Chapter 1: Why Use Unix?
Trang 29Displaying All Unix Commands
To quickly see all of the binary executables—Unix programs—on your system, open the Terminal, hold down the Shift key, and press Esc-?, or press Control-X followed by Shift-1 (using Shift-1 to get an exclamation mark)
Before the commands are displayed in the Terminal, however, you’ll first be prompted (asked) to make a choice:
$
Display all 1718 possibilities? (y or n)
If you press the n key on your keyboard, you’ll be taken back to a command prompt and nothing else will happen However, if you press the y key, you’ll see a multicolumn list
of Unix commands stream past in the Terminal window At the bottom of the screen, you’ll see:
More This lets you know that there’s more to display If you hit the space bar, the next “page”
of commands scrolls into view Keep pressing the space bar to view the entire list of commands; or, if you’re getting tired of that, just hit “q” to quit the output and go back
to the command prompt
If you’re a software developer or are just curious about programming, for example, you’ll want to install the optional Xcode Tools, available as a free download from the Apple Developer’s Site The Xcode Tools give you a full, professional-grade software development environment that lets you develop new applications in Objective-C, C, or C++ Pretty nice for something free from Apple, eh?
The 10 Most Common Unix Commands
If you want to just jump in and try things out, here are the 10 most common commands, with a very short summary of what each does:
Removes a file, set of files, or folder(s) full of files
Commands Included with Unix | 9
Trang 30Use the rm command with caution; there’s no “Trash” where things are
moved to Once you’ve used rm to delete something, it’s gone forever.
Accesses OS X’s built-in documentation for the Unix commands To read the man
page for the ls command, for example, type in man ls.
less filename
Displays a long text file, one screen at a time Pressing the space bar gets the next page when you’re ready, and pressing Q at any time quits the program and returns you to the command prompt
grep pattern filename(s)
Searches for the specified pattern across as many files as you desire—a fast way to find that email message you sent to Uncle Linder, for example
top
Shows you which applications and processes are running on your system, including those that the Finder’s Force Quit window ordinarily hides
A Simple Guided (Unix) Tour
Enough talking about what Unix can do; it’s time to flex your fingers, open up your Mac, and try a few commands so you can get a sense of how it all works!
The first step is to launch the Terminal application, through which you’ll interact with
the command shell Terminal is tucked into the Utilities folder within your Applica tions folder.
Since you’ll be using the Terminal application throughout this book (and hopefully in the future, as you grow more comfortable with Unix), you should drag the Terminal’s icon to the Dock so it’s always at the ready Or, if the Terminal’s already running, you can Control-click on its icon in the Dock and select Options→Keep in Dock, as shown
in Figure 1-3
10 | Chapter 1: Why Use Unix?
Trang 31Figure 1-3 Control-click the Terminal’s Dock icon, and select “Keep in Dock” from the Options menu so it will always be there when you need it
Throughout the following examples, type in the commands you see in bold, pressing
the Return key after each one (again, this is known as “entering a command” in speak) Preceding each command, I’ve included some comments to let you know what you’re about to do
Unix-Without any arguments, the cd command moves you to your home directory:
$ cd
The pwd (present working directory) command shows you the path for the directory
you’re currently in:
$ pwd
/Users/taylor
Use the ls command to list the files in your home directory; compare this listing with
the picture of the Finder window shown in Figure 1-2 If you omit the -a option, all the
hidden dot files stay hidden in this directory:
$ ls
Desktop Dropbox Music Public
Documents Library Pictures Sites
Downloads Movies Presentations bin
Now let’s change directories to your Library folder:
$ cd Library
A Simple Guided (Unix) Tour | 11
Trang 32Use the ls command again to see what’s inside (there’s very little here you’ll need to mess
with):
$ ls
Accounts FontCollections PreferencePanes
Address Book Plug-Ins Fonts Preferences
Application Support Fonts Disabled Printers
Assistants Frameworks PubSub
Audio Google Safari
Automator Icons Saved Application State
Autosave Information Input Methods Screen Savers
Breakpad Internet Plug-Ins Services
Caches Keyboard Layouts Snapz Pro X
Calendars Keychains Sounds
ColorPickers LaunchAgents Spelling
Colors Logs StickiesDatabase
Compositions Mail SyncedPreferences
Containers Mail Downloads Voices
Cookies Messages WebKit
Developer Metadata Widgets
FIPLAB Mobile Documents disclabel
Favorites PDF Services iMovie
FileSync Parallels iTunes
Now let’s go back a directory For this, use the shortcut for moving up one directory
in the filesystem In this case, since you were in your Library folder (e.g., /Users/taylor/ Library, or just ~/Library), the following command moves you back to your home di rectory (as noted by the pwd command that follows):
$ cd
$ pwd
/Users/taylor
Finally, when it’s time to quit the Terminal, use the exit command rather than just quit
ting the application with ⌘-Q:
a Mac user Ready? Let’s go!
12 | Chapter 1: Why Use Unix?
Trang 33CHAPTER 2 Using the Terminal
With a typical Unix system, a staff person has to set up an account for you before you can use it With OS X, however, the operating system installation process automatically
creates a default user account The account is identified by your username, which is
usually a single word or an abbreviation Think of this account as your office—it’s your personal place in the Unix environment
When you log into your OS X system, you’re automatically logged into your Unix account as well In fact, your Desktop and other customized features of your OS X environment have corresponding features in the Unix environment Your files and programs can be accessed either through the Finder or through a variety of Unix command-line utilities that you can use in OS X’s Terminal application
In this chapter, you’ll not only learn about the Terminal and how to customize it for your own needs, but you’ll also gain an understanding of the command-line nature of
OS X when accessed through the Terminal If you’re used to moving your mouse around and clicking on buttons, this might seem wonderfully—or awkwardly—retro, but as is
so often the case, the differences between the Finder and the Terminal are part of what makes the Terminal, and Unix, so remarkably powerful
Launching the Terminal
The way you use Unix on OS X is through an application known as the Terminal, or, to
Mac geeks, Terminal.app (pronounced “Terminal dot app”) Open a Finder window and look in Utilities, as shown in Figure 2-1 Double-click on “Terminal” and it will start up, presenting you with a dull, uninspiring white window with black text that says “Last login:” and a shell prompt
13
Trang 34Figure 2-1 Finding Terminal in the Utilities folder
By default, the Terminal uses bash as its shell If you’d like to configure
it to use a different shell, you can do so by selecting Terminal→Prefer
ences and specifying the shell to use I talk about that in “What Is a
Shell?” (page 19), later in this chapter
Most OS X applications you’ve run to this point probably have a pretty graphical interface and allow you to move the cursor around with your mouse or trackpad Move it over something you want to do, and you can simply click for the action to take place The Terminal is different, though: your mouse gets a rest for a while as you type in the commands on your keyboard, ending each line with a Return
Syntax of a Unix Command
Unix command lines can be simple, one-word entries, such as the date command They
can also be more complex; you may need to type more than the command or program
name The command can be the name of a Unix program (such as date), or it can be a command that’s built into the shell (such as exit) You probably don’t need to worry
about this!
A Unix command can have arguments An argument can be an option or a filename
The general format for a Unix command line is:
command option(s) filename(s)
14 | Chapter 2: Using the Terminal
Trang 35There isn’t a single set of rules for writing Unix commands and arguments, but these general rules work in most cases:
• Enter commands in lowercase Unix is case-sensitive, so echo and ECHO are not
you’ll have to try it
Some commands also have options made from complete words or phrases and starting with two dashes, such as delete or confirm-delete When you enter a
command line, you can use this option style, the single-letter options (which each start with a single dash), or both
• The argument filename is the name of a file you want to use Most Unix programs
also accept multiple filenames, separated by spaces or specified with wildcards (see Chapter 4) If you don’t enter a filename correctly, you may get a response such as
“filename: no such file or directory” or “filename: cannot open.”
Some commands, such as who, have arguments that aren’t filenames.
• You must type spaces between commands, options, and filenames You’ll need to
“quote” filenames that contain spaces For more information, see Chapter 4
• Options come before filenames
• In a few cases, an option has another argument associated with it; type this special argument just after its option Most options don’t work this way, but you should know about them The sort command is an example of this feature: you can tell sort
to write the sorted text to a filename given after its -o option In the following example, sort reads the file sortme (given as an argument), and writes to the file sorted (given after the -o option):
$ sort -o sorted -n sortme
I also used the -n option in that example, but -n is a more standard option; it has nothing to do with the final argument (sortme) on that command line So, I also
could have written the command line this way:
$ sort -n -o sorted sortme
Don’t be too concerned about these special cases, though If a command needs an option like this, its documentation will say so
Launching the Terminal | 15
Trang 36• Command lines can have other special characters, some of which you’ll see later in this book They can also include several separate commands For instance, you can write two or more commands on the same command line, each separated by a semicolon (;) Commands entered this way are executed one after another by the shell.
OS X has a lot of commands! Don’t try to memorize all of them In fact, you’ll probably need to know just a few commands and their options As time goes on, you’ll learn these commands and the best way to use them for your job
Let’s look at a sample command The ls program displays a list of files You can use it
with or without options and arguments If you enter:
information about a particular file by using its name as the second argument For ex
ample, to find out about a file called chap1, enter:
$ ls -l chap1
Many Unix commands have more than one option For instance, ls has the -a (all) option
for listing hidden files You can use multiple options in either of these ways:
$ ls -a -l
$ ls -al
You must type one space between the command name and the dash that introduces the
options If you enter ls-al, the shell reports back with:
ls-al: command not found
Exercise: Entering a Few Commands
The best way to get used to the Terminal is to enter some commands To run a command, type the command and then press the Return key Remember that almost all Unix commands are typed in lowercase Try issuing the commands shown in Table 2-1 to see what results are produced in the Terminal
16 | Chapter 2: Using the Terminal
Trang 37Table 2-1 Sample Unix commands to test out
Get today’s date. date
List logged-in users. who
Obtain more information about users. who -u, finger, or w
Find out who is at your terminal. who am i
Enter two commands in the same line. who am i;date
Mistype a command. woh
In this session, you’ve tried several simple commands and seen the results on the screen
Types of Commands
When you use a program, you’ll want to know how to control it How can you tell it what job you want done? Do you give instructions before the program starts, or after it’s started? There are several general ways to run programs on an OS X system, and it’s good to be aware of them:
Graphical programs
Some programs work only within the graphical window environment On OS X,
you can run these programs using the open command For instance, when you type
open -a Chess at a command prompt, the Chess application (/Applications)
launches and opens one or more windows on your screen The program has its own way to receive your input—through menus and buttons on its windows, for instance Although you can’t interact with these programs using traditional Unix utilities, OS
X includes the osascript utility, which lets you run AppleScript commands from the
Unix shell
Noninteractive Unix programs
You can run many Unix programs (though we generally call them “commands” when they’re being typed in) directly at a shell prompt These programs work within
a specific command window and you control them from the Unix command line—that is, by typing options and arguments at a shell prompt before you start the program After you start the program, wait for it to finish; you generally don’t interact with it
Interactive Unix programs
Some Unix programs that work in the Terminal window have commands of their own (If you’d like some examples, see Chapters 3 and 4.) These programs may accept options and arguments on their command lines but, once you start a program, it prints its own prompt and/or menus, and it understands its own commands
It also takes instructions from your keyboard that weren’t given on its command line
Launching the Terminal | 17
Trang 38For instance, if you enter ftp at a shell prompt (refer back to the example in Chapter 1), you’ll see a new prompt from the ftp program At this prompt, you can enter
certain FTP commands for transferring files to and from remote systems When you
enter the special command quit to quit the ftp program (or you can use bye), ftp stops
prompting you for more input Once you quit FTP, you’re returned to the standard Unix shell prompt, where you can enter other Unix commands
Changing the Terminal’s Preferences
To change the Terminal’s preferences, go to Terminal→Preferences; this opens the complicated Preferences window, as shown in Figure 2-2
Figure 2-2 The Terminal Preferences window lets you configure the settings for your Terminal windows
At the top of the window, there’s a pop-up list that lets you select which options to configure: Startup, Settings, Window Groups, and Encodings The names suggest what each does, but let’s have a closer look anyway, particularly since some of these settings
definitely should be changed (in my view).
18 | Chapter 2: Using the Terminal
Trang 39When you first open the Terminal Preferences, the Startup settings are displayed, as shown in Figure 2-2 This lets you manage the overall behavior of a window, including its color scheme (my default is “Ocean”), what shell you’d like to use, and even what happens when you open a specific tab
What Is a Shell?
A shell, at least in the Unix world, is the environment in which you work on the Unix side
of things To put this into context, when you’re using the Aqua user interface for OS X, you’re using OS X’s native “environment.” With Unix, however, everything is text-based, and the shell offers you an interface in which to issue commands, and to configure how your shell environment works and behaves
Shells also offer their own scripting language, which allow you to write mini-programs for mundane things, such as displaying a message to tell you to clean the cat box, or much larger tasks, such as backing up your computer With shell scripts, you’re basically using the shell’s environment to run Unix commands—or other shell scripts—to automate tasks and processes
If you want to learn more about the bash shell and how to program shell scripts with it,
look to the venerable Learning the bash Shell by Cameron Newham and Bill Rosenblatt (O’Reilly) Don’t let the age of this book fool you; it’s packed with lots of useful information that still applies to using bash under OS X And if you want to see what you can do with
shell scripts, I’d recommend picking up a copy of Wicked Cool Shell Scripts (No Starch
Press), authored by yours truly and still a timely and popular scripting reference
The choice of shells in OS X are: /bin/bash, /bin/csh, /bin/ksh, /bin/tcsh, /bin/zsh, and /bin/sh Unix fans will no doubt find a shell to their liking, but if you’re just learning, stick with bash (/bin/bash) and you’ll be able to follow every example in this book without
a hiccup
Settings
The Settings pane (shown in Figure 2-3) shows lots of different appearance options, including nice visual thumbnails of the many different predefined color schemes available in the Terminal
The left side of the Settings window shows the different color profiles, but the right side
is where the action is It’s split into five sections: Text, Window, Shell, Keyboard, and Advanced
The text section is where you can specify what typeface you want to use: what size, what color, etc You can see all of the options in Figure 2-4
Launching the Terminal | 19
Trang 40Figure 2-3 Terminal Preferences Settings pane
If you use a predefined profile, of course, you don’t have to tweak any of the color settings, but I know that some people can spend hours fiddling and tweaking to get it just so.The most interesting section of the Settings pane of the Preferences window is the Window section, shown in Figure 2-3 Here, you can add useful information to the Terminal window, change the background of the Terminal window to a graphic or photo (though
I can’t imagine why you would!), and change the default window size The standard size
is 25 lines by 80 characters, but that’s just a historical artifact from the early neolithic era of computing Setting the size to 100 characters wide by 40 or 50 lines makes it considerably easier to work in the Terminal
One really nice thing that the Terminal does is save the textual information that scrolls off the top of the screen so you can scroll up and review what’s transpired earlier In the old days, once it was off the top, it was off, gone, kaput Now you can go back and review your command-line interaction from days or even weeks ago, depending on your available memory You can also configure the size of the scrollback buffer in the Window section; by default, it is unlimited
20 | Chapter 2: Using the Terminal