You are on page 1of 10

10 essential OS X command-line tips for power

users
Believeitornot,theMacGUIcan'tdoeverythingoratleastnotasquickly
orpowerfullyasthesecommandlineutilities

By Paul Venezia Follow


InfoWorld | Feb 3, 2016

For most casual users, the OS X command line, accessed via the Terminal app, is at least as murky and daunting as the
Windows Command Prompt, to be used only in times of extreme distress. For those users, this is usually when something
has gone inexplicably wrong, and typing cryptic commands into the prompt seems the only hope for a cure. Of course,
most likely theyre actually restarting a launchdservice or deleting a plistfile.

To those familiar with the Unix shell, the command line or terminal is a powerful tool to be used to facilitate many system
functions and interactions. Because OS X is built around a BSD core, you can bring over your fancy one-liners and skip the
cumbersome GUI tools to do simple things like walking a directory tree, deleting every file older than 30 days, or pulling a
list of files in the current directory that contain a specific text string. Although graphical interfaces can simplify many tasks,
they can also complicate other tasks -- and the command line comes to the rescue.

[ Also on InfoWorld: Top 20 OS X command-line secrets for power users | Windows 10 vs. OS X El Capitan: Which gives sys
admins the most management control? | Cut to the key news in technology trends and IT breakthroughs with the
InfoWorld Daily newsletter, our summary of the top tech happenings. ]

OS X has hidden gems that even power users might not know about. Heres a list of 10 handy utilities that allow you to
perform many functions on your Mac from the command line. You should find all of them useful and, in at least one case,
even entertaining.

1. pbcopyand pbpaste: Copy and paste to/from the clipboard

The pbcopyand pbtasteutilities work in concert, allowing access to and from the system clipboards/pasteboards from the
command line. For instance, if you wanted to list all of the files in a directory that start with the letter "f" and put that list
into the clipboard, youd type the following:

$ ls f* | pbcopy

Boom -- that output can then be pasted into any GUI app.

The converse works as you might expect. If you have that list of files in the clipboard from another app, you can process it
on the command line with pbpaste:

$ pbpaste | grep foo

It will use the grepcommand to extract only the lines containing the string foo.

If your work takes you into the command line in concert with GUI apps, these two commands can definitely come in handy.

2. rsync: synchronize files or directories

The rsyncutility can synchronize directory trees between folders on the same system or between folders on a local and a
remote system. Its immensely useful and has been a bastion of IT for many years. Its also included in OS X.

If you have a need to keep two directory trees identical, using rsyncon the local system is trivial:

$ rsync -av /path/to/directory1/ /path/to/directory/2/


This will make sure that any and all files in /path/to/directory1/ also exist in /path/to/directory2/. If you want to make the
directories exactly identical, youll need to instruct rsyncto also delete files in /path/to/directory2/ that do not exist in
/path/to/directory1/:

$ rsync -av --delete /path/to/directory1/ /path/to/directory2/

If you dont want the files listed during synchronization, remove the vflag:

$ rsync -a --delete /path/to/directory1/ /path/to/directory2/

Or if you want to see which files would be copied or deleted, add an n:

$ rsync -avn --delete /path/to/directory1/ /path/to/directory2/

You can also use rsyncbetween different systems, as long as the remote system has rsyncinstalled and is running SSH:

$ rsync -av --delete /path/to/directory1/ user@remotesystem:/path/to/directory1/

Note that the trailing slash is important here. It indicates that rsyncis to read files within the source directory and
synchronize them within the destination directory. Omit the trailing slash, and rsyncwill copy (append) the source directory
to the destination directory, creating an additional directory level that you might not have intended.

To enable SSH access on a Mac, open System Preferences, go to Sharing, and select Remote Login. You will then be able to
rsyncto the Mac over SSH, or use SSH to connect to a shell on the system.

3. ditto: Copy or merge directories or archives

The dittocommand is superficially similar to rsync, but in reality its a very different tool. Its been included in OS X for
quite some time, but remains relatively unknown.

