Access the Clipboard from the Command Line

Dec 9, 2009 - 6 Comments

terminal-icon-512x5122 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'

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


» 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

Subscribe to OSXDaily

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