54

I want to pipe/dump the contents (esp. text) of the clipboard/Ctrl+C to a file, preferably using Bash or Perl (in order). I'd rather not use GUI applications please.

kiri
  • 28,986
rusty
  • 16,917

4 Answers4

49

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

sourav c.
  • 46,120
35

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.

d512
  • 451
29

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.

kiri
  • 28,986
1

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.

Darvesh
  • 103
Presbitero
  • 1,175
  • 3
  • 11
  • 23