Last Updated: February 25, 2016
·
1.226K
· ryepdx

pbcopy and pbpaste under *nix

If you're a recent transplant from OS X, or if you just do a lot of copying and pasting from the command line under X and want to save yourself some typing, you might find it helpful to add these aliases to your .bashrc:

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

This allows you to copy files or command output to the clipboard, and paste your clipboard to files and the command line.

For example, to decrypt my Bitcoin WoT authentication challenge string and copy it to my clipboard, I can just run:

wget -O - http://bitcoin-otc.com/otps/680F5108B06DBB77 | gpg -d | pbcopy

The line above just grabs the text at http://bitcoin-otc.com/otps/680F5108B06DBB77, passes it to gpg for decryption, and then passes the decrypted string to my clipboard via the pbcopy alias.

Alternatively, if I want to copy a file:

pbcopy somefile.md

This does rely on xclip, which may not already be on your system. To install it under Ubuntu:

sudo apt-get install xclip

The source code for xclip can be found on Sourceforge.