Working in the Linux terminal often involves commands like pwd, ls, or administrative tools such as useradd, along with editing configuration files in tools like the vim editor or the visudo editor. At some point, you’ll need to copy command output quickly—whether you’re documenting something, debugging, or sharing results. While it may seem like a small task, doing it efficiently can significantly improve your workflow, especially if you spend a lot of time in the terminal.
In this guide, I’ll walk you through three reliable methods to copy terminal output directly to your clipboard, along with detailed explanations so you fully understand what’s happening behind the scenes.
Understanding How This Works
Before jumping into tools, it’s important to understand the underlying concept.
In Linux, most commands print their results to something called standard output (stdout). Normally, this output is displayed in your terminal. However, using a pipe (|), you can redirect that output to another program instead.
Clipboard tools like xclip, xsel, or wl-copy act as receivers for that output. Instead of showing it on screen, they capture it and place it into your system clipboard.
This is why a command like:
ls | xclip -selection clipboard
doesn’t display anything in the terminal—it redirects the output entirely to the clipboard.
Method 1: Using xclip (Most Common and Practical)
xclip is the most widely used clipboard utility on Linux, especially in X11-based desktop environments. It integrates well with most terminal emulators and works reliably across distributions.
Installation
Ubuntu / Debian:
sudo apt install xclip
Arch Linux:
sudo pacman -S xclip
AlmaLinux / Rocky Linux:
sudo dnf install xclip
How xclip Works
When you use xclip, you’re interacting with X11 clipboard selections. The important part here is:
-selection clipboard
This tells xclip to use the system clipboard (the one you paste with Ctrl+V), not the primary selection (which is used for middle-click paste).
Practical Usage
pwd | xclip -selection clipboard
This copies your current working directory into the clipboard.
ls -lah | xclip -selection clipboard
This copies a detailed directory listing.
These examples might seem simple, but they become extremely useful when writing documentation or sharing outputs remotely.
Seeing Output While Copying
A common issue is that piping removes output from the screen. This is where tee becomes very useful.
ls -l | tee >(xclip -selection clipboard)
What happens here is interesting. The tee command duplicates the output: one copy goes to your terminal, and the other is sent to xclip. This allows you to both see and copy the result at the same time.
Method 2: Using xsel (A Minimal Alternative)
xsel provides similar functionality to xclip, but with a slightly different syntax and a lighter footprint. Some users prefer it because it feels more minimal and straightforward.
Installation
Ubuntu / Debian:
sudo apt install xsel
Arch Linux:
sudo pacman -S xsel
AlmaLinux / Rocky Linux:
sudo dnf install xsel
How xsel Works
Unlike xclip, which uses flags like -selection, xsel uses options such as --clipboard and --input. Functionally, the result is the same: it takes input and stores it in the clipboard.
Usage Example
ls | xsel --clipboard --input
This command sends the output of ls directly into your clipboard.
pwd | xsel --clipboard --input
Again, very similar to xclip—just a different syntax.
Method 3: Using wl-copy (Wayland Environments)
Modern Linux distributions are gradually moving from X11 to Wayland. If you’re using a newer desktop environment, xclip might not work properly.
That’s where wl-copy comes in.
Installation
Ubuntu / Debian:
sudo apt install wl-clipboard
Arch Linux:
sudo pacman -S wl-clipboard
AlmaLinux / Rocky Linux:
sudo dnf install wl-clipboard
How wl-copy Works
wl-copy is designed specifically for Wayland compositors. It behaves more simply than xclip—no need for selection flags.
Usage Examples
ls | wl-copy
pwd | wl-copy
To retrieve clipboard content:
wl-paste
10 Real-Life Examples You’ll Actually Use
Now let’s move beyond theory and look at practical, everyday use cases. These examples are based on real scenarios you’re likely to encounter.
1. Copy Current Directory Path
pwd | xclip -selection clipboard
This is useful when you need to paste a path into a configuration file or script without typing it manually.
2. Copy Detailed Directory Listing
ls -lah | xclip -selection clipboard
Ideal for sharing directory structures or including them in documentation.
3. Copy Contents of a Specific Directory
ls /etc/nginx | xclip -selection clipboard
Helpful when debugging services or reviewing configuration directories.
4. Copy Output Including Errors
useradd testuser 2>&1 | xclip -selection clipboard
This captures both standard output and errors, which is essential when troubleshooting.
5. Copy File Content
cat /etc/hosts | xclip -selection clipboard
Useful when editing or comparing system configuration files.
6. Copy Installed Packages List
dpkg -l | xclip -selection clipboard
Or on RHEL-based systems:
rpm -qa | xclip -selection clipboard
Great for audits, migrations, or system documentation.
7. Copy Disk Usage Information
df -h | xclip -selection clipboard
Quick way to share storage information with others.
8. Copy Running Processes
ps aux | xclip -selection clipboard
Very useful when diagnosing performance or resource issues.
9. Copy Network Configuration
ip a | xclip -selection clipboard
Perfect for troubleshooting connectivity or sharing system info.
10. Copy Output While Still Seeing It
ls -l | tee >(xclip -selection clipboard)
This is one of the most practical commands—you don’t lose visibility while copying.
Common Issues and Limitations
One important thing to understand is that clipboard tools depend on a graphical session. If you’re working on a headless server (no GUI), these tools won’t function.
In such cases, your best alternative is to redirect output to a file:
ls -l > output.txt
You can then transfer or open that file locally.
Another common issue is related to the DISPLAY variable. If you see:
Error: Can't open display
You may need to run:
export DISPLAY=:0
Final Thoughts
Copying terminal output to the clipboard is one of those small improvements that has a surprisingly large impact on productivity. It reduces friction, minimizes manual errors, and integrates well with both scripting and documentation workflows.
Whether you’re copying output from pwd, analyzing results from ls, managing users with useradd, or editing system configurations in vim and visudo, having a reliable way to move data from terminal to clipboard makes everything more efficient.
Once you start using these methods regularly, manual copy-paste will feel slow and unnecessary.





