If you do much work at all in the Terminal, there are two macOS commands you should know about but probably don’t. They’re called
pbpaste, and they’re extremely powerful. These two commands let you use Clipboard in Terminal without ever raising your fingers from the keys.
Why Do I Need to Use Clipboard in Terminal?
If you’re really asking this question, maybe you aren’t as much of a Terminal fanatic as you claim to be. Using
pbcopy, you’re able to not only copy the contents of a file into the Clipboard, but also redirect the output of Terminal commands into it.
If you’ve ever tried to easily get the contents of a text file into the Clipboard, you know what a hassle it can be. You have to open the file in TextEdit, highlight the contents, and then use the Copy shortcut. If you want an easier method,
pbcopy might just become your new best friend.
Copying to the Clipboard
There are two main uses I have for
pbcopy. Those are copying text from a file, and redirecting output from a Terminal command. The first is the easiest:
pbcopy < file.txt
This will take all of the contents of
file.txt and copy it to the Clipboard. You can then access it using the Finder’s Clipboard shortcuts or
Let’s look at a more complicated example. Let’s say I want to generate a text file of all of the processes running on my Mac. I could simply type
ps aux and then try to highlight everything, but the output would likely span several screens. To more effectively use Clipboard in Terminal, I just do this:
ps aux | pbcopy
That command string will pipe the results of my
ps command to the Clipboard. Again, I can use the Finder’s Clipboard shortcuts or the
pbpaste command to access the text.
Pasting From the Clipboard in Terminal
Next up is
pbpaste. This command retrieves the data from the Clipboard buffer, writing to the Terminal. If I just type
pbpaste without any arguments, the command will output the Clipboard contents straight into my command line.
Let’s say I want the Clipboard contents to go into a file. For that, I just add an argument:
pbpaste > file.txt
That would insert the contents of the Clipboard into a file called
That’s all cool and stuff, but what about filtering through the data? Take a look at this.
ps aux | pbcopy pbpaste > file.txt
When I ran that set of commands just now, it generated a 430-line text file. What if I wanted to fiilter it to certain processes? For that, I use
grep alongside either
pbpaste. So, I do this:
ps aux | pbcopy pbpaste | grep Chrome > file2.txt
Now I have a much more manageable text file; it’s only six lines instead of 430.
But Wait, There’s More
pbpaste commands are insanely powerful, and I’m not even fully fluent in the intricacies of making the most of them. For example, there are multiple Clibpoards, or pasteboards, that the Terminal commands can work with. You can also specify what type of data to look for first in the pasteboard — plain text, rich text, or Encapsulated PostScript.
If you want to really brush up on how to use
pbpaste, I recommend reading through
man pbcopy in the Terminal. There are also several topics in the ADC Reference Library available to paid registered developers. These topics include:
- Cocoa > Interapplication Communication > Copying and Pasting
- Carbon > Interapplication Communication > Pasteboard Manager Programming Guide
- Carbon > Interapplication Communication > Pasteboard Manager Reference.
As you can see, these are some pretty powerful commands. I’d say add them to my list of the five best Terminal commands you need to know about.