This can be done with either
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.)
sudo apt install xsel
sudo apt install xclip
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:
ls | xsel -ib
ls | xclip -sel clip
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.
sudo lshw -C network | xsel -ib
sudo lshw -C network | xclip -sel clip
Make this even easier with a new bash alias!
~/.bash_aliases file (if it doesn't exist yet create it first with
Then add one (depending on which program you decided to go with) of the following:
alias copy='xclip -sel clip'
alias copy='xsel -ib'
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)
ls | copy