The Power of pbcopy
When working in the macOS Terminal, you often generate complex output that you need to use elsewhere. Perhaps you just ran a diagnostic command that spat out a hundred lines of hardware errors, or you used a script to generate a complex SSH key.
The standard way to handle this is tedious: you use your mouse, click and drag to highlight the massive block of text in the terminal window, hit Cmd+C, and hope you didn’t accidentally miss the final character or highlight unwanted empty space.
Instead of relying on the GUI, macOS provides a brilliant, native command-line utility called pbcopy (Pasteboard Copy). It allows you to pipe the exact output of any terminal command directly into the macOS clipboard, silently and perfectly, ready to be pasted (Cmd+V) into an email, a Slack message, or a text document.
Step 1: The Basic pbcopy Pipe
The true power of pbcopy comes from the “pipe” operator (|), which takes the output of one command and feeds it directly into another.
Open the Terminal app (located in Applications > Utilities).
Let’s say you want to generate a list of all files in your current directory and instantly copy that list so you can paste it into an email.
Instead of just running ls -la, you pipe it into pbcopy:
ls -la | pbcopy
When you press Enter, nothing will appear on the terminal screen. The output was hijacked and sent directly to your Mac’s invisible clipboard. Open a blank document in TextEdit and press Cmd+V. The entire directory list will appear perfectly formatted.
Step 2: Copying the Contents of a File
If you need to copy the contents of a specific file (like a configuration script or a public SSH key), you do not need to open the file in a text editor.
You can use the cat command to read the file, and immediately pipe that output to the clipboard.
For example, to instantly copy your public SSH key so you can paste it into GitHub:
cat ~/.ssh/id_rsa.pub | pbcopy
The exact string of cryptography is now on your clipboard, with zero risk of you accidentally highlighting extra spaces with your mouse.
Step 3: Copying System Diagnostics
If you are troubleshooting a network issue with an IT helpdesk, they might ask you for your Mac’s current routing table.
The output of the netstat command is notoriously long and difficult to highlight manually. Instead, run:
netstat -nr | pbcopy
You can then immediately flip over to your email or chat application and paste the pristine diagnostic data to the technician.
Step 4: Using pbpaste (The Reverse Operation)
The sister command to pbcopy is pbpaste. As you might expect, this takes whatever is currently sitting on your macOS clipboard and dumps it straight into the terminal.
This is incredibly useful if you copied a block of text from a website (like a list of URLs or IP addresses) and want to instantly save it as a new text file on your Mac without opening an editor.
You can use the redirect operator (>) to push the pasted text into a new file:
pbpaste > ~/Desktop/saved_links.txt
Step 5: Chaining Commands Together
Because these commands operate natively in the shell, you can chain them with other powerful UNIX tools like grep or awk.
For example, if you have a massive server log file and you only want to copy the lines containing the word “ERROR” to your clipboard, you can combine the tools:
cat /var/log/system.log | grep "ERROR" | pbcopy
This command reads the file, filters it to only show the errors, and instantly copies those specific errors to your clipboard, bypassing the screen entirely.