The OS X Terminal opens up a world of powerful UNIX utilities and scripts. If you’re migrating from Linux, you’ll find many familiar commands work the way you expect. But power users often aren’t aware that OS X comes with a number of its own text-based utilities not found on any other operating system.
Learning about these Mac-only programs can make you more productive on the command line and help you bridge the gap between UNIX and your Mac.
This post originally appeared on Mitchell Cohen’s Blog, Mitchchn.me.
open opens files, directories and applications. Exciting, right? But it really does come in handy as a command-line double-click. For instance, typing:
$ open /Applications/Safari.app/
…will launch Safari as if you had double-clicked its icon in the Finder. Recall that OS X apps are not true executables, but actually special directories (bundles) with the extension .app.
open is the only way to launch these programs from the command line. It can also launch other files that are truly bundles, such as Pages documents.)
If you point
open at a file instead, it will try to load the file with its associated GUI application.
open screenshot.png on an image will open that image in Preview. You can set the
-a flag to choose the app yourself, or
-e to open the file for editing in TextEdit.
open on a directory will take you straight to that directory in a Finder window. This is especially useful for bringing up the current directory by typing
Remember that the integration between Finder and Terminal goes both ways – if you drag a file from Finder into a Terminal window, its full path gets pasted into the command line.
2. Pbcopy and Pbpaste
These two commands let you copy and paste text from the command line. Of course, you could also just use your mouse—but the real power of
pbpaste comes from the fact that they’re UNIX commands, and that means they benefit from piping, redirection, and the ability to be in scripts in conjunction with other commands. Typing:
$ ls ~ | pbcopy
…will copy a list of files in your home directory to the OS X clipboard. You can easily capture the contents of a file:
$ pbcopy < blogpost.txt
..or do something crazier. This hacked-up script will grab the link of the latest Google doodle and copy it to your clipboard.
<code>$ curl <a href="<a href=" http:="" http://www.google.com="" doodles#oodles="" archive"="">http://ift.tt/1lL4zLu>">http://ift.tt/1wm4nVb;>http://ift.tt/1lL4zLu> | grep -A5 'latest-doodle on' | grep 'img src' | sed s/.*'
pbcopy with pipes is a great way to capture the output of a command without having to scroll up and carefully select it. This makes it easy to share diagnostic information.
pbpaste can also be used to automate or speed up certain kinds of tasks. For instance, if you want to save email subject lines to a task list, you could copy the subjects from Mail.app and run:
$ pbpaste >> tasklist.txt
Many a Linux power user has tried to use
locate to search for files on a Mac and then quickly discovered that it didn’t work. There’s always the venerable UNIX
find command, but OS X comes with its own killer search tool: Spotlight. So why not tap into its power from the command line?
That’s exactly what
mdfind does. Anything Spotlight can find,
mdfind can find too. That includes the ability to search inside files and metadata.
mdfind comes with a few conveniences that make it stand out from its big blue brother. For instance, the
-onlyin flag can restrict the search to a single directory:
$ mdfind -onlyin ~/Documents essay
mdfind database should stay up to date in the background, but you can also troubleshoot it (as well as Spotlight) using
mdutil. If Spotlight isn’t working the way it should,
mdutil -E will erase the index and rebuild it from scratch. You can also turn off indexing entirely with
mdutil -i off.
screencapture lets you take many different kinds of screenshots. It’s similar to Grab.app and the keyboard shortcuts cmd + shift + 3 and cmd + shift + 4, except it’s far more flexible. Here are just a few different ways you can use
Capture the contents of the screen, including the cursor, and attach the resulting image (named ‘image.png’) to a new Mail message:
$ screencapture -C -M image.png
Select a window using your mouse, then capture its contents without the window’s drop shadow and copy the image to the clipboard:
$ screencapture -c -W
Capture the screen after a delay of 10 seconds and then open the new image in Preview:
$ screencapture -T 10 -P image.png
Select a portion of the screen with your mouse, capture its contents, and save the image as a pdf:
$ screencapture -s -t pdf image.pdf
To see more options, type
launchctl lets you interact with the OS X init script system,
launchd. With launch daemons and launch agents, you can control the services that start up when you boot your computer. You can even set up scripts to run periodically or at timed intervals in the background, similar to cron jobs on Linux.
For example, if you’d like to have the Apache web server start automatically when you turn on your Mac, simply type:
$ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
launchctl list will show you what launch scripts are currently loaded.
sudo launchctl unload [path/to/script] will stop and unload running scripts, and adding the
-w flag will remove those scripts permanently from your boot sequence. I like to run this one on all the auto-update "helpers" created by Adobe apps and Microsoft Office.
Launchd scripts are stored in the following locations:
To see what goes into a launch agent or daemon, there’s a great blog post by Paul Annesley that walks you through the file format. And if you’d like to learn how to write your own
launchd scripts, Apple provides some helpful documentation on their Developer site. There’s also the fantastic Lingon app if you’d prefer to avoid the command line entirely.
$ say "Never trust a computer you can't lift."
You can also use
say to speak the contents of a text file with the
-f flag, and you can store the resulting audio clip with the
$ say -f mynovel.txt -o myaudiobook.aiff
say command can be useful in place of console logging or alert sounds in scripts. For instance, you can set up an Automator or Hazel script to do batch file processing and then announce the task’s completion with
But the most enjoyable use for
say is rather more sinister: if you have
ssh access to a friend or coworker’s Mac, you can silently log into their machine and haunt them through the command line. Give ’em a Siri-ous surprise.
You can set the voice (and language!) used by
say by changing the default setting in the Dictation & Speech panel in System Preferences.
diskutil is a command line interface to the Disk Utility app that comes with OS X. It can do everything its graphical cousin can, but it also has some extra capabilities—such as filling a disk with zeroes or random data. Simply type
diskutil list to see the path names of disks and removable media attached to your machine, and then point the command at the volume you want to operate on. Be careful:
diskutil can permanently destroy data if it’s used incorrectly.
Alright–this isn’t technically a native command. But no OS X power user should be without Homebrew. The website calls it "The missing package manager for OS X," and that couldn’t be truer. If you’ve ever used
apt-get in Linux, you will feel right at home in Homebrew. (In truth, Homebrew is more similar to FreeBSD’s Ports system than Linux’s apt. It uses a hybrid source/binary system: if no binary is available for a particular package it will simply download the source tarball and compile it—not a problem on today’s multicore Macs.)
brew gives you easy access to thousands of free utilities and libraries from the open source community. For instance,
brew install imagemagick will set you up with ImageMagick, a powerful utility that makes it possible to do anything from whipping up animated gifs to converting images between dozens of different types.
You can have fun with Homebrew too:
brew install archey will get you Archey, a cool little script for displaying your Mac’s specs next to a colorful Apple logo. The selection in Homebrew is huge—and because it’s so easy to create formulas, new packages are being added all the time.
Archey—My command line brings all the boys to the yard.
But the best part about Homebrew? It keeps all its files in a single directory:
/usr/local/. That means you can install newer versions of system software, such as
mysql, without interfering with the built-in equivalents. And if you ever want to get rid of your Homebrew installation, it’s easy to remove using the uninstall script.
For more fun with Terminal.app, here is an A-Z list of all available console commands in OS X 10.9 Mavericks.
Thanks to reader feedback, I’ve written about a few more commands in a follow-up post: (And eight hundred more).
Mitchell Cohen is a writer and technophile from Toronto. He writes about coffee, code, journalism, language, insomnia and giant spiders over at his blog, Mitchchn.me. You can follow him on Twitter at @mitchchn.
Want to see your work on Lifehacker? Email Andy.