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
xclipcommand in Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to install xclip
xclipbeing 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.
$ sudo apt update $ sudo apt install xclip
$ sudo dnf install xclip
$ 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.
- You can pipe output to
xclipif you want it to end up on the user’s clipboard instead of in their terminal. For example, we can
cata file and send the output directly to the clipboard:
$ cat myfile.txt | xclip
If using the X window system, the contents of
myfile.txtwill not be on your clipboard. Otherwise, it is still accessible using the middle mouse button.
xclipcan also the contents of your clipboard. For example, to output the clipboard, use the
$ xclip -o
- Or to send the clipboards content directly to a file, use Bash shell redirection
$ xclip -o > myfile.txt
- To place a text string into xclip and your clipboard, you can use
echoand pipe the string to
$ echo "welcome to linuxconfig.org" | xclip
- To interact with the system clipboard, rather than the X window clipboard, you can use the
-selectionoption. For example, we can supply the
clipboardargument 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
xclipto access the other clipboard with
-o, make sure you specify the correct
$ xclip -o -selection clipboard
- xclip can simplify tasks that would ordinarily take a few steps to complete. For example, we can
sshto 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
In this tutorial, we saw how to install and use the
xclipcommand 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.