copy to clipboard in ubuntu

How to Copy File to Clipboard in Ubuntu

Sometimes you may need to copy a file or screen content to clipboard in Ubuntu. But if you are using Linux system which does not have desktop, this can be difficult. In this article, we will learn how to copy file to clipboard in Ubuntu. We will use xclip utility for this purpose.


How to Copy File to Clipboard in Ubuntu

Here are the steps to copy file to clipboard in Ubuntu using xclip.


1. Install xclip

Open terminal and run the following command to install xclip.

$ sudo apt-get install xclip


2. Copy Single File to Clipboard

Run the following command to copy file data.txt’s contents to clipboard.

$ cat ./data.txt|xclip -i

The above command will copy the entire file data.txt’s contents to clipboard. You can paste it somewhere else using middle/right mouse button.

Alternatively, you can copy data to clipboard selection, with the following command.

$ cat ./data.txt|xclip -i -selection clipboard


3. Copy Multiple Files to Clipboard

If you want to copy multiple files’ contents to clipboard, you need to pass the list of filenames one on each line. You can do this using find or some other command. Here is a command to copy content of all .pdf files in your present working directory.

$ find ${PWD} -name "*.pdf"| xclip -i -selection clipboard -t text/uri-list

You can also add these commands to a shell script. Create an empty shell script.

$ sudo vi sample.sh

Add the following lines to it.

#!/bin/sh
xclip -i -selection clipboard -t text/uri-list

Save and close the file. Make the file executable with the following command.

$ sudo chmod +x sample.sh

Run the following command to copy all .pdf files in present working directory using the above shell script.

$ find ${PWD} -name "*.txt"| sample.sh

In this short article, we have learnt how to copy to clipboard in Ubuntu.

Also read:

How to Assign Command Output to Variable in Shell Script
How to Disable Updates in Yum/Dnf
How to Group Multiple Columns in Python Pandas
How to Access Environment Variables in Python
How to Password Protect PDF in Python

Leave a Reply

Your email address will not be published. Required fields are marked *