36

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.

38

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

8
  • 5
    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 2: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
  • 1
    This did not work. Resulted in xsel: Can't open display: (null) : Inappropriate ioctl for device. 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
19

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.

4
  • 1
    For images you could use something like: xclip -selection clipboard -t image/png -o > "`date +%Y-%m-%d_%T`.png". Sep 10 '17 at 18:16
  • 2
    xclip worked for me with a paste of about 200k lines from a log. xsel (accepted answer) did not
    – user985366
    Nov 9 '18 at 12:16
  • While copypasting to vim took ages (didn't finished after 10 minutes), I didn't have time to blink before xclip had the job done. xsel didn't work. Dec 11 '19 at 7:58
  • xclip -o > clipboard.txt worked just fine for me May 18 '20 at 12:29
9

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.

6
  • How does this at all achieve what is asked? And how does it have any advantage over piping directly? Feb 26 at 8:35
  • 2
    It pastes the contents of the clipboard into a file using bash. That's exactly what they asked for.
    – d512
    Feb 26 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. Aug 7 at 14:21
  • Good solution if you do not want to install any extra tools.
    – Forss
    Sep 7 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. Nov 1 at 14:41
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.

2
  • after installing, gpaste still won't work! This sucks
    – Russo
    May 23 '18 at 6:44
  • It's now "gpaste-client" in Ubuntu 18.04 ... cc @Russo
    – David
    Oct 24 '18 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.