This post was first published, in a different form, at Opensource.com and appears here via a CC-BY-SA 4.0 license.
How do you usually copy all or part of a text file when working on the Linux desktop? Chances are you open the file in a text editor, select all or just the text you want to copy, and paste it somewhere else.
That works. But you can do the job a bit more efficiently at the command line using the xclip utility. xclip provides a conduit between commands you run in a terminal window and the clipboard in a Linux graphical desktop environment.
xclip isn’t standard kit with many Linux distributions. To see if it’s installed on your computer, open a terminal window and type which xclip. If that command returns output like /usr/bin/xclip, then you’re ready to go. Otherwise, you need to install xclip.
To do that, use your distribution’s package manager. Or, if you’re adventurous, grab the source code from GitHub and compile it yourself.
Doing the Basics
Let’s say you want to copy the contents of a file to the clipboard. There are two ways to do that with xclip. Type either:
xclip -sel clip file_name
What’s the difference between the two commands (aside from the second one being longer)? The first command works if you use the middle button on the mouse to paste text. However, not everyone does. Many people are conditioned to use a right-click menu or to press Ctrl+V to paste text. If you’re one of those people (I am!), using the -sel clip option ensures you can paste what you want to paste.
Using xclip with Other Applications
Copying the contents of a file directly to the clipboard is a neat parlor trick. Chances are, you won’t be doing that very often. There are other ways you can use xclip, and those involve pairing it with another command-line application.
That pairing is done with a pipe (|). The pipe redirects the output of one command line application to another. Doing that opens several possibilities. Let’s take a look at three of them.
Say you’re a system administrator and you need to copy the last 30 lines of a log file into a bug report. Opening the file in a text editor, scrolling down to the end, and copying and pasting is a bit of work. Why not use xclip and the tail utility to quickly and easily do the deed? Run this command to copy those last 30 lines:
tail -n 30 logfile.log | xclip -sel clip
When my writing needs to go into one content management system (CMS) or another for publishing on the web, I rarely (if ever) use a CMS’s WYSIWYG editor to write. I prefer to write offline in plain text formatted with Markdown. That said, many of those editors have an HTML mode. By using the command below, I can convert a Markdown-formatted file to HTML using Pandoc and copy it to the clipboard in one fell swoop:
pandoc -t html file.md | xclip -sel clip
From there, I paste away.
There was a time when I needed to generate the HTTPS certificates for a couple of my websites using a tool called Certbot. And once that was done, I needed to copy the certificate for each site whenever I had to renew it. Combining the cat command and xclip is faster and more efficient than using an editor. For example:
cat /etc/letsencrypt/live/website/fullchain.pem | xclip -sel clip
Is that all you can do with xclip? Definitely not. I’m sure you can find more uses to fit your needs.
Not everyone will use xclip. That’s fine. It is, however, one of those little utilities that really comes in handy when you need it. And, as I’ve discovered on a few occasions, you don’t know when you’ll need it until you do. When that time comes, you’ll be glad xclip is there.