How can I copy the output of a command directly into my clipboard?

469,212

Solution 1

I always wanted to do this and found a nice and easy way of doing it. I wrote down the complete procedure just in case anyone else needs it.

First install a 16 kB program called xclip:

sudo apt-get install xclip

You can then pipe the output into xclip to be copied into the clipboard:

cat file | xclip

To paste the text you just copied, you shall use:

xclip -o

To simplify life, you can set up an alias in your .bashrc file as I did:

alias "c=xclip"
alias "v=xclip -o"

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):

Terminal 1:
pwd | c

Terminal 2:
cd `v`

Notice the ` ` around v. This executes v as a command first and then substitutes it in-place for cd to use.

Only copy the content to the X clipboard

cat file | xclip

If you want to paste somewhere else other than a X application, try this one:

cat file | xclip -selection clipboard

Solution 2

On OS X, use pbcopy; pbpaste goes in the opposite direction.

pbcopy < .ssh/id_rsa.pub

Solution 3

I've created a tool for Linux/OSX/Cygwin that is similar to some of these others but slightly unique. I call it cb and it can be found in this github gist.

In that gist I demonstrate how to do copy and paste via commandline using Linux, macOS, and Cygwin.

Linux

_copy(){
    cat | xclip -selection clipboard
}

_paste(){
    xclip -selection clipboard -o
}

macOS

_copy(){
    cat | pbcopy
}

_paste(){
    pbpaste
}

Cygwin

_copy(){
    cat > /dev/clipboard
}

_paste(){
    cat /dev/clipboard
}

Note: I originally just intended to mention this in my comment to Bob Enohp's answer. But then I realized that I should add a README to my gist. Since the gist editor doesn't offer a Markdown preview I used the answer box here and after copy/pasting it to my gist thought, "I might as well submit the answer." If you would like to discuss functionality/bugs it would probably be best to do that in the comments for the gist on github.

cb

A leak-proof tee to the clipboard

This script is modeled after tee (see man tee).

It's like your normal copy and paste commands, but unified and able to sense when you want it to be chainable

Examples

Copy

$ date | cb
# clipboard contains: Tue Jan 24 23:00:00 EST 2017

Paste

# clipboard retained from the previous block
$ cb
Tue Jan 24 23:00:00 EST 2017
$ cb | cat
Tue Jan 24 23:00:00 EST 2017
$ cb > foo
$ cat foo
Tue Jan 24 23:00:00 EST 2017

Chaining

$ date | cb | tee updates.log
Tue Jan 24 23:11:11 EST 2017
$ cat updates.log
Tue Jan 24 23:11:11 EST 2017
# clipboard contains: Tue Jan 24 23:11:11 EST 2017

Copy via file redirect

(chronologically it made sense to demo this at the end)

# clipboard retained from the previous block
$ cb < foo
$ cb
Tue Jan 24 23:00:00 EST 2017
# note the minutes and seconds changed from 11 back to 00

Solution 4

I wrote this little script that takes the guess work out of the copy/paste commands.

The Linux version of the script relies on xclip being already installed in your system. The script is called clipboard.

#!/bin/bash
# Linux version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard     # Pipe someapp's output into clipboard
#        clipboard | someapp     # Pipe clipboard's content into someapp
#

if command -v xclip 1>/dev/null; then
    if [[ -p /dev/stdin ]] ; then
        # stdin is a pipe
        # stdin -> clipboard
        xclip -i -selection clipboard
    else
        # stdin is not a pipe
        # clipboard -> stdout
        xclip -o -selection clipboard
    fi
else
    echo "Remember to install xclip"
fi

The OS X version of the script relies on pbcopy and pbpaste which are preinstalled on all Macs.

#!/bin/bash
# OS X version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard     # Pipe someapp's output into clipboard
#        clipboard | someapp     # Pipe clipboard's content into someapp
#

if [[ -p /dev/stdin ]] ; then
    # stdin is a pipe
    # stdin -> clipboard
    pbcopy
else
    # stdin is not a pipe
    # clipboard -> stdout
    pbpaste
fi

Using the script is very simple since you simply pipe in or out of clipboard as shown in these two examples.

$ cat file | clipboard

$ clipboard | less

Solution 5

Linux, macOS, Windows (WSL/CYGWIN)

Each of those systems use its own tool to incorporate the clipboard functionality into the command line interface (CLI). This means, when using for example the Ubuntu CLI on Windows Subsystem for Linux (WSL) the usual xclip solution won't work. The same holds true for macOS.

The following table provides an overview for the copy/paste tools needed on the different systems:

OS Copy Paste
WSL clip.exe powershell.exe Get-Clipboard
CYGWIN > /dev/clipboard cat /dev/clipboard
macOS pbcopy pbpaste
Linux xclip -sel clip xclip -sel clip -o

Unified .bashrc solution

Just put the following code into your ~/.bashrc to enable the usage of copy and paste on all systems. The solution works on "normal" Linux systems (i.e. Ubuntu, Debian) as well as on WSL and macOS:

if grep -q -i microsoft /proc/version; then
  # on WSL: version contains the string "microsoft"
  alias copy="clip.exe"
  alias paste="powershell.exe Get-Clipboard"
elif grep -q -i cygwin $(uname -a); then
  # on CYGWIN: uname contains the string "cygwin"
  alias copy="/dev/clipboard"
  alias paste="cat /dev/clipboard"
elif [[ ! -r /proc/version ]]; then
  # on MAC: version is not readable at all
  alias copy="pbcopy"
  alias paste="pbpaste"
else
  # on "normal" linux
  alias copy="xclip -sel clip"
  alias paste="xclip -sel clip -o"
fi

Usage on ALL systems

To copy:

# pipe
echo "hello world" | copy

# or for direct file input
copy < file

To paste:

paste > file
Share:
469,212
Legend
Author by

Legend

Just a simple guy :)

Updated on January 25, 2022

Comments

  • Legend
    Legend over 2 years

    How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance:

    cat file | clipboard
    
  • khotyn
    khotyn about 13 years
    "cat file | xclip" only copy the content to the 'X' clipboard, if you want to paste somewhere else other than a 'X' application, try this one: "cat file | xclip -selection clipboard"
  • Matt Joiner
    Matt Joiner about 13 years
    Your choice of handle is appropriate!
  • doublejosh
    doublejosh about 13 years
    Yups! Just like this for ex: pbcopy < .ssh/id_rsa.pub
  • Anake
    Anake almost 11 years
    If you want to remove the newline character so that you can directly paste and edit the result of "pwd | c", you can do "pwd | tr -d '\n' | xclip -selection c"
  • StackedCrooked
    StackedCrooked over 10 years
    I use the names pbcopy and pbpaste on Linux so it works for both Linux and OS X.
  • Ben
    Ben over 9 years
    doesn't work for command output tho - e.g. pbcopy < git merge-base master some-branch
  • Hockey
    Hockey about 9 years
    To make the script work globally in ubuntu: * Save it at ~/.scripts/clipboard * Make it executable chmod +x ~/.scripts/clipboard for bash: * add export PATH=$PATH:~/.scripts to the end of ~/.bashrc for fish: * add set PATH ~/.scripts $PATH to ~/.config/fish/fish.config If any of the files or folders don't already exist just create them.
  • Sebastian Nowak
    Sebastian Nowak almost 9 years
    You can also wrap this script as a function clipboard(){ ... } and paste it into .bashrc
  • Atav32
    Atav32 almost 9 years
    if you're using tmux, it won't work unless you reattach your session: superuser.com/a/413233/146254
  • d.raev
    d.raev over 8 years
    @AfshinMoazami, the title and the question itself is generic, Only the tags hint for unix system, and my answer covers the case when you try to get the date FROM a unix server TO a Windows machine, which may be useful future readers.
  • Yibo Yang
    Yibo Yang over 8 years
    also define alias "cs=xclip -selection clipboard" and alias "vs=xclip -o -selection clipboard" to make copying/pasting from system clipboard easier
  • Champ
    Champ about 8 years
    xclip fileName works on my Ubuntu Trusty 14.04. Also, xclip -selection c works for normal copy of Control-C. The equivalent of Control-V is xclip -o -selection c
  • Nearoo
    Nearoo almost 8 years
    Wouldn't the syntax of aliasses be alias a="b -c" instead of alias "a=b -c"? That's what I read on ubuntuusers, but apparently noone here notices it...
  • Bruno Bronosky
    Bruno Bronosky over 7 years
    This is very similar to the one I made, but I allow chaining like date | cb | tee -a updates.log. That would send the date command output to the clipboard and pass it along to the tee -a command which appends it to a file and passes it along to stdout. But it's like a "leak-proof tee to the clipboard" because if you just do date | cb you get no output. And finally it also does cb > file.txt gist.github.com/RichardBronosky/…
  • Ulf Aslak
    Ulf Aslak over 7 years
    Give a usage example please
  • Ivaylo Strandjev
    Ivaylo Strandjev almost 7 years
    One suggestion - strip the trailing newline character to avoid executing the command on pasting(e.g. if copy-pasting a path). I do that by modifying the command xclip -i -selection clipboard to xargs echo -n | xclip -i -selection clipboard
  • Ivaylo Strandjev
    Ivaylo Strandjev almost 7 years
    Actually the command above does not work well if the thing you want to copy to clipboard is multi line. Here is what I did to fix this: cat - | perl -pe 'chomp if eof' | xclip -selection clipboard
  • flith
    flith almost 7 years
    Upvoted because yes, useful to Windows admins who only have Powershell to play with locally (although now with LSW actually functional, I can drop into bash directly from Powershell and do my *nixy things from there).
  • ren.rocks
    ren.rocks almost 7 years
    you can also use cat, if you already work with it: cat file.txt | pbcopy
  • rogue lad
    rogue lad almost 7 years
    Its not available in RHEL.
  • Admin
    Admin over 5 years
    Do note that if an xserver (such as vcxsvr or xming) is installed on windows, a display is up and xclipboard is running, you can actually use xclip seemlessly.
  • Admin
    Admin over 5 years
    Its working on DOS prompt as well as git bash on windows 10 i.e. echo Hello World | clip
  • alper
    alper over 5 years
    I am not sure but I am getting following error Error: Can't open display: (null) @Legend
  • imrek
    imrek over 4 years
    cat file | ... There we go again.
  • Sam
    Sam over 4 years
    I know the comment is 5 years old, but in case someone stumbles on to this in the future, to use with command output, do git merge-base master some-branch | pbcopy
  • Joshua Dyck
    Joshua Dyck about 4 years
    This worked perfect for me to copy a command output to the clipboard in terminal on macOS, thanks!
  • Pablo Bianchi
    Pablo Bianchi over 3 years
    -i is the default. I use a lot for eg xsel -b | sort | uniq -c | sort -rn | xsel -b
  • HydraHatRack
    HydraHatRack about 3 years
    If you don't want a newline at the end of the copied content: cat file | xargs echo -n | pbcopy
  • limido
    limido about 3 years
    no need for cat and pipelines : xclip file
  • Anton Krug
    Anton Krug about 3 years
    That looks convoluted, what are the benefits compared to existing answers which use xsel?
  • awdr
    awdr about 3 years
    you dont have to install anything
  • Anton Krug
    Anton Krug about 3 years
    I would say that a single standalone xsel binary is nothing compared to the huge python3 framework. Some people might have xsel already installed, while not having python3. On top of that, your python script depends on the tkinter, which might not be installed as well. If the motivation of this answer to provide something with fewer dependencies, then I think it's exact opposite.
  • awdr
    awdr about 3 years
    On most linux systems is python already installed and tkinter is also includet.
  • galactica
    galactica about 3 years
    I also found this is more natural on OS X, the up-voted one: pbcopy < command didn't work for me.
  • Sebastián Vásquez
    Sebastián Vásquez about 3 years
    @alper, In WSL, instead of xclip use clip.exe. Check this: stackoverflow.com/a/44913872/3457432
  • alper
    alper about 3 years
    @SebastiánVásquez I am not using Windows I cannot run clip.exe
  • alper
    alper almost 3 years
    cd v`` says zsh: command not found: v
  • Ivan Gonzalez
    Ivan Gonzalez over 2 years
    This should be a ZSH plugin!
  • kay
    kay over 2 years
    pbcopy < [some alias command] doesn't work. But [alias command] | pbcopy does work. Thanks @HydraHatRack for newline fix. That helped my need.
  • patrick.holzer
    patrick.holzer over 2 years
    One solution to rule 'em all! Big thanks, worked for me on MacOS - i'll test it on Linux too
  • DraganescuValentin
    DraganescuValentin about 2 years
    pay attention to pbcopy to not confuse < with > because with this > you will paste into the file, possibly erasing it's contents
  • Pablo Adames
    Pablo Adames about 2 years
    It hangs while doing the chaining example in Ubuntu 18.04. The tests show the same behaviour, tried running cb --test and it hangs on the changing test.
  • Bruno Bronosky
    Bruno Bronosky about 2 years
    Thanks for the feedback @PabloAdames! I got some comments on the gist about it having when run over ssh. Are you running it on a local Ubuntu 18.04 or over ssh?
  • Fathy
    Fathy almost 2 years
    I created a tool here github.com/Ahmed7fathi/copycat
  • Pablo Adames
    Pablo Adames almost 2 years
    @bruno-bronosky I found the reported hanging in Ubuntu 18.04 as the local operating system