Introduction - Take Control of the Mac Command Line with Terminal (2015)

Take Control of the Mac Command Line with Terminal (2015)

Introduction

Back when I began using computers, in the early 1980s, user interfaces were pretty primitive. A computer usually came with only a keyboard for input—mice were a novelty that hadn’t caught on yet. To get your computer to do something, you typed a command, waited for some result, and then typed another command. There simply was no concept of pointing and clicking to make things happen.

When I finally switched from DOS to the Mac (without ever going through a Windows phase, I should mention!), I was thrilled that I could do my work without having to memorize lists of commands, consult manuals constantly, or guess at how to accomplish something. Everything was right there on the screen, just a click away. It was simpler—not in the sense of being less powerful, but in the sense of requiring less effort to access the same amount of power. Like most everyone else, I fell instantly in love with graphical interfaces.

Fast forward a couple of decades, and I find myself faced with some mundane task, such as deleting a file that refuses to disappear from the Trash or changing an obscure system preference. After wasting time puzzling over how to accomplish my task—and perhaps doing some Web searches—I discover that Mac OS X’s graphical interface does not, in fact, offer any built-in way to do what I want. So I have to hunt on the Internet for an application that seems to do what I want, download it, install it, and run it (and perhaps pay for it, too), all so that I can accomplish a task with my mouse that would have taken me 5 seconds in DOS 30 years ago.

That’s not simple.

I’m a Mac user because I don’t have time to waste. I don’t want my computer to put barriers between me and my work. I want easier ways to do things instead of harder ways. Ironically, Mac OS X’s beautiful graphical interface, with all its menus, icons, and buttons, doesn’t always provide the easiest way to do something, and in some cases it doesn’t even provide a hard way. The cost of elegance and simplicity is sometimes a lack of flexibility.

Luckily, Mac OS X isn’t restricted to the graphical realm of windows and icons. It has another whole interface that lets you accomplish many tasks that would otherwise be difficult, or even impossible. This other way of using Mac OS X looks strikingly like those DOS screens from the 1980s: it’s a command-line interface, in which input is done with the keyboard, and the output is sent to the screen in plain text.

The usual way of getting to this alternative interface (though there are others) is to use a program called Terminal, located in the Utilities folder inside your Applications folder. It’s a simple program that doesn’t appear to do much at first glance—it displays a window with a little bit of text in it. But Terminal is in fact the gateway to vast power.

If you read TidBITS, Take Control books, Macworld, or any of the numerous other Mac publications, you’ve undoubtedly seen tips from time to time that begin, “Open Terminal and type in the following…”. Many Mac users find that sort of thing intimidating. What do I click on? How do I find my way around? How do I stop something I’ve started? Without the visual cues of a graphical interface, lots of people get stuck staring at that blank window.

If you’re one of those people, this book is for you. It’s also for people who know a little bit about the command line but don’t fully understand what they can do, how to get around, and how to stay out of trouble. By the time you’re finished reading this book and trying out the examples I give, you should be comfortable interacting with your Mac by way of the command line, ready to confidently use Terminal whenever the need arises.

It’s not scary. It’s not hard. It’s just different. And don’t worry—I’ll be with you every step of the way!

Much of this book is concerned with teaching you the skills and basic commands you must know in order to accomplish genuinely useful things later on. If you feel that it’s a bit boring or irrelevant to learn how to list files or change directories, remember: it’s all about the end result. You learn the fundamentals of baking not because measuring flour or preheating an oven is intrinsically interesting, but because you need to know how to do those things in order to end up with cookies. And let me tell you, the cookies make it all worthwhile!

Speaking of food—my all-purpose metaphor—this book doesn’t only provide information on individual ingredients and techniques. The last chapter is full of terrific, simple command-line recipes that put all this power to good use while giving you a taste of some advanced capabilities I don’t explore in detail. Among other things, you’ll learn:

· How to figure out what’s preventing a disk from disconnecting (unmounting or ejecting)

· How to tell which applications are currently accessing the Internet

· How to rename lots of files at once, even if you’re not running Yosemite

· How to change a number of hidden preferences

· How to understand and change file permissions

· How to automate command-line activities with scripts

Astute readers may note that some of these tasks can be accomplished with third-party utilities. That’s true, but the command line is infinitely more flexible—and Terminal is free!

I should be clear, however, that this book won’t turn you into a command-line expert. I would need thousands of pages to describe everything you can accomplish with the command line. Instead, my goal is to cover the basics and get you up to a moderate level of familiarity and competence. And, based on feedback from the first edition of this book, I’ve expanded the scope of this revised second edition to include a number of topics that are a bit more advanced.

Most of my examples work with any version of Mac OS X from 10.6 Snow Leopard on, although many also apply to earlier versions of Mac OS X. A few techniques require 10.9 Mavericks or 10.10 Yosemite; I point out those out as we go along.