How to pipe/dump clipboard contents to a file?

How to pipe clipboard contents to a file?

You can do it using xsel. Type in terminal to install it,

sudo apt-get install xsel

To put the contents of a file to the clipboard use: xsel -b < some.txt

To paste the contents of the clipboard to a file use.

`xsel -b >> some.txt`

Copy file content/string to clipboard

You can go through this answer by Radu Rădeanu which described how you can copy file content/string from a terminal to clipboard that can be pasted using Ctrl+V


You can also use xclip (install with sudo apt-get install xclip) like so:

xclip -selection clipboard -o > clipboard.txt

which will put the clipboard into clipboard.txt in the working folder.


Here's a way that is done from the command line and does not require any libraries:

  1. Copy your data to the clipboard.

  2. Run cat > /your/file/path in the terminal window

  3. Paste the contents to the terminal window

  4. Press press Ctrl + D.

Tested on ubuntu.


An other option is gpaste which has the advantage of being able to get several previous clipboard copies.

Install it by

sudo apt-get install gpaste

And you can recover the last copy with

gpaste-client get 0 > file.txt

Note that you can change the 0 to any number to get the other copies.