151

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?

1

5 Answers 5

186

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 to install them using 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 Snap Shot's 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:

    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
6
  • 1
    Thanks for the answer but do you know anything just for Linux terminal?
    – Mohamad
    Mar 29, 2017 at 8:15
  • 4
    You don't have to restart the terminal; you can just source ~/.bash_aliases
    – Rob K
    Nov 14, 2017 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. Feb 26, 2018 at 12:00
  • 2
    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.
    – chriad
    Jun 13, 2018 at 6:37
  • 1
    Isn't there a solution which makes use of default and already installed packages?
    – winklerrr
    Sep 18, 2019 at 8:27
21

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,

pwd | clip.exe 

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.

3
  • 1
    Thank you for this addition!
    – Insperatus
    May 7, 2020 at 4:47
  • This is exactly what I was looking to do: copy the output of terminal in WSL Ubuntu so I can paste it in Windows apps. I can confirm that this works with Ubuntu 20.04 LTS and WSL 2 on Windows 10. Just be sure to include the file name extension: clip.exe
    – Samir
    Jun 29, 2023 at 9:50
  • I never would have guessed that I can use clip, a Windows command equivalent to xclip and xsel, in a GNU/Linux context. So indeed, thanks for this addition!
    – Samir
    Jun 29, 2023 at 9:58
3

For more recent Ubuntu versions with Wayland wl-copy will do. To install it, run:

sudo apt install wl-clipboard

Example of usage:

ls | wl-copy
2

If you're using zsh with Oh My Zsh, you can use the clipcopy command to pipe the stdout output to the clipboard, as shown in the following examples:

pwd | clipcopy
ifconfig | clipcopy
echo $PATH | clipcopy
neofetch | clipcopy

You don't need to pass any other parameters, the output is copied directly to the clipboard.

Note: You can also use the clippaste command to view what's currently inside the clipboard.

0
0

Copying the path of a file in command line, without any new line after paste:

realpath filename | xclip -r -selection c

The -r switch is necessary to avoid the new line generated after pasting.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .