O'Reilly logo

Learning Unix for OS X, 2nd Edition by Dave Taylor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 dramatically. Heck, many of you reading this have never known a non–OS X Mac interface! There were a lot of interface changes, but the biggest update when Apple switched operating systems was that every machine gained multitasking and multiuser 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 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 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 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! Fortunately, 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.

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 still stymied by left-clicking versus right-clicking your mouse, 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 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!

A 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 Mac OS X in 1999 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, 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 on the command line, 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 many different 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 worry too much about what’s from which 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 Macintosh has a command-line interface. If you know how to use the command line, you’ll always be a power user.

  • As 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 within 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 El Capitan: 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 El Capitan.

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 commands, 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.

Chapter 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 a couple 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 books, websites, and similar resources to investigate.

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).

Italic

Indicates new terms, URLs, email addresses, pathnames, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant 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 FileOpen, EditCopy, etc. Arrows are also used to signify a navigation path when using window options; for example, System PreferencesScreen EffectsActivation means you would launch System Preferences, click on the icon for the Screen Effects preferences panel, and select the Activation pane within that panel.

Pathnames

Pathnames are used to show the location of a file or application in the filesystem. Directories (or folders for Mac and Windows users) are separated by forward slashes. For example, if you see something like “launch the Terminal application (/Applications/Utilities)” in the text, that means the Terminal application can be found in the Utilities subfolder of the Applications folder.

A carriage return (↲) at the end of a line of code is used to denote an unnatural line break; that is, you should not enter these as two lines of code, but as one continuous line. Multiple lines are used in these cases due to printing constraints.

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 (FileOpen), 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.

Figure 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.

Tip

This icon signifies a tip, suggestion, or general note.

Warning

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 2016 Dave Taylor, 978-1-4919-3998-7.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

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
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/learn-unix-osx-2.

To comment or ask technical questions about this book, send email to .

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 original O’Reilly title Learning the Unix Operating System, by Jerry Peek, Grace Todino, and John Strang. 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 El Capitan edition is the sixth 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. Thanks 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, and a special “ta, mate!” to Dave Kitabjian for helping with the tech edit process, too.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required