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

Last updated on June 29, 2020 by Dan Nanni

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

For Ubuntu, Debian or Linux Mint:

To install xclip on Ubuntu, Debian or Linux Mint:

$ sudo apt-get install xclip

For Fedora:

To install xclip on Fedora:

$ sudo yum install xclip

For CentOS or RHEL:

To install xclip on CentOS/RHEL, 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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean