Copy Shell Prompt Output To Linux / UNIX X Clipboard Directly

Q. How do I copy output of command to Linux Gnome clipboard? How do I send files directly to X Windows clipboard from a shell prompt? How do I insert command line output or files contains into the clipboard?

A. You can copy command line output to X Windows clipboard directly using xclip command. You can read from standard input (keyboard), or from one or more files, and makes the data available as an X selection for pasting into any X applications such as gedit, OpenOffice or Firefox / email client. You can also print current X selection to standard out (screen or printer) form a shell prompt.

A note about Linux / UNIX X Server CLIPBOARD

There are totally 3 clipboards maintained by the X server as follows:

  • PRIMARY: The PRIMARY selection is conventionally used to implement copying and pasting via the middle mouse button. xclip command use this by default. So you need to hit middle button to paste data.
  • SECONDARY: This is less frequently used by X application. You need to use XA_SECONDARY constant to select this clipboard.
  • CLIPBOARD: Same as SECONDARY, use XA_CLIPBOARD constant to select clipboard.

xclip – Linux / UNIX Command line clipboard grabber

You can install xclip using any one of the following method:

Install xclip under Debian / Ubuntu Linux

Type the following command at shell prompt:
$ sudo apt-get install xclip

Install xclip under Red hat / CentOS / RHEL / Fedora Linux

Type the following command at shell prompt (make sure 3rd party repos are activated):
# yum install xclip

How do I use xclip command?

Copy output of the following command to clipboard:
$ sort -n -k 3, -k 2 file.txt | xclip

How do I paste output to GUI applications?

Just press middle click (mouse button) in an X application to paste data.

Task: Insert files contains into the clipboard

Send data.txt contains to the clipboard, enter:
$ cat data.txt | xclips

Task: Paste data from the clipboard

Copy data using CTRL + C or middle mouse button. Type the following command to paste output:
$ xclip -o
Put the contents of the selection into a file.
$ xclip -o > file.txt

Loop option

The -l (-loop) option help to send number of x selection requests (pastes into X applications) to wait for before exiting. For example start xclip and exit only after text has been pasted 10 times.
$ who | xclip -loops 3 -verbose

Task: Select secondary clipboard

$ uptime | xclip -selection XA_SECONDARY

xsel command

xsel is xclip like command which can be used to retrieve and set the X selection (copy and paste operations) with few additional options. Type the following command to install xsel:
$ sudo apt-get install xsel

xsel examples

Copy output of pwd command in the X selection. Then middle click in an X application to paste:
$ pwd | xsel
Put /etc/passwd in the X selection. Then middle click in an X application to paste:
$ cat /etc/passwd | xsel

Further readings:

  1. man page xsel and xclip

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Bill Bartmann Sep 2, 2009 @ 15:02

    Cool site, love the info.

  • en Aug 16, 2010 @ 12:41

    Thank you for the helpful information.

  • A. B. Apr 14, 2011 @ 1:14

    Tried “xclip -o > file.txt”, but it just made a blank file… When I used ctrl-v, it pasted into the file… any idea why?


  • A. B. Apr 14, 2011 @ 1:23

    Ah, this worked:
    xclip -selection clipboard -o > file.txt

  • castet Jul 11, 2012 @ 15:50

    # — just a copy/paste from my .bashrc
    [ . ] &function pclip () { xclip -o -selection primary; }

    function gclip1 () { xclip -i -selection primary; }
    function pclip1 () { xclip -o -selection primary; }

    function gclip2 () { xclip -i -selection secondary; }
    function pclip2 () { xclip -o -selection secondary; }

    function gclip3 () { xclip -i -selection clipboard; }
    function pclip3 () { xclip -o -selection clipboard; }

  • trusktr Jul 6, 2013 @ 7:45

    This guide seems to be outdated or wrong. For example instead of doing

    $ uptime | xclip -selection XA_SECONDARY

    you need to do

    $ uptime | xclip -selection secondary

    To make it so it appears in Gnome to paste with ctrl+v, do something like

    $ cat myfile.txt | xclip -selection clipboard

    If you just do

    $ cat myfile.txt | xclip

    then you can’t do ctrl+v in Gnome.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @