Open In App

How To Copy Command Output To Linux Clipboard Directly

To copy commands outputs directly to Linux Clipboard we will be using a Program called xclip. xclip is a program that allows us to clip-> copy/crop ->cut and external reference or a block to a specific area. xclip reads text from standard inputs or files and make it available to other application for passing an X section. It reads from all files specified, or from standard input if no files are specified. xclip has many parameters like -copyfile, -cutfile, -pastefile, etc that are used to redirect the outputs of input or command or files to the specified location or area.

1. How to Use xclip for clipboard copy and paste:

$ sudo apt install xclip

 

After installing this program we are all set to use it to copy text or files to the clipboard directly.



usage:

$ nano testfile

 

 



$ cat testfile

 

$ cat testfile | xclip

 

By using a pipe we can copy the content of the file to the clipboard.

 

This is one way of doing this, there are many other programs and utilities which allow you to copy files/ outputs to a clipboard.

2. How to copy sort command output into the clipboard without using the Mouse in Linux:

$ command | xclip -selection clipboard
$ sort -n -k 3. -k 2 test.txt | xclip -selection clipboard

 

 

3. How to paste the output to GUI applications?

$ cat test | xclip -selection clipboard

 

4. Using xclip in shell scripts:

$ echo "hello your script is running" xclip --selection clipboard

 

#!/bin/bash
echo "hello your script is running" xclip --selection clipboard
$ chmod +x bash.sh
$ bash bash.sh

 

Conclusion:

$ man xclip

 

Article Tags :