How to Copy Paste Clipboard on Linux (With Examples) – Use xclip Tricks

If you’ve ever copied something in a Linux terminal and wished you could paste it into a browser, file, or GUI app — xclip is the tool you need.

Quick Summary: xclip Commands Cheat Sheet

TaskCommand
Install xclip (Ubuntu/Debian)sudo apt install xclip
Install xclip (RHEL/CentOS)sudo dnf install xclip
Copy text to clipboardecho "hello" | xclip -selection clipboard
Copy file contentxclip -selection clipboard < file.txt
Paste from clipboardxclip -selection clipboard -o
Copy command outputls -l | xclip -selection clipboard
Copy SSH public keyxclip -selection clipboard < ~/.ssh/id_rsa.pub
Use primary selection (mouse select)xclip -selection primary

What Is xclip?

xclip is a command-line utility that allows you to access the X11 clipboard from the terminal.

It works in desktop Linux environments such as:

  • GNOME
  • KDE
  • XFCE
  • Any system running X11

⚠️ Note: xclip does not work in pure TTY or Wayland-only environments without X11 compatibility.


How to Install xclip

Ubuntu / Debian

sudo apt update
sudo apt install xclip

RHEL 8 / RHEL 9 / CentOS / Rocky Linux

sudo dnf install xclip

Since you’re experienced with RHEL systems, you’ll typically use dnf in modern enterprise environments.

Arch Linux

sudo pacman -S xclip

How to Copy Text to Clipboard in Linux Terminal

Method 1 – Copy Simple Text

echo "Hello World" | xclip -selection clipboard

Now you can paste it into:

  • Browser
  • VS Code
  • LibreOffice
  • Any GUI app

Method 2 – Copy File Content

xclip -selection clipboard < file.txt

Useful when:

  • Copying configuration files
  • Sending logs to Slack
  • Sharing output quickly

Method 3 – Copy Command Output

ls -lah | xclip -selection clipboard

Perfect for:

  • Debugging
  • Sharing server info
  • Sending command results

How to Paste from Clipboard in Terminal

xclip -selection clipboard -o

The -o flag means “output”.

This prints clipboard content into the terminal.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide


Understanding Clipboard Selections in xclip

Linux has two clipboard types:

SelectionDescription
primaryMouse selection (middle-click paste)
clipboardStandard Ctrl+C / Ctrl+V clipboard

Most users want:

xclip -selection clipboard

Practical Real-World Examples

1. Copy SSH Public Key (Very Common)

xclip -selection clipboard < ~/.ssh/id_rsa.pub

Now paste into:

  • GitHub
  • GitLab
  • Cloud console
  • Remote server

2. Copy IP Address Quickly

hostname -I | awk '{print $1}' | xclip -selection clipboard

Very useful when configuring remote servers.


3. Copy Kubernetes Pod Logs

kubectl logs mypod | xclip -selection clipboard

Instantly share logs without saving files.


4. Copy Database Query Result

psql -c "SELECT now();" | xclip -selection clipboard

Helpful when working with PostgreSQL.


5. Copy JSON Pretty Output

cat data.json | jq . | xclip -selection clipboard

Clean formatted JSON copied instantly.



xclip vs xsel – Which Is Better?

Featurexclipxsel
Simple syntax
Widely used⚠️
Better documentation⚠️

Most tutorials and enterprise environments use xclip.



5 Quick One-Line FAQ

1. What does xclip do?
It copies and pastes data between the Linux terminal and GUI clipboard.

2. Does xclip work over SSH?
Yes, if X11 forwarding is enabled (ssh -X).

3. What’s the difference between primary and clipboard?
Primary is mouse-select paste; clipboard is Ctrl+C/Ctrl+V.

4. Does xclip work on Wayland?
Not reliably — use wl-copy instead.

5. How do I copy a file without opening it?
Use: xclip -selection clipboard < filename


Learning xclip will save you time every day.


David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 596

Leave a Reply

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