How to pipe terminal output to your clipboard! (And Vice Versa)

OMG! This is super useful! I can’t tell you how many times I’ve used these commands after cleaning up some file or output in terminal. It’s way better then highlighting all or the classic, using the mouse to highlight thousands of lines lol.

For OSX you can use the pbcopy command.

For example

cat file | pbcopy

Now the content is in your clipboard which is easily pasted wherever you want.

Alternatively, you can use pbpaste to put the clipboard content into the terminal!

pbpaste > pasted

Lucky for you, both of these commmands are baked into every version of modern OSX!

Now for Kali you can use the xclip command. Which of course is a little longer of a process to get set up. But is very painless.

First lets do the usual

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Once that’s done lets do

sudo apt-get install xclip

Once that’s done lets add the aliases to our .zshrc file

nano ~/.zshrc
CRTL + V to the bottom
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
Added the aliases

Now you can run the same command as OSX to directly copy terminal output to your clipboard!

cat file | pbcopy
Pipping output to the pbcopy command

Now feel free to paste it anywhere you like!

If you want to output the what’s currently in the clipboard you can do

echo `pbpaste`
Outputting clipboard to terminal

If you want to save it to a file you can do

echo `pbpaste` > pasted
Saving clipboard ontents to a file

Now just cat the file to confirm

cat pasted
Confirming the contents was saved

Leave a Reply