Great Utilities: pbcopy

This is the first of what I hope will become a series of posts that highlight useful command-line computing utilities.

Sometimes (like this post), the focus will be on what I consider to be relatively obscure programs; at other time, I’ll focus on obscure features of commonly used utilities.

This post is dedicated to a pair of Mac OS X utilities: pbcopy and pbpaste. The “pb” is both stands for “pasteboard”: they are command-line programs to getting content to and from the Mac clipboard.

I find myself using pbcopy far more often than pbpaste, mostly because my typical use-case is to transfer content from a local text file to either a remote file or to a browser-based application like a wiki.

Use Case 1: Quick SSH key delivery

I create and destroy several virtual machines every month. I don’t have the time or inclination to integrate these disposable VMs into our larger configuration-management system, but I do want to get an SSH authorized_keys file in place for the root account.

On my system:

pbcopy < ~/.ssh/

While logged into the remote VM’s serial console, I use vi to open /root/.ssh/authorized_keys and the paste contents into it using Cmd-V.

Use Case 2: Add content to wiki

I often keep host-specific information in wiki pages partly just for general documentation purposes and partly for new-employee training. Let’s say that I want to document the layout of the /usr/local tree on a remote host. I could log into that system and try to copy the output from my terminal into the wiki’s editing window. Or, I could use pbcopy on my workstation:

ssh tree -F -L2 --charset=ascii /usr/local | pbcopy

Then all I need to do is Cmd+V my pasteboard into my browser!