Accessing the OS X Clipboard from the Command Line

Dec 9, 2009 - 6 Comments

terminal-icon-512x5122 With the pbcopy and pbpaste commands, you can use the command line to manipulate clipboard contents but also 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. We’ve shown a brief introduction on how to use both pbcopy and pbpaste from the command line before, but we wanted to demonstrate a few additional methods to use these incredibly useful tools on the Mac, like how to directly access whatever is currently stored in the Macs clipboard from the terminal prompt.

Accessing the OS X Clipboard Contents with pbpaste

pbpaste – pbpaste is how you dump the currently active contents of the clipboard. If you just want to see what’s in the clipboard, simply type this:

pbpaste

You’ll see whatever is stored in the clipboard right now, as if you hit Command+V in OS X.

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.

Adding Contents to the Clipboard with pbcopy

pbcopy – as you might imagine, pbcopy is how you can copy things from the command line. This is basically like using Comamnd+C in the Finder or GUI of OS X. The best way to use it is by piping something into pbcopy, 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.

Now that you’ve copied something to the clipboard of OS X with pbcopy you can dump the output back into the Terminal by using pbpaste, if you had just run the ls -lha|pbcopy command, the output will be that.

You can learn more about modifying the clipboard from the terminal by redirecting command output here with pipes and redirects to the pbcopy command.

pbcopy and pbpaste can even work across networks by using ssh or other protocols, check this out:

Pasting Clipboard Contents Across Networks with SSH & pbpaste

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'

Nice huh?

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Bill Ellis in Command Line, How to, Tips & Tricks

6 Comments

» Comments RSS Feed

  1. Moschos says:

    Really cool, didn’t know it existed on my MACs terminal. Great !!

  2. warmz says:

    [Quote] Nice huh? [/Quote]

    Beautiful .. terminal heaven!

    :)

  3. […] Access the clipboard from the command line – OS X Daily (tags: osx howto cli) […]

  4. sudopeople says:

    FYI: this works on iPhones as well :)
    I’m not sure if one of the Cydia packages I have installed is required, but chances are if you’re trying to do this on on your iPhone, you’re jailbroken and have adv-cmds and things like that installed already. Happy hacking.

  5. […] This uses the pbcopy and pbpaste commands, you may recognize them as the command line front to the Mac OS X clipboard. […]

  6. […] customizable, right down to the menubar icon itself. It even works fine with the command line pbcopy and pbpaste tools. If there is a better clipboard manager for Mac OS X, I haven’t seen […]

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates