How to Use the pbcopy and pbpaste Commands in the macOS Terminal

If you spend a significant amount of time working in the macOS Terminal, you frequently encounter situations where you need to move text between your command-line environment and your graphical applications. Highlighting long outputs with your mouse, right-clicking, and selecting “Copy” is slow and prone to errors, especially when dealing with hundreds of lines of log files or complex SSH keys.

Apple includes two powerful, native command-line utilities built specifically to solve this problem: pbcopy and pbpaste. These tools provide a direct bridge between your Terminal commands and the system-wide macOS clipboard (the “pasteboard”), allowing you to pipe data directly into your clipboard without ever touching your mouse.

Why Use pbcopy and pbpaste?

The traditional method of manually selecting text in a terminal window is inefficient for several reasons. If an output is larger than your terminal window, scrolling and highlighting perfectly can be frustrating. Furthermore, manual selection often accidentally captures unwanted line breaks or terminal prompt characters.

By using pbcopy, you ensure that the exact output of a command—and nothing else—is securely saved to your clipboard, ready to be pasted into an email, a web browser, or a text editor using the standard Command+V shortcut.

How to Copy Command Output to the Clipboard (pbcopy)

The pbcopy command works by taking standard input (stdin) and placing it onto the macOS clipboard. The most common way to use it is by “piping” the output of another command into it using the vertical bar character (|).

Example: Copying the Contents of a File

One of the most frequent uses for this command is copying the contents of a public SSH key so you can paste it into a server provider’s dashboard like GitHub or DigitalOcean.

Instead of opening the file in a text editor, simply use the cat command and pipe it to pbcopy:

cat ~/.ssh/id_rsa.pub | pbcopy

The command will execute silently. Your terminal will return to the prompt, but the entire contents of your public key are now securely on your clipboard. You can immediately press Command+V in any application to paste it.

Example: Copying Directory Listings

If you need to send a colleague a list of all the files in a specific project folder, you can pipe the output of the ls command:

ls -la ~/Documents/ProjectFolder | pbcopy

How to Paste from the Clipboard to the Terminal (pbpaste)

The pbpaste command performs the exact opposite function. It takes whatever text is currently sitting on your macOS clipboard and outputs it to standard output (stdout) in the terminal.

While you could just press Command+V in the terminal window, pbpaste allows you to manipulate the clipboard data programmatically before it ever appears on your screen or writes to a file.

Example: Saving Clipboard Text to a File

If you have copied a large block of code or an error message from a website in Safari, and you want to save it directly to a text file in your current directory, you can use pbpaste with the redirect operator (>):

pbpaste > error_log.txt

This instantly creates a new file called error_log.txt containing your clipboard’s contents.

Example: Filtering Clipboard Content

You can also pipe the output of pbpaste into other powerful Linux utilities like grep. If you copied a massive system log from a webpage but only want to see the lines containing the word “Error”, you can run:

pbpaste | grep "Error"

This command reads your clipboard, filters the text, and displays only the relevant lines in your terminal, demonstrating how seamlessly these tools integrate the macOS graphical environment with the power of the command line.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.