Like rsync, dittocan be used to copy directory trees, preserving permissions, ownership, and metadata. Thus:

$ ditto /path/to/source /path/to/destination


If the destination directory doesnt exist, dittowill make an exact copy of the source directory there. If the destination
directory does exist, dittowill merge the source directory with the destination, overwriting duplicate filenames. For
instance, you could use dittoto merge the contents of two large directories of pictures into a single nested directory
structure.

But dittogoes further, as it can also create, extract, and manipulate CPIO (Copy In, Copy Out) and Zip archives. You can
specify a bill of materials (bom) document that dittowill use to selectively copy or merge, have dittoomit metadata
during the file copies, or even instruct dittoto reduce universal binaries to one specific architecture during an operation.

The dittoutility is a fairly complex tool that can be very handy when used properly, but it can also take some
experimentation to fully understand.

4. tmutil: Back up and restore with Time Machine

Apples Time Machine feature is extremely useful in providing a way for users to maintain ongoing backups of their
computers to an external drive such as a NAS or USB drive. That said, the Star Wars interface can be cumbersome when
power users are trying to navigate backups, and the controls in System Preferences are quite Spartan.

Luckily, tmutilis there to fill in the gaps when you need it.

For instance, whereas the Time Machine GUI will show you the latest backup, if you want to show all available backups, run
the following:

$ tmutil listbackups

Youll see a list of every accessible backup of the current system. To view the latest backup, simply enter the following:

$ tmutil latestbackup

You can also use tmutilto start and stop backups, compare backups to one another, analyze the amount of change
between backups, inherit backups that might have been made from an older system, show information about backup
destinations, associate and disassociate backup destinations, and even restore files from a backup.
Basically, all of the backup-related tasks that a power user is missing in the GUI is in tmutil. If you are in dire straits and
need to dig deeper into backups to fix something, it can be a lifesaver.

5. fs_usage: Display file system activity

If youve ever been in a position where your disk is thrashing and you want a quick command-line look at which system
processes are causing the turmoil, fs_usagehas your back. This tool provides a constant stream of real-time information on
which processes are accessing the file system.

By default, fs_usageexempts a few processes from the output, including Terminal and Secure Shell (sshd). You can run
fs_usagein Terminal like so:

$ sudo fs_usage

If youre using another terminal application, youll need to exempt it from the output with the -eswitch:

$ sudo fs_usage -e iTerm

The above will exempt both fs_usageand the iTerm app from the output.

In addition to providing a systemwide view, fs_usagecan profile individual processes, such as Google Chrome:

$ sudo fs_usage "Google Chrome"

6.drutiland hdiutil: Burn CDs and DVDs and manipulate disk images

If youve ever wanted to burn a data DVD or audio CD quickly and easily, drutilis for you. With it, you can burn a directory
tree to a CD with a single line:

$ drutil burn /path/to/folder

If you want to burn an audio CD, simply reference a directory full of audio files:

$ drutil burn -audio /path/to/folder


This utility can also come in handy for erasing CD-RW media with the erasecommand (drutil erase /path/to/folder).
With the bulkerasecommand, it will erase a CD-RW disc, eject it, and wait for another to be inserted, then rinse and repeat.

The hdiutilutility is somewhat related, in that hdiutilis used to manipulate disk images. You can use hdiutilto create an
Apple disk image (that is, a DMG file) from a directory path:

$ hdiutil create -srcfolder /path/to/files/ myfiles.dmg

In El Capitan, you can burn ISO images to CDs with the following command:

$ hdiutil burn /path/to/file.iso

The hdiutilutility has many other functions as well, such as mounting and unmounting images, converting image formats,
creating encrypted images, and verifying images.

7. system_profiler: Report system information

When debugging problems or investigating a system, its handy to be able to get a report on all of the pertinent information
about the hardware and software in use. Thats what system_profilerdoes, and it outputs that report to a text file for easy
reading.

For most purposes, the basic report is sufficient:

$ system_profiler -detailLevel basic > report.txt

