How do I copy a file to the clipboard in the terminal (linux)

Viewed 4583

I would like to copy a file to the clipboard with the terminal, like when right clicking a file and selecting "copy" in the gui. Someting like "cp test.txt" (without a destination) so whenever I want, I could right click and paste the file (or files) in the gui. Thank you

enter image description here

  1. No file in clipboard
  2. What I am trying to do: (copy a file to the clipboard with the terminal). Exactly like right clicking a file and selecting copy. (test.txt already on desktop)
  3. Now the file is in the clipboard and I can paste it anywhere
  4. File is pasted
1 Answers

What you want to achieve isn't impossible, but looks like there isn't any universal solution. The only thing I managed to find was this question: Copy file from commandline, paste into Gnome window or vice-versa.


When you want to copy and move files via the X clipboard in CLI use xclip-copyfile and xclip-pastefile.


Additionally:

xclip -i -selection clipboard < ~/foo.c

This will copy the contents of file ~/foo.c into CLIPBOARD (without -selection clipboard it will copy into PRIMARY).


Tools will be different on other systems (e.g. on Windows will be copy) or if you aren't using X server.

Related