How to use xclip on Linux

The xclip tool can be used to interact with the system clipboard from the command line. This can come in handy in instances where you want to send information directly to the user’s clipboard, without requiring that they highlight and copy the text themselves. It also works in the other direction, so the xclip command can check the contents of the clipboard.

When working with Bash scripting to automate tasks, xclip is a life saver in certain scripts. In this tutorial, you will see how to install xclip on all major Linux distributions, and then learn how to use the xclip command through practical examples. Let’s see what it can do.

In this tutorial you will learn:

  • How to install xclip on all major Linux distros
  • How to use the xclip command in Linux
How to use xclip on Linux
How to use xclip on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software xclip
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install xclip

Despite xclip being the useful tool that it is, it is often not included by default on various Linux systems. You can use the appropriate command below to install xclip with your system’s package manager.

To install xclip on Ubuntu, Debian, and Linux Mint:

$ sudo apt update
$ sudo apt install xclip

To install xclip on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install xclip

To install xclip on Arch Linux and Manjaro:

$ sudo pacman -S xclip
The “x” in “xclip” refers to the X window manager. xclip specifically interacts with the clipboard that belongs to X (X11 / Xorg), so the behavior might vary a bit depending on which window manager you are using. For example, Wayland uses a separate clipboard, but you can still access the X clipboard (and therefore xclip) via the middle mouse button in terminal or other X program.

How to use xclip command on Linux

Now that the tool has been installed, let’s see how it can be useful to us by looking at some practical command examples below.

  1. You can pipe output to xclip if you want it to end up on the user’s clipboard instead of in their terminal. For example, we can cat a file and send the output directly to the clipboard:

    $ cat myfile.txt | xclip

    If using the X window system, the contents of myfile.txt will not be on your clipboard. Otherwise, it is still accessible using the middle mouse button.

  2. xclip can also the contents of your clipboard. For example, to output the clipboard, use the -o option.
    $ xclip -o
  3. Or to send the clipboards content directly to a file, use Bash shell redirection > operator:
    $ xclip -o > myfile.txt
  4. To place a text string into xclip and your clipboard, you can use echo and pipe the string to xclip.
    $ echo "welcome to" | xclip
  5. To interact with the system clipboard, rather than the X window clipboard, you can use the -selection option. For example, we can supply the clipboard argument to instruct xclip to copy the text to the same clipboard accessible through the right click context menu.
    $ cat myfile.txt | xclip -selection clipboard

    If you want to use xclip to access the other clipboard with -o, make sure you specify the correct -selection once more:

    $ xclip -o -selection clipboard
  6. xclip can simplify tasks that would ordinarily take a few steps to complete. For example, we can ssh to a remote host and copy the contents of a file to our clipboard, with it ready to paste into any program or terminal that we want.
    $ ssh user@host "cat /myfile.txt" | xclip -selection clipboard

Closing Thoughts

In this tutorial, we saw how to install and use the xclip command on a Linux system. Xclip is the perfect tool for accessing the contents of the clipboard from the command line, or for placing text on the clipboard. It has access to both the X system clipboard and the user’s system clipboard, depending on your use case.