How to copy file content to clipboard from the command line on Linux desktop

Question: I have a relatively big text file. I would like to copy the whole file content, and paste it to somewhere else on my Linux desktop. Is it possible to copy the content of a file to clipboard without opening the file?

In X windows system, clipboard is a mechanism for transferring data from one window to another, or sharing data among different applications. Typically you use a mouse to select a text and copy it to clipboard.

However, if you want to copy the whole file content to clipboard without opening it, you can use xclip, which is a command line interface to clipboard. The xclip command can read text directly from standard input, and copy it to clipboard.

Install xclip on Linux

To install xclip on Ubuntu, Debian or Linux Mint:

$ sudo apt-get install xclip

To install xclip on Fedora or RHEL:

$ sudo yum install xclip

To install xclip on CentOS, first enable Repoforge repository, and then run:

$ sudo yum install xclip

Copy File Content to Clipboard using Xclip

To copy the content of a file to clipboard, simply run the following command.

$ cat input.txt | xclip -i

Once the file content is copied to clipboard, you can paste it into another window or application simply by clicking on mouse middle-button.

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

2 thoughts on “How to copy file content to clipboard from the command line on Linux desktop

  1. When I loaded 'xclip' only my linux mint-16 KDE workstation, I cannot get anything into the clipboard. I know that 'xclip' has been around quite some time, and I suspect that there are differences between the X-windows clipboard and the desktop-clipboard used by KDE and associated apps.
    Specifically, the man page refers to "X selection" rather than "clipboard."

    • Works fine on Debian KDE. Just make sure you use mouse middle button to paste it into other apps.

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *