pbcopy & pbpaste: Manipulating the Clipboard from the Command Line

Mar 5, 2007 - 6 Comments

pbcopy and pbpaste are command line interfaces to the clipboard of Mac OS X

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:
$ pbpaste
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.

Terminal in OS X 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.

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: Paul Horowitz in Command Line, How to, Mac OS X


» Comments RSS Feed

  1. […] puesto que el programa que extrae los EXIF tambien funciona en Mac, y se puede usar el comando pbcopy para copiar el resultado al portapapeles, pero no lo he probado porque no tengo un Mac … si […]

  2. macdet says:

    wau, I love osx … but this is new and helpfull

  3. […] covered pbcopy & pbpaste a while ago, they’re just command line interfaces for the Mac OS X clipboard but combined […]

  4. […] text, or a command string that you want to securely transfer from one Mac to another? Combining the command line clipboard tools pbcopy and pbpaste with SSH we can do exactly that, and it doesn’t matter where the other Mac is located:LAN, […]

  5. Lauri Ranta says:

    Depending on the environment, pbcopy and pbpaste might replace non-ASCII characters with question marks. It can be avoided by setting the __CF_USER_TEXT_ENCODING variable: `__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste`. See http://hints.macworld.com/article.php?story=20081231012753422.

  6. michael says:

    Copy an URL from a website to the clipboard. E.g.: the URL of an podcast ( == audiofile).
    Then …

    curl -LO `pbpaste`

    The command downloads the file into the current working directory.

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