You can manipulate the clipboard directly from the command line using built-in utilities or small third-party tools. On Linux, xclip or xsel handle the X11 clipboard; on macOS, pbcopy and pbpaste are built-in; on Windows, clip and Get-Clipboard/Set-Clipboard in PowerShell do the job. This article explains the exact commands, how they work, and common pitfalls for each platform.
Why Use the Clipboard in a Terminal?
The clipboard is normally a graphical feature, but scripting it from the command line unlocks several practical uses:
- Pipe output directly – save a command’s result (like a file listing or a log excerpt) straight into the clipboard without opening an editor.
- Automate repetitive tasks – copy dynamic values (timestamps, generated passwords) into a clipboard for pasting into a web form or chat.
- Integrate with scripts – a backup script can copy the path of the last saved file, or a build script can copy a version number.
- Work on headless servers – with a remote desktop or X11 forwarding, you can still interact with the remote clipboard.
Once you know the right commands, the clipboard becomes just another stream you can pipe to and from, like stdout or a file.
macOS: pbcopy and pbpaste
macOS includes two commands that are as simple as they sound:
pbcopy– reads from standard input and places it on the clipboard.pbpaste– writes the current clipboard contents to standard output.
Examples:
echo "Hello from terminal" | pbcopy
pbpaste
cat file.txt | pbcopy
pbcopy < file.txt
You can also copy the output of a command directly:
ls -la | pbcopy
That copies the directory listing as plain text. To paste it into a file, redirect pbpaste:
pbpaste > clipboard_content.txt
One subtlety: pbcopy and pbpaste work with the general clipboard by default. If you want to use the “find” pasteboard (used by the Find panel in many apps), add the -pboard find option:
echo "search term" | pbcopy -pboard find
Linux (X11): xclip and xsel
Most Linux distributions don’t include a clipboard utility by default, but two popular tools fill the gap:
xclip– providesxclip -selection clipboardto copy andxclip -o -selection clipboardto paste.xsel– providesxsel --clipboard --inputto copy andxsel --clipboard --outputto paste.
Install them with your package manager, for example:
sudo apt install xclip # Debian/Ubuntu
sudo dnf install xclip # Fedora
sudo pacman -S xclip # Arch
Basic usage with xclip:
echo "text" | xclip -selection clipboard
xclip -o -selection clipboard
With xsel:
echo "text" | xsel --clipboard --input
xsel --clipboard --output
Both tools also support the “primary” selection (the middle-click paste buffer in X11) if you omit the --clipboard flag or use -selection primary. That’s a separate clipboard that many users confuse with the main one.
Windows: clip and PowerShell
Windows has a built-in clip.exe that copies standard input to the clipboard. It works in Command Prompt and PowerShell:
echo Hello | clip
dir | clip
To paste from the clipboard in Command Prompt, there’s no direct built-in, but you can use PowerShell:
powershell -command "Get-Clipboard"
In PowerShell itself, you have two cmdlets:
Set-Clipboard– takes input and copies it to the clipboard.Get-Clipboard– retrieves the clipboard content.
Examples:
Get-Process | Out-String | Set-Clipboard
Get-Clipboard
Note that Set-Clipboard can also handle non-text data like images, but the command-line examples above focus on text. For plain text, the classic clip.exe is still the simplest.
Clipboard in SSH and Remote Sessions
When you SSH into a remote Linux machine, the clipboard commands may not work because there’s no X server. In that case, you have a few options:
- Use a terminal multiplexer –
tmuxorscreenhave their own copy-paste buffers that work over SSH. - Forward X11 – if you connect with
ssh -X, the remotexclipcan talk to your local X server, so the clipboard is shared. - Use a local clipboard bridge – tools like
sshpassor custom scripts can copy the remote output to your local clipboard after the command finishes.
For most SSH users, the simplest approach is to use tmux and its copy mode (Ctrl-b [ to enter, then select and Ctrl-b ] to paste). That works entirely on the remote side and doesn’t need X.
Common Pitfalls and How to Avoid Them
- Trailing newline –
echoadds a newline by default. Useecho -non Linux/macOS orWrite-Output -NoNewlinein PowerShell to avoid it. - Binary data – the clipboard is text-oriented. Copying binary data (like an image file) with
cat image.png | pbcopymay corrupt it. Use dedicated tools likepngpasteon macOS orxclip -selection clipboard -t image/pngon Linux. - Multiple selections on X11 – remember that “clipboard” and “primary” are separate. If you copy with
xclipbut paste with middle-click, you may see old content. - Empty clipboard – some commands return nothing when the clipboard is empty. Check with
pbpaste | wc -corxclip -o | wc -cto see if it’s truly empty. - Permissions – on Linux,
xclipneeds access to the X display. If you run it from a cron job or a service, it may fail. UseDISPLAY=:0if needed, though that’s a security risk.
Frequently Asked Questions
How do I copy a file’s content without adding a newline?
Use cat file | tr -d '\n' | pbcopy (macOS) or cat file | tr -d '\n' | xclip -selection clipboard (Linux). The tr -d '\n' removes all newlines. If you only want to remove the final newline, use head -c -1 file | pbcopy (GNU head) or perl -pe 'chomp if eof' file | pbcopy.
Can I copy the output of a command that runs for a long time?
Yes, just pipe it. The command will run to completion before the clipboard is updated. For example, find / -name "*.log" 2>/dev/null | xclip -selection clipboard will wait until find finishes. If you want to see the output while it’s copying, you can use tee: find / -name "*.log" | tee /dev/stderr | xclip -selection clipboard.
Why does xclip not work in a script?
Usually because the script doesn’t have access to the X display. Add export DISPLAY=:0 at the top, but be aware that this exposes your X server to the script. Alternatively, use a tool like xsel which may have different behavior, or run the script from a terminal where DISPLAY is already set.
Is there a way to clear the clipboard from the command line?
Yes. On macOS, echo -n | pbcopy sets it to an empty string. On Linux, echo -n | xclip -selection clipboard does the same. On Windows PowerShell, Set-Clipboard -Value "" clears it. Some systems also have a xclip -selection clipboard -i /dev/null trick, but that may not work everywhere.
How do I copy a file’s path instead of its content?
Use realpath file.txt | pbcopy (macOS/Linux) or (Resolve-Path file.txt).Path | Set-Clipboard (PowerShell). The realpath command prints the absolute path, which you can then pipe to the clipboard tool.
Can I copy multiple lines from a command output?
Yes, the clipboard handles multiline text naturally. Just pipe the output as-is. For example, ps aux | head -20 | pbcopy copies 20 lines. When you paste, the line breaks are preserved.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Backup Not Enough Space? Fix It Now200.00 GB
- Approximate Match Lookup: Conditional Formatting
- GST on Delivery Charges in India: E-Commerce Rules
- NFC Reader on Your Phone: 9 Things You’re Not Doing
- Smart TV DNS Settings: Why They Change & How to Fix
- RabbitMQ Hosting for Startups: Managed vs Self-Hosted