xclip Command Examples in Linux

Linux provides a command, xclip, that connects X selections to stdin and stdout. You can therefore insert copy and paste operations into pipelines and other combined commands. For example, you may have copied text into an application like this:

  • Run a Linux command and redirect its output to a file.
  • View the file.
  • Use your mouse to copy the file’s content to the clipboard.
  • Paste the content into another application.

With xclip, you can shorten the process considerably:

  • Pipe a Linux command’s output to xclip.
  • Paste the content into another application.

xclip Command Examples

1. Copy the output from a command to the X11 primary selection area (clipboard):

# echo 123 | xclip

2. Copy the output from a command to a given X11 selection area:

# echo 123 | xclip -selection primary|secondary|clipboard

3. Copy the output from a command to the system clipboard, using short notation:

# echo 123 | xclip -sel clip

4. Copy the contents of a file into the system clipboard:

# xclip -sel clip input_file.txt

5. Copy the contents of a PNG into the system clipboard (can be pasted in other programs correctly):

# xclip -sel clip -t image/png input_file.png

6. Copy the user input in the console into the system clipboard:

# xclip -i

7. Paste the contents of the X11 primary selection area to the console:

# xclip -o

8. Paste the contents of the system clipboard to the console:

# xclip -o -sel clip
Related Post