Paul Mestemaker
Published on

Copying File Contents to Clipboard via Terminal Command

Authors

Have you ever been in terminal and wanted to copy contents of a file to the clipboard and move on to your next task? This seems to be a pretty common occurrence for me and I just found out there is a command to do precisely that: pbcopy.

Let's say you're IMing with someone, and you want to send them your public key, so they can set you up to ssh into their server. Depending on your IM client, it could be a pain to send a file, but the contents of your public key is just text. Previously, I would just do this:

$ cat ~/.ssh/id_rsa.pub

... then highlight the output with my mouse, and copy & paste. Not only is using the mouse for n00bs, but when you have something like public keys, you need to be sure to capture the whole key exactly and have no room for human error. Now I've learned that I can just pipe to pbcopy and the contents will be copied to my clipboard.

$ cat ~/.ssh/id_rsa.pub | pbcopy

In case you're wondering, pbcopy stands for "pasteboard copy" because apparently clipboard wasn't a thing when this command was written decades ago. Here is another article on clipboard vs. pasteboard.