This will give you tons of data on the system, from basics like CPU, RAM, graphics, and storage to serial number, hardware
UUID, network information, RAM slot population, network particulars, power info, printer software, USB, Thunderbolt, and
Time Machine backup information.

Its a one-stop shop for all the data you might need on a particular Mac. This is especially handy when trying to
troubleshoot problems with a remote system you cant access, such as when Mom or Dad call with an inexplicable
problem.
8. tar, gzip, bzip2, and zip: Create and open compressed archives

In the Unix world, tar(short for tape archive) was originally used to copy files to backup tapes in a standardized format.

Today, we no longer use tarin quite the same way. We use it to create archives of individual files or directories. Employed
alongside compression tools gzipand bzip2, tarlets us create compressed archives of files. The result is similar to a Zip file
archive, which is used on Mac, Windows, and other platforms.

To create a gzipped tararchive of a directory, we might run:

$ tar zcpf myfiles.tgz /path/to/files

This will create myfile.tgz, which is a gzipped tar archive of all of the files in the referenced path. If we want to use bzip2,
we might get a smaller archive, but it might take longer to compress and decompress:

$ tar jcpf myfiles.tbz /path/to/files

And we can always use regular Zip:

$ zip r myfiles.zip /path/to/files

To open a gzipped tar file, we run this command:

$ tar zxf myfiles.tgz

To open a bzipped (bzip2) archive, the command is the following:

$ tar jxf myfiles.tbz

And for Zip archives, the command is unzip:

$ unzip myfiles.zip
You might get better mileage out of tarand gzipor bzip2than zipfor some file types, but be warned that Windows users
wont be able to open the archives without specific software, whereas Zip files will open automatically on modern Windows
versions.

9. mdfind: Perform powerful Spotlight searches

OS X has had Spotlight search for years. Spotlight indexes files on your disk and allows for advanced searching by metadata,
file type, file contents, and more. Fortunately, Spotlight searches are also available on the command line via mdfind.

This works exactly like the Spotlight tool in the Finder, but its more flexible in search types, and it returns all of the data
found. For instance, the following command will return literally everything indexed by Spotlight containing the keyword
foobar:

$ mdfind "foobar"

You can search all metadata too, such as file type:

$ mdfind "kMDItemContentType == 'com.microsoft.word.doc'"

You can search by file type with keywords:

$ mdfind "kind:pdf Bread cheese salami"

You can even search based on time frame:

$ mdfind -onlyin ./tmp/ 'kMDItemFSContentChangeDate >= $time.today(-2)'

The Spotlight search GUI is certainly handy for simple searches, but if youre really trying to scour your storage for files,
mdfindmight be a better bet.

10. say: Have your Mac read a file to you


The saycommand can be useful for those who need audio assistance due to disability, but it can also be a lot of fun. This
tool does what you might expect: It translates text to speech. At its most basic, its very simple to use:

$ say "Hello world"

Youll get a stereotypical robot voice saying Hello world. However, it doesnt stop there. There are 64 different voices to
choose from, in a variety of languages. In some of the foreign voices, English text will be uttered in an approximation of a
speaker of that languages English accent. You can see a list of all of the voices with this command:

$ say -v ?
Sign In | Register
Once youve decided on a suitable voice, you can have say, well, say anything on the command line or in a normal text file.
Include the --interactiveflag, and saywill highlight the words as they are read aloud:

$ say -v Vicki -f myfile.txt --interactive

You can even set the rate at which text is read back, and if the destination system is properly configured, you can have say
read back text on a remote system.

The Macs GUI makes most things easy, and its a pleasant place to spend your time. But theres more to the Mac than the
pretty face. When the GUI seems too limited or too slow, pop open the Terminal and tap the power of the command line. In
addition to these 10 essentials, check out the 20 OS X command-line secrets in InfoWorlds previous article.

Paul Venezia Columnist


Paul Venezia is a veteran enterprise architect and senior contributing editor at InfoWorld, where he writes
analysis, reviews and The Deep End blog.

View Comments
View Comments

YOUMIGHTLIKE

Copyright 1994 - 2016 InfoWorld, Inc. All rights reserved.

You might also like