10 essential OS X command-line tips for power users

Believe it or not, the Mac GUI can't do everything -- or at least not as quickly or powerfully as these command-line utilities

10 essential OS X command-line tips for power users
Credit: Thinkstock

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 they’re actually restarting a launchd service or deleting a plist file.

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.

OS X has hidden gems that even power users might not know about. Here’s 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. pbcopy and pbpaste: Copy and paste to/from the clipboard

The pbcopy and pbtaste utilities 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, you’d 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 grep command 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 rsync utility can synchronize directory trees between folders on the same system or between folders on a local and a remote system. It’s immensely useful and has been a bastion of IT for many years. It’s also included in OS X.

If you have a need to keep two directory trees identical, using rsync on 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, you’ll need to instruct rsync to 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 don’t want the files listed during synchronization, remove the v flag:

$ 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 rsync between different systems, as long as the remote system has rsync installed 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 rsync is to read files within the source directory and synchronize them within the destination directory. Omit the trailing slash, and rsync will 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 rsync to the Mac over SSH, or use SSH to connect to a shell on the system.

3. ditto: Copy or merge directories or archives

The ditto command is superficially similar to rsync, but in reality it’s a very different tool. It’s been included in OS X for quite some time, but remains relatively unknown.

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

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

If the destination directory doesn’t exist, ditto will make an exact copy of the source directory there. If the destination directory does exist, ditto will merge the source directory with the destination, overwriting duplicate filenames. For instance, you could use ditto to merge the contents of two large directories of pictures into a single nested directory structure.

But ditto goes 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 ditto will use to selectively copy or merge, have ditto omit metadata during the file copies, or even instruct ditto to reduce universal binaries to one specific architecture during an operation.

The ditto utility 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

Apple’s 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, tmutil is 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

You’ll 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 tmutil to 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 you’ve 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_usage has your back. This tool provides a constant stream of real-time information on which processes are accessing the file system.

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

$ sudo fs_usage

If you’re using another terminal application, you’ll need to exempt it from the output with the -e switch:

$ sudo fs_usage -e iTerm

The above will exempt both fs_usage and the iTerm app from the output.

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

$ sudo fs_usage "Google Chrome"

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

If you’ve ever wanted to burn a data DVD or audio CD quickly and easily, drutil is 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 erase command (drutil erase /path/to/folder). With the bulkerase command, it will erase a CD-RW disc, eject it, and wait for another to be inserted, then rinse and repeat.

The hdiutil utility is somewhat related, in that hdiutil is used to manipulate disk images. You can use hdiutil to 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 hdiutil utility 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, it’s handy to be able to get a report on all of the pertinent information about the hardware and software in use. That’s what system_profiler does, 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.

It’s 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 can’t 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 tar in quite the same way. We use it to create archives of individual files or directories. Employed alongside compression tools gzip and bzip2, tar lets 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 tar archive 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 tar and gzip or bzip2 than zip for some file types, but be warned that Windows users won’t 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 it’s 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 you’re really trying to scour your storage for files, mdfind might be a better bet.

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

The say command 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, it’s very simple to use:

$ say "Hello world"

You’ll get a stereotypical robot voice saying “Hello world.” However, it doesn’t 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 language’s English accent. You can see a list of all of the voices with this command:

$ say -v ‘?’

Once you’ve decided on a suitable voice, you can have say, well, say anything on the command line or in a normal text file. Include the --interactive flag, and say will 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 Mac’s GUI makes most things easy, and it’s a pleasant place to spend your time. But there’s 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 InfoWorld’s previous article.

This story, "10 essential OS X command-line tips for power users" was originally published by InfoWorld.

Must read: Hidden Cause of Slow Internet and how to fix it
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies