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?

up vote 86 down vote accepted

This can be done with either xsel Install xsel or xclip Install xclip command line utilities. Since neither program comes with Ubuntu by default you'll need to first install them via Software Center 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

Now some examples. If you want to copy the output of ls to the clipboard here's what you'd do:

With xsel:

ls | xsel -ib

With xclip:

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.

With xsel:

sudo lshw -C network | xsel -ib

With xclip:

sudo lshw -C network | xclip -sel clip

Make this even easier with a new bash alias!

Edit your ~/.bash_aliases file (if it doesn't exist yet create it first with touch ~/.bash_aliases)

Then add one (depending on which program you decided to go with) of the following:

alias copy='xclip -sel clip'

or

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)

For example:

ls | copy
  • 1
    With newer Ubuntu versions, you should use apt. So sudo apt install xsel or sudo apt install xclip. – wisbucky Feb 25 '17 at 0:55
  • 1
    @jpaugh. Compare the Ubuntu package management documentation for 16.04 vs 14.04. In 16.04, it only mentions apt and not apt-get. You can still use apt-get, but they're trying to encourage people to use apt. – wisbucky Sep 5 '17 at 20:12
  • 1
    You don't have to restart the terminal; you can just source ~/.bash_aliases – Rob K Nov 14 '17 at 19:30
  • 1
    @Mohamad These are for running inside an X server (as it provides the clipboard functionality). If you don't have that, you need to do it in your terminal emulator. How to do that, depends on the actual software you are using, as there is no de-facto standard for that. – Thorbjørn Ravn Andersen Feb 26 at 12:00
  • 1
    If you also want to capture stderr (the errors that might occur with your command, besides stdout), you have to use e.g. ls nonexisting_file 2>&1 | xclip -sel clipboard, meaning you redirect stderr to stdout. – user2376000 Jun 13 at 6:37

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.