Copying File Contents to Clipboard via Terminal Command

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×

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.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×

About Paul Mestemaker

Paul Mestemaker is a full stack engineer /Product guy who splits time between Los Angeles and San Francisco. He helps startups launch their MVPs and gather feedback through usability tests and live site metrics. In his former life, Paul was a co-founder of Mythly Studios and System Center Advisor and Head of Product at Close Inc. He co-authored a book on SQL Server performance tuning. He has been rated as a top speaker at conferences (TechEd, PASS, Connections, et al). He is also a guest lecturer at Colleges and Universities across the U.S. He was the co-host of a weekly tech news show with over 6 million views. Paul's proudest accomplishments is unlocking the "World Champ" achievement on Wordament.

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×