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
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/path
in the terminal windowPaste the contents to the terminal window
Press press
Ctrl + D
.
Tested on ubuntu.

- 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
-
3It 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
-
@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.

- 28,246
- 16
- 81
- 118
-
3For 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

- 1,155
- 3
- 11
- 22
xsel: Can't open display: (null) : Inappropriate ioctl for device
. – rocksNwaves Aug 29 '20 at 18:50DISPLAY=:0 xsel -b
. Runecho $DISPLAY
in your X environment to know your exact DISPLAY value – Jack Feb 04 '21 at 10:13