pbcopy & pbpaste: Manipulating the Clipboard from the Command Line
Copy and Paste are absolute necessities for virtually all computer users, and if you find yourself working in the command line frequently, you’ll want to know how to manipulate the clipboard of OS X directly from the terminal prompt. That’s what the Mac commands pbcopy and pbpaste are for, and as you may have guessed the two commmands do exactly what they sound like, pbcopy is to copy, and pbpaste is to paste through the command line. They’re actually quite powerful and you’ll be sure to find them useful the next time you’re hanging out with your bash, tcsh, zsh, or whatever is your preferred shell prompt.
We will cover a quick explanation on how to use pbcopy and pbpaste to manipulate clipboard data, with some examples indicating how to redirect the output of terminal commands as input into the clipboard, and of course, how to expel the contents of the clipboard into the command line with pbpaste.
pbcopy: takes standard input and places it in the clipboard buffer
pbcopy is simple enough to use, basically direct something into it, and it’ll copy it into the clipboard buffer. This can then be accessed either through pbpaste, or the standard Finder’s paste command (command-v). Here’s how to use it:
$ pbcopy < file.txt
That's it, now the contents of file.txt will be in your clipboard, ready to paste where ever. But pbcopy is much more powerful than that, and you can direct the output of commands and programs to be copied as well. Here's an example with the command 'ps':
$ ps aux | pbcopy
This pipes the results of the ps command into the clipboard, once again it can be pasted anywhere. If you want to filter your results a bit, you can throw do that too. An example using the grep command is:
$ ps aux | grep root | pbcopy
This pipes the results of the 'ps aux' command, but filters specifically for 'root', and copies only those results into the clipboard. Cool huh?
pbpaste: takes data from the clipboard buffer and writes it to the standard output
pbpaste is equally simple to use, it will retrieve whatever you have placed in the clipboard buffer and spit it out. In it's most simple form, simply type:
This will print whatever data you copied from the pbcopy command or the Finder's copy command (command-c). You can easily route this output into a file via the command line to access later if you want though, with this simple command:
$ pbpaste > pastetest.txt
Filtering what is pasted is very useful though, and the command structure is much like what we saw earlier with pbcopy. We'll filter for 'rcp' but of course you can filter for anything you want
$ pbpaste | grep rcp
What you'll see pasted is only what matches your search for 'rcp' inside the data in the clipboard.
There's much more use to be had with pbcopy and pbpaste, but that should give you a general idea of how to use it and perhaps give you some ideas to implement them in your own productivity. The screenshot below simply demonstrates that you can copy and paste from the command line into a Finder app (in this case, TextEdit) without losing any formatting.
Users who are interested in learning more powerful uses of pbcopy and pbpaste to modify the pasteboard of OS X through the command line are encouraged to review the commands man pages, which are basically interchangeable and cover both sides of the command. Accessing that is easily achieved through 'man pbcopy' or 'man pbpaste' where you will see some more details of the command usage, as well as other options for how to use the commands, adjust encoding, strip or maintain rich text details, and so much more.