Access the Clipboard from the Command Line
With the pbcopy and pbpaste commands, you can access your Mac OS X clipboard directly through the Terminal. Yes, that means you can access what you copied in a GUI app and use it in the command line seamlessly, and vice versa. Here’s an introduction on how to use both copy and paste from the command line:
pbcopy – as you might imagine, pbcopy is how you can copy things from the command line. The best way to use it is by piping something to it, for example:
ls -lha |pbcopy
This will pipe the results of ls -lha into your clipboard, which you can now access using the pbpaste command.
pbpaste – pbpaste is how you dump the contents of the clipboard. If you just want to see what’s in the clipboard, simply type
pbpaste and you’ll see whatever is stored. If you used the above ls -lha|pbcopy command youll see the results of that now. You can also easily store the contents of the clipboard into a file by using pbpaste, as follows:
pbpaste > clipboard.txt
Now you’ll have the document clipboard.txt with the contents of your clipboard. You can double-check this by opening it in any text editor, or by typing
cat clipboard.txt to see the contents.
pbcopy and pbpaste are a lot more powerful than the above examples though. Here’s how to use pbpaste to send the contents of your clipboard to another machine, by piping the output through an ssh connection into a file named myclipboard.txt on the remote machine:
pbpaste | ssh username@host 'cat > ~/myclipboard.txt'