Search

Top Posts

Comments

  • MMES: If you customize the equalizer setting in iTunes, those settings will carryover to the iPhone and you can enjoy...
  • hadjime: Doesn’t work on me… i remove the file and then i open photo booth and it’s still wotking
  • John Doe: I’m having a bit of trouble trying to install fink, I know how to execute the command lines but I...
  • Slopjong: The same to me. It doesn’t work on 10.5.
  • Pascal (Former  IS&T France): It would be nice if the equalizzer could remember your prefered eq setting...

Categories

Recent Posts


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. The commands pbcopy and pbpaste do exactly what they sound like, copy and 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 prompt. So here’s a quick explanation on how to use them and what to use them for:

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.

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.

Posted by: Editor

Share

Save big on Mac Deals from Amazon.com

Comments:

Comments: 2

Pingback from Extraer los EXIF de una foto al portapapeles con Lightroom – Canonistas.com
Time: June 23, 2009, 2:51 am

[...] 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 [...]

Comment from macdet
Time: January 8, 2010, 3:45 am

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

Write a comment






March 5th, 2007