The xclip and xsel commands in Linux: copy to clipboard from terminal

Introduction

In the day-to-day work with Linux, it is frequent to need to copy text or commands from the terminal to other applications, such as a text editor, a web browser, or a chat client. Instead of selecting with the mouse and using Ctrl+Shift+C, there are command-line tools that allow sending the content directly to the graphical environment’s clipboard. The most popular ones are xclip and xsel. This article explains what they are, how to install them, their basic options, and some advanced examples so you can integrate them into your workflow.

What are xclip and xsel?

Both utilities act as a bridge between standard input (stdin) and the X11 clipboard. When you redirect the output of a command to xclip or xsel, the text becomes available in the clipboard and can be pasted with the usual shortcut (Ctrl+V or Shift+Insert). The main difference lies in their options and in the default behavior regarding the different buffers (primary, secondary, and clipboard).

Installation

In most Debian-based distributions, the package is called xclip or xsel. On Red Hat/Fedora systems the names are identical. Typical installation commands are:

  • Debian/Ubuntu: sudo apt update && sudo apt install xclip xsel
  • Fedora: sudo dnf install xclip xsel
  • Arch Linux: sudo pacman -S xclip xsel

After installation, simply verify that the binaries are in the PATH by running which xclip and which xsel.

Basic usage of xclip

The simplest way is to redirect the output of a command to xclip:

echo "Example text" | xclip

This call uses by default the primary buffer (selection). To paste, just middle-click or use Shift+Insert in most environments. If you want to use the clipboard that responds to Ctrl+V, you must specify the option -selection clipboard:

echo "Text for Ctrl+V" | xclip -selection clipboard

You can also read the clipboard content with the -o (output) option:

xclip -o -selection clipboard

Basic usage of xsel

xsel works very similarly, but its options are a bit more concise. To copy to the standard clipboard:

echo "Text for xsel" | xsel --clipboard --input

 

To read the clipboard:

 

xsel --clipboard --output

If you omit --clipboard, xsel acts on the primary buffer (selection).

Practical examples

    • Copy the current directory path:
pwd | xclip -selection clipboard

 

  • Save the output of a long command to the clipboard:

 

grep -r "error" /var/log --include="*.log" | xsel --clipboard --input
  • Exchange content between two terminals:
# In the first terminal
cat archivo.txt | xclip -selection clipboard
# In the second terminal
xclip -o -selection clipboard > copia.txt
  • Use xclip in an alias to simplify the command:
alias cpclip='xclip -selection clipboard'
# Then
ls -l | cpclip

Key differences between xclip and xsel

Although they fulfill the same function, there are nuances that may influence the choice:

  • Dependencies: xclip requires the X11 library, while xsel can be compiled with Wayland support via additional extensions.
  • Speed: In simple tests, xsel tends to be slightly faster when handling large volumes of data.
  • Syntax: Some users find xsel’s syntax more intuitive because the options --input and --output are explicit.
  • Availability: In minimal server installations without a graphical environment

    This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .