We use copy and paste all the time, so why not use them from the command line? I do it all the time, so let’s do a quick run down.
First, the commands:
- Command line copy:
pbcopy1 - Command line paste:
pbpaste
Copy something from the web and paste it to a file
This is my most frequent usage of either of these commands, specifically pbpaste. Imagine you’re at work looking at some code or some data in your browser, and you want to save it locally. Let’s use jsonplaceholder api data as an example:

The screenshot above is me copying data from my browser. I’d like to put that data directly into a file on my system:
# Paste the json in your terminal and redirect the output into a file
# This file doesn't need to exist already
pbpaste > example.jsonThat’s it. Now the contents of example.json are exactly the same json I just copied.
Copy the output of a command directly to your clipboard
Let’s say someone asks you: “Can you find me references to {thing} in our code?” An obscure request, but sure we can do that using grep (note: you should use ripgrep instead):
# Search for "thing", case-insensitive, and then pipe it into pbcopy
grep -i thing ./* | pbcopyNow you can jump into slack or email or wherever and just paste the result. I find I do something like this with files more often:
Copy the contents of a file to your clipboard
# cat (print) the file and then pipe the output to your clipboard
cat example.json | pbcopy
# or alternatively, use input redirection "<"
# this way feels awkward to me since it breaks the usual
# left-to-right command line pattern
pbcopy < example.jsonNow the contents of example.json are on your clipboard, ready to be pasted wherever your heart desires.
Helpful links
Pipes on the linux command line – Redhat
Notes
- It’s
pbcopyandpbpastebecause apparently it’s really called “pasteboard” instead of “clipboard”. Who knew? ↩︎