50

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,246
  • 16
  • 81
  • 118
rusty
  • 16,327

4 Answers4

45

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.
  • 44,715
  • 8
    Reminder: If you have something very important in your clipboard, don't copy and paste text from this answer as you will regret it. – benathon May 17 '19 at 22:15
  • @portforwardpodcast can you please elaborate so that we can know what/how exactly can cause a problem. – sourav c. May 18 '19 at 02:54
  • 4
    The problem occurs like this
    1. Copy something very long and important to your clipboard
    2. Accidentally close the source of this data
    3. Frantically google how to write clipboard out to a file
    4. Find this post
    5. Copy the example text
    6. You lost your original important text
    – benathon Jun 27 '19 at 22:12
  • 2
    This did not work. Resulted in xsel: Can't open display: (null) : Inappropriate ioctl for device. – rocksNwaves Aug 29 '20 at 18:50
  • @rocksNwaves I request you to tell us about what exactly you tried. As I can see it works fine for me. IMHO there might be a small misunderstanding somewhere. – sourav c. Aug 30 '20 at 16:55
  • @souravc I tried exactly what you had listed here. Word for word, step for step. – rocksNwaves Aug 30 '20 at 22:40
  • I had the same problem. Installed with the command above and used it with the second command. Did it on a Raspberry Pi with Raspbian GNU/Linux 10 (buster). Not sure if it might be related to beeing a headless server. But actually I installed something like X11 I think and I have a GUI which I can use over VNC. It also might be related to the fact that I was logged in via SSH. – bomben Jan 31 '21 at 21:36
  • @rocksNwaves Every command that works under X needs.. well.. X server running, xsel is not an exception to this. When you do ctrl+C you are doing it in a X session. Make xsel connect to that session by exporting the DISPLAY variable, for example DISPLAY=:0 xsel -b. Run echo $DISPLAY in your X environment to know your exact DISPLAY value – Jack Feb 04 '21 at 10:13
29

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
  • 391
  • How does this at all achieve what is asked? And how does it have any advantage over piping directly? – Artur Meinild Feb 26 '21 at 08:35
  • 3
    It pastes the contents of the clipboard into a file using bash. That's exactly what they asked for. – d512 Feb 26 '21 at 15:35
  • @d512 This is not a feasible solution in cases where I have a huge chunk of text, pasting of which could possibly freeze the terminal session and/or make me wait way longer till everything is printed on the terminal. Other major issue with this approach is that, if you have too many characters in a single line, it will be truncated. – Melvin Abraham Aug 07 '21 at 14:21
  • 2
    Good solution if you do not want to install any extra tools. – Forss Sep 07 '21 at 14:10
  • @MelvinAbraham: What does huge chunk of text mean here? How many bytes? At what point does this method start truncating lines? Can you suggest an alternative for systems that aren't running X? I have to do this weekly (because, despite documentation, the frapping API returns HTML instead of JSON). This method handles 4k of JSON as a single line. – Mike Sherrill 'Cat Recall' Nov 01 '21 at 14:41
  • Tested on Manjaro (Arch Linux): working perfectly. – Amir Pourmand Nov 15 '21 at 20:09
  • You may need to press CTRL-D twice for it to go through – Pro Q Apr 20 '22 at 18:25
  • Does not work for binary/image files. – FractalSpace Oct 18 '23 at 16:09
28

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,246
  • 16
  • 81
  • 118
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,155
  • 3
  • 11
  • 22