How To Copy / Paste Command's Output using Xclip on Linux

February 21, 2017 | By in OPEN SOURCE TOOLS
| Reply More

Clipboard is a very useful utility in modern day operating systems. Clipboard technology uses the method of copy and paste to temporary store and transfer data between different applications or documents. If you are using GUI  based Linux operating system than copy and paste operation is seamless, however, command line users of Linux operating system or those managing remote linux servers might find it frustrating to copy and paste commands on the terminal. Especially when the command output is lengthy or they need to copy & paste the contents of whole file. Gladly, Xclip is a freeware utility which can make their life much easier. Xclip lets you place the output of any command directly to your clipboard storage; you can also place the contents of whole file into clipboard. This utility comes handy when you need to paste the output of command in Text Editors like VI, Nano, Gedit or need to paste command's results in web browsers and Email clients etc. You can find its source code on GitHub, in this article, we will learn about its installation process and usage.

Installing Xclip

Xclip is a pretty useful utility and has been around for a while now. It has been included into the repositories of all popular Linux package managers like APT-Get, Yum, Pacman. So installing this application on any Linux flavor is simple.

Installing Xclip on Ubuntu 16.04 / 16.10

Run following command on your system terminal to install Xclip on Ubuntu 16.04/16.10 or any previous versions.

sudo apt-get install xclip

xclip Install

Installing Xclip on CentOS 7 / RHEL 7 / Fedora

Following command will install this utility on your Centos  7, or RHEL 7 system. Same command can be used for any previous versions of CentOS/RHEL or Fedora operating systems.

sudo yum install xclip

The latest versions of Fedora operating systems have replaced YUM package manager with its improved version DNF. Fedora 23 users should use following command to install it.

dnf install xclip

Installing Xclip on Arch Linux

Arch Linux operating systems are gaining popularity as well, so here is Pacman (Arch Linux operating system's default package manager) command to install it.

sudo pacman install xclip

You can use following command to verify that your Linux system is equipped with Xclip utility.

whereis xclip

This command should return output as shown in the following screenshot.

xclip version

Using Xclip

Let's show some examples to demonstrate the usage of this utility.  We will be demonstrating the usage process for this tool on Ubuntu 16.10 system, but please note that same commands/instructions should work on all popular Linux distributions as well. Also note that Xclip uses the well know Linux redirection operator ( | ). It is also called "pipe" sign, the purpose of this operator is to redirect the output of one command as input for another.

a) Copying your system's hostname

In the following example, we will show how to copy the contents of "hostname" command to clipboard using xclip.

hostname | xclip

Now open any other application, e.g Gedit and paste the contents there, you will be able to see your system's hostname properly copied and pasted there.

xclip gedit

Here are some more examples of this utility; Following command will copy the contents of your system's host file to clipboard.

xclip /etc/hosts

b) Copying directory tree listing

"ls -la" is a well known linux command, it is used to list the number of files under a particular directory. You can use following xclip command to copy the contents of "ls -la" so you may be able to paste them in any other application. If directory listing is very long, this command becomes extremely helpful, as it is quite hard to browse and view all files on command line. Use xclip to copy it to Text editor for further analysis.

ls -la | xclip

c) Copying your sytem's uptime

If you are a professional system administrator, you often need to paste your system's uptime in emails or messenger conversations, If you want the clipboard to hold the contents of Linux system's uptime utility, use following command:

uptime | xclip

So far we have used the Xclip to copy and paste the command's output or any file's contents. Lets see how to utilize it to copy and move file(s) among different directories. This utility includes following three sub command which can be used to accomplish such operations with files.

  • xclip-copyfile
  • xclip-pastefile
  • xclip-cutfile

d) Copying File(s) among directories

For the sake of demonstration, let's first create a file, copy it using the above mentioned command and paste it to new directory.  Following is the command we used to create a new file.

echo "My Linoxide Test File" > Testfile

Copied this whole file to clipboad using the following command.

xclip-copyfile Testfile

We changed our current working directory and moved to an already existent directory.

cd /home/aun/Downloads

We used following command to paste the file in this directory.

xclip-pastefile

The whole process has been depicted in the following screenshot as well.  Hope you will find it pretty easy to understand and use.

xclips Process

Difference between "xclip-copyfile" and "xclip-cutfile" is that "xclip-cutfile" will remove the file after placing it in the clipboard, just like normal cut operation on any operating system.

Conclusion

In this article, we have discussed the features, installation process and usage of xclip utility. This tiny little utility is extremely helpful to copy Linux terminal command's outputs to the clipboard. For system administrators and professional users, this command is a must-have utility. The installation process is pretty simple, it hardly consumes any system resources and works flawlessly on all Linux flavors. Hope you enjoyed this article, try Xclip today, do let us know about your experience with this utility.

Filed Under : LINUX HOWTO, OPEN SOURCE TOOLS

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.