Appearance
Sometimes when dealing with relatively chunky lists of data I've run into the issue of having to manually copy the contents of a CSV into a GUI program for further processing or presenting it nicely.
For the convenience of copying the contents of the file to the clipbord directly from the command line without making mistakes and without having to open the file in another program, it's useful to be able to know how to do this directly from the terminal.
Below how to approach this in different environments.
Windows ​
Since Windows 7 introduced the clip
command, this has been made very simple.
You can just pipe the output of a command into clip
and it'll be made available in the clipboard.
powershell
cat filename | clip
Linux ​
For Linux, it might depend on what display server you are using Wayland, or X Windows System.
Theoretically you could also copy text from a pure text mode on a TTY, but in this case a "clipboard" buffer would only be avaialble if you are using external services like gpm
(the General Purpose Mouse server) or if you are running under a screen
session.
Wayland ​
For Wayland we have the wl-copy
utility, which is part of the wl-clipboard
project.
The content to copy can be either piped to its standard input, or given as parameter.
sh
cat filename | wl-copy
X Window System ​
For X, there's xclip
, which also allows content being piped to it.
However, note that the X Window System has 3 different type of "clipboards":
- Primary Selection: holding the last selected text and pasted by pressing middle click.
- Secondary Selection: meant to be used by applications but nowadas not really used much.
- Clipboard Selection: your typical Ctrl+C / Ctrl+V buffer, which is most likely what you want.
By default xclip
will copy the content into the primary selection. We'll need to add -sel clipboard
(or -sel c
for short) so it uses the clipboard selection.
sh
cat filename | xclip -sel c
Mac OS X ​
Mac OS X offers the command pbcopy
, which works also by piping the content into it.
sh
cat filename | pbcopy