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.
4 Answers
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
-
6Reminder: 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
-
4The 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
-
1This 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
Here's a way that is done from the command line and does not require any libraries:
Copy your data to the clipboard.
Run
cat > /your/file/pathin the terminal windowPaste the contents to the terminal window
Press press
Ctrl + D.
Tested on ubuntu.
- 361
- 3
- 4
-
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
-
2It 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
-
1
-
@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
-
-
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.
- 27,676
- 16
- 81
- 117
-
2For images you could use something like: ``xclip -selection clipboard -t image/png -o > "`date +%Y-%m-%d_%T`.png"``. – Pablo Bianchi Sep 10 '17 at 18:16
-
2xclip worked for me with a paste of about 200k lines from a log. xsel (accepted answer) did not – user985366 Nov 09 '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. – Skippy le Grand Gourou Dec 11 '19 at 07:58
-
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.
- 103
- 4
- 1,145
- 3
- 11
- 21