For example,
Say I want to list the contents of a folder and directly paste them into a chat window for a friend to see.
I realize I could do ls > filename.txt
to create a file (filename.txt) with those contents; I'd then have to open or print the file and manually select and copy the text block (which can be annoying/tedious.) I clearly could also select and copy the output of ls
directly from within the terminal window.
It would be much faster/easier to simply pipe standard output to the clipboard.
What terminal command allows me to do this?
This can be done with either xsel or xclip command line utilities. Since neither program comes with Ubuntu by default you'll need to first install them via Ubuntu Software or the terminal. Here's how in the terminal (but remember you only need one of these two.)
Note: If you're using Ubuntu in Windows Subsystem for Linux (WSL) see this other answer instead.
Now some examples. If you want to copy the output of
ls
to the clipboard here's what you'd do:With xsel:
With xclip:
This can of course be utilized for other terminal commands as well. Let's say you want to paste your network info into a help forum.
With xsel:
With xclip:
Make this even easier with a new bash alias!
Edit your
~/.bash_aliases
file (if it doesn't exist yet create it first withtouch ~/.bash_aliases
)Then add one (depending on which program you decided to go with) of the following:
or
Then save and close.
Now (after restarting your terminal) you can send standard output to the clipboard just by piping it to 'copy' (or whatever you decide to name your new alias)
For example:
If you are attempting to copy to the clipboard using Ubuntu in Windows Subsystem for Linux (WSL) xsel or xclip will not work unless you are using X Windows as clipboard is only for graphical. However, to pipe terminal standard output to the clipboard in WSL Ubuntu you can use
clip.exe
. You can then paste into the WSL Ubuntu terminal with standard paste commands and the copied text will be available in Windows as well. For example,will copy the current working directory to the (Windows) clipboard.
This search result appears at the top when looking for ways to copy/paste text in WSL so I think it is worthwhile to mention this so readers do not needlessly install xsel or xclip in Ubuntu and instead use
clip.exe
.Found this helpful for using the
xclip
utility in addition to the answers above. (source)To Paste (from system clipboard):
Demo:
And thus I added this to my shell profile:I Found This Solution Which I Think Is Simpler & Easy To Remember:
You Can Use It To Copy Other Things Aswell:
You don't need to pass any other parameters, it copies directly to the clipboard. Hope you learnt something :D
Note: You can use the
clippaste
command to view what's currently inside the clipboard :)