This article originally appeared in TidBITS on 2016-02-01 at 10:43 a.m.
The permanent URL for this article is:
Include images: Off

Do Cool Things on the Command Line with Five Quick Recipes

by Joe Kissell

My earliest experiences on the Internet (and, for that matter, my earliest experiences with Unix) were by way of a terminal emulator on a dial-up connection. Using nothing more than my keyboard and text commands, I was able to navigate servers, run remote programs, upload and download files, search for information, communicate with people around the world, and even make online purchases. It wasn’t pretty, but I found it satisfying, and it certainly earned me a few geek bragging rights.

Nowadays, command-line interfaces seem quaint if not downright backwards; we click, tap, swipe, and force-touch things to get stuff done. But OS X is a version of Unix, which means that you can still do things the old way if you want to — all it takes is a quick trip to the Terminal app (find it in /Applications/Utilities), which opens the world of the command line to any Mac user.

And the command line is worth knowing about, because even after all these years, there are plenty of activities a Mac user may want to perform that can’t be done in OS X’s graphical user interface. Sure, you can find third-party apps that expose some of these features to people who don’t want to use Terminal, but as I explain in “Take Control of the Mac Command Line with Terminal, Second Edition [1],” Terminal is nothing to be afraid of. If you can type words into a text editor, you can use Terminal. You just have to learn a few basic principles and memorize a handful of commands. Once you’ve done that, you’ll have much greater power to customize your Mac and solve unusual problems.

To give you a taste of the cool things you can do in Terminal, here are 5 of the 50 recipes in the newly released version 2.1 of the book, which is designed to help you become comfortable working on the command line, starting with fundamentals and learning more advanced topics as you go. These recipes are particularly easy — anyone can open Terminal and type (or copy and paste) these commands, even without knowing all the ins and outs of how things work behind the scenes. These quick techniques are among my favorites, and I hope you find them useful.

Two hints. First, enter each command as a single, long line, even if it wraps in your browser or email program. Second, after entering a command in Terminal, be sure to press Return or Enter to execute it.

Press the Power Button to Show the Shutdown Dialog -- Prior to 10.9 Mavericks, pressing the power button (or key) on your Mac for a second or so displayed a dialog with Restart, Sleep, Cancel, and Shut Down buttons. Starting in Mavericks, Apple changed the behavior so that pressing that button (or key) for a second or so puts your Mac to sleep, while pressing Control-Eject brings up the shutdown dialog.

To restore the old behavior of displaying the shutdown dialog by pressing the power button (or key), enter this:

defaults write PowerButtonSleepsSystem -bool FALSE

If you change your mind later, you can undo this command by repeating it with TRUE in place of FALSE.

Stop the Help Viewer from Floating -- This may be my favorite “defaults” recipe of all time. In recent versions of OS X, the Help window (which appears when you choose most commands from any application’s Help menu) floats above all other windows, no matter what you do. You can minimize it to the Dock to get it out of the way, but that makes it awkward for switching back and forth between the Help window and your app.

With this simple command, you can make the Help window act like any other window — it’ll appear in front initially, but you can click another window to bring that window in front of the Help window. Here’s the command:

defaults write DevMode -bool TRUE

To return the Help window to its irritating always-float behavior, repeat this command with FALSE instead of TRUE.

Figure Out Why You Can’t Unmount a Volume -- Have you ever tried to eject a CD, disk image, or network volume, only to see an error message saying the volume is in use? If so, the maddening part can be figuring out which process is using it so you can quit that process. So enter the following, substituting for VolumeName the name of the volume you can’t unmount:

lsof | grep /Volumes/VolumeName

This command shows you any processes you own that are currently using this volume; armed with this information, you can quit the program (using the kill command if necessary, which I discuss in the book). One frequent offender: the bash shell itself! If that’s the case, you’ll see something like this:

bash 14384 jk cwd DIR 45,8 330 2/Volumes/Data

If you’ve navigated to a directory on this volume in your shell, Mac OS X considers it “in use.” The solution in this case is to exit the shell, or simply cd to another directory.

If this command doesn’t tell you what you need to know, repeat it, preceded by sudo.

Reset the Launch Services Database -- Mac OS X’s Launch Services database keeps track of which programs are used to open which files, among other things. If you find that the wrong app opens when you double-click a file, or that icons don’t match up with the correct items, you may need to reset your Launch Services database. Do it like this (be sure to enter this command as a single, long line):

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Because this resets a lot of default mappings, your Mac may think you’re launching applications for the first time and ask if it’s OK. Agree to the alerts and you should be in good shape.

Disable Your Mac’s Startup Chime -- If you’re going to be turning on (or restarting) your Mac in a quiet environment where the startup chime would be distracting, you can turn it off with the following command:

sudo nvram SystemAudioVolume=%80

To re-enable it, you use a somewhat different command:

sudo nvram -d SystemAudioVolume

Take Control of the Mac Command Line with Terminal -- If you found any of these recipes helpful, note that there are 45 more in my freshly updated book [2]. But that’s not all. Even if you’re a complete command-line newbie, I take you through everything from the very basics up through fairly advanced procedures that will bring joy to any propeller-head. The book covers navigation, working with programs, customizing Terminal (and the command-line interface itself) to your liking, dealing with permissions, and a long list of other topics.

The command line is dead; long live the command line!