Using the Mac OS or Mac OS X is an exciting experience as there are many options to explore for work or play. The pbcopy and pbpaste commands are of the many options you can use to create, manage, and share from the shell. You can copy and paste input/output to paste to IM/IRC or even use macro tools to create modifiers for workflows to increase productivity.

Many users are not aware of these useful commands that can be used in Terminal to regulate the clipboard. This tutorial will show you how to use the pbcopy and pbpaste commands on Mac.

Use Pbcopy Command on Mac

Gain access to the mac OS or Mac OS X clipboard from a shell and integrate the data from GUI apps using command line apps. Pbcopy is a command used to copy data from STDIN to the clipboard on Mac. The standard input is placed on the pasteboard that is specified. If the user doesn’t specify the pasteboard, the data is posted to the general pasteboard by default. You can select text in Terminal, copy it and paste it somewhere else. Or, paste text into the command prompt. Use pbcopy to copy data using these commands:

Syntax

pbcopy [-pboard {general | ruler | find | font}]

Key

-pboard Specify a pasteboard to copy to.

By default the general pasteboard

All input data is copied to a board in ASCII format with the exception of Encapsulated PostScript (EPS) file headers and Rich Text Format (RTF). The data may be converted on the clipboard. Here are a few examples of how pbcopy command may be used:

# Copy the contents of a file to clipboard:

$ pbcopy < cookies.txt

# Copy part of a file to clipboard:

$ grep 'ip address' serverlist.txt | pbcopy

# Copy a list of files in the home directory to mac OS clipboard:

$ ls ~ | pbcopy

Paste the content into an app using ⌘ + v. This way, you don’t need to reopen them in another application to use them. You can also manipulate the data using:

`$ pbpaste | sed -e ‘s/foo/bar/g’ | pbcopy`

Pbcopy and sed used together can always be combined with other tools on Mac and Linux. Make the new content on the clipboard visible using pbpaste as the next command.

Use Pbpaste Command On Mac

Pbcopy is a command used to remove data to STDOUT from the clipboard on Mac. You can control what files or data is searched for. Use the pbpaste command:

Syntax

pbpaste [-pboard {general | ruler | find | font}]

[-Prefer {ascii | rtf | ps}]

Key

-pboard Specify a pasteboard to copy to.

By default the general pasteboard.

-Prefer Specify the type of data to look for in the pasteboard.

The standard ASCII input is searched out first on the clipboard. If there isn’t any ASCII data, the search will expand to look for the Encapsulated PostScript (EPS) file headers and Rich Text Format (RTF) data. If neither file format is found, no output will come from the pasteboard.

When using pbcopy and pbpaste, the data is not visible during the transfers. You can make the content more visible with pbpaste using this script:

$ uuidgen | pbcopy

$ pbpaste

A524B454-5B42-4832-943D-896DF755FDEC

Or, you can also use:

$ uuidgen | pbcopy; pbpaste

95DC9C0E-052E-4896-A4D3-1BB5EAECD93C

Here are some other examples of using the pbpaste command:

Save the clipboard to a file in the users home folder:

$ pbpaste > ~/ss64.txt

Save the clipboard to END of file in the users home folder:

$ pbpaste >> ~/ss64.txt

Replace spaces in the clipboard text with %20

$ pbpaste | sed 's/ /%20/g' | pbcopy

Replace the \t with something liberal such as:

$ pbpaste | sed "s/,/\t/g" | pbcopy

Look first for the RTF format:

$ pbpaste -Prefer RTF

Replace the current contents of the clipboard with a base64 encoded version:

$ pbpaste | base64 | pbcopy

Note: These commands connect the clipboard and shell commands more closely. Most of these types of commands are interchangeable between Mac and Linux. However, pbcopy and pbpaste are not.

Contact Apple Support

If you were not able to use the pbcopy and pbpaste commands in Terminal by following the steps in this tutorial, you can contact Apple Support or the Genius Bar for more help.