Open In App

How To Copy Command Output To Linux Clipboard Directly

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • In the first step we need to install this program in Linux to do so we will use the following command:
$ 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:

  •  Open nano editor or some other editor of your choice type in some text and save it.
$ nano testfile

 

 

  •  Now with the cat command, we can see the content of the file as follows.
$ cat testfile

 

  •  Use the xclip command to copy the file text to the clipboard as follows:
$ cat testfile | xclip

 

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

  •  Now to see test whether it is copied or not simply paste the clipboard on the terminal.

 

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:

  • To copy sort command output we are going to use the xclip parameter called -selection clipboard we are specifying the output to be copied into the clipboard so that we can paste it anywhere.
$ command | xclip -selection clipboard
  • Replace the command with the sort command like this:
$ sort -n -k 3. -k 2 test.txt | xclip -selection clipboard

 

  • Now you can paste it with the keyboard shortcut SHIFT+ CTRL+V. for the terminal and simple CTRL+V to paste it in the GUI applications like a text editor.

 

3. How to paste the output to GUI applications?

  • we can follow the above approach simply as you can see in the above output that we have pasted the clipboard into our text editor without using a mouse, so this step is similar to the above one just use the command like the following:- 
$ cat test | xclip -selection clipboard
  • With this being done you can go to any of your GUI applications and paste the output of the command cat test.

 

4. Using xclip in shell scripts:

  • Using xclip in a shell script is something that you might think is difficult but it’s not all you need to do is just write the command within the bash script. we can take an example by using the following command :
$ echo "hello your script is running" xclip --selection clipboard
  • The above command will simply copy the string that is in quotes to your clipboard so let’s try to write it in a script.
  • First, create a file with extension .sh like bash.sh.
  • Then write the above command with a line addition that will specify the bash script.

 

#!/bin/bash
echo "hello your script is running" xclip --selection clipboard
  • Now we are good to go just run this script in your terminal and the string will be copied to your clipboard. to run the bash file you need to modify the permissions first and follow up these commands.
$ chmod +x bash.sh
$ bash bash.sh

 

Conclusion:

  • Now if you want to know more about this command you can always go to its MAN page with the command 
$ man xclip
  • it’s going to show you all the details about all the options available for you to perform with xclip command.

 


Last Updated : 04 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads