Home Linux Copy File Contents Into Clipboard Without Displaying Them
Copy File Contents Into Clipboard Without Displaying Them In Linux

Copy File Contents Into Clipboard Without Displaying Them

By sk
1438 Views

This guide explains what is Clipboard, and how to copy file contents into Clipboard without displaying the contents of the file using any text viewer applications in Linux.

What is Clipboard?

You will definitely cut or copy and paste texts on your system multiple times a day. You may not have remembered how many times you copied something or haven't ever thought about where the copied texts are actually stored. But, you should have copied/cut texts so many times. For those wondering, there is temporary place called "Clipboard" in an operating system. Clipboard is the place where the copied/cut data are kept temporarily.

Clipboard is a buffer used for short-term data storage. It is mainly used to transfer data within and between applications, via cut, copy and paste operations. Clipboard is usually temporary and unnamed place that resides in your Computer's RAM.

The clipboards are called "Selections" and there are three types of clipboards available in X11 window system in Linux. They are:

  • PRIMARY - This is normally used when copy/paste data using Mouse middle button.
  • SECONDARY - It is not used used much, but exists.
  • CLIPBOARD - This is used for explicit copy/paste commands via Keyboard using ctrl+c and ctrl+v keys and via menu items.

There are many tools exists to manipulate the contents of clipboards. They are known as clipboard mangers and monitors. In this guide, we will discuss two command line tools namely Xclip and Xsel that are used to access clipboard contents.

Now let us get back to our main topic. How do you copy the contents of a file without actually displaying the file contents using any external applications like nano, vi editors or commands like cat? Before I know this method, I usually open the file or display the contents of the file in standard output and then copy its contents using Mouse or Ctrl+c keys from the Keyboard. But you can do this without displaying the contents. Read on to know how.

Please note that xclip and xclip are X11 utlities. They will only work on systems that has X window system installed.

Copy file contents into Clipboard without displaying its contents, using Xclip and Xsel programs in Linux

Make sure you have installed Xclip and Xsel programs on your Linux system. They both are available in the default repositories of most Linux distributions.

To install xclip and xsel on Arch Linux and its derivatives, run:

$ sudo pacman xclip xsel

On Fedora:

$ sudo dnf xclip xsel

On Debian, Ubuntu, Linux Mint:

$ sudo apt install xclip xsel

Now let us see how to copy a file contents using Xclip and Xsel programs. For the purpose of this guide, I use text file named ostechnix.txt and the contents of this file is given below:

$ cat ostechnix.txt 
Linux is not an operating system.
It is a kernel.
GNU/Linux is the operating system.

To copy the contents of ostechnix.txt file into clipboard, without displaying its contents, run:

$ xclip -selection clipboard < ostechnix.txt

Or shortly use this:

$ xclip -sel c < ostechnix.txt
Copy file Contents into Clipboard without displaying them using Xclip in Linux
Copy file Contents into Clipboard without displaying them using Xclip in Linux

Xclip has now copied the contents of ostechnix.txt file to the clipboard. You can paste the copied data to anywhere using Ctrl+p keys or selecting the paste option from menu items or right click context menu.

You can also copy the contents of a file into clipboard using Xsel command like below:

$ xsel --clipboard < ostechnix.txt

Or, shortly use this:

$ xsel -b < ostechnix.txt
Copy file contents into Clipboard without displaying them using Xsel in Linux
Copy file contents into Clipboard without displaying them using Xsel in Linux

To save a few strokes, you can create a script named "send2clip" with the following lines:

#! /bin/bash
xclip -selection clipboard -i [email protected]

Use any name of your choice for this script. Then make the script executable:

$ chmod +x send2clip 

Now pass any file as an argument to copy its contents to clipboard. For example, the following command will copy the contents of ostechnix.txt file:

$ ./send2clip ostechnix.txt

Copy Linux and Unix commands output to clipboard

Not just the output of files, you can also send the output of any Linux and Unix commands to clipboard using Xclip and Xsel programs.

To copy a Linux command's output into clipboard using Xclip and Xsel, run:

$ command_name | xclip -sel c
$ command_name | xsel -b

Example:

Copy Linux and Unix commands output to clipboard
Copy Linux and Unix commands output into clipboard

The above commands will copy your Linux system Kernel details into clipboard.

To learn more about Xclip and Xsel commands, refer the man pages.

$ man xclip
$ man xsel

Hope this helps.

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More