I often use terminal to get things done quickly, be it generating a ssh key or modifying a config file, but it can be tough at times if we need to copy the contents of a file to clipboard quickly. Well, MacOS comes with pbcopy
Copying contents of a file to clipboard
You can run the below command to copy the contents of a file to clipboard. It is an easy command to remember and no much parameters. All one is doing is using cat
to read the file and instead of making it output to standard output we ‘pipe’ it to pbcopy
command which will copy the contents to clipboard.
cat /path/to/file | pbcopy
Where is it useful?
Without pbcopy
the normal way of doing it would be first highlight the text to copy and then do Command(⌘) + c
. This would work for small files with 10-20 lines. Now imagine a file which has around 100 lines, it would be time consuming to do it that way, using pbcopy
it just gets done in a single command.
You may be wondering why not use the GUI, well sometimes for me personally it is faster to get somethings done in terminal than use a GUI. It is a personal choice but it is good to know there are options still around for those who use terminal as well.