How to Copy to/From Clipboard in Linux

Abid Ullah Feb 02, 2024
  1. Copy Command to/From the Clipboard in Linux
  2. Copy Text to/From the Clipboard in Linux
  3. Copy File to/From the Clipboard in Linux
How to Copy to/From Clipboard in Linux

In Windows, we are familiar with the buttons used to copy and paste, but in an operating system like Linux, the buttons or operations are different for copy and paste. If you are using Ubuntu and you want to copy contents in the terminal, you can use Ctrl+U to copy the contents and Ctrl+Y to paste the contents on the screen. This is just used to copy contents in the terminal. But this is only the case when we want to use keyboard shortcut keys to perform those operations.

Copy Command to/From the Clipboard in Linux

Now let’s focus on copying to/from the clipboard in Linux. There are multiple ways to perform these operations in Linux. You can copy a file, command, text, etc., by using xclip or xsel, into the clipboard without using a mouse.

Using programs like xclip or xsel allows you to copy anything into the clipboard and paste it into your desired destination. To use these programs, you need first to install them in utilities. You can install them by the following command.

sudo apt-get install xclip

Or,

$ sudo apt-get install xsel

After successful installation, you can proceed to use these programs.

For example, we need to follow this procedure if we want to copy the uptime command.

First type $ uptime | xclip - i. It will copy the uptime command into the clipboard.

To paste the uptime command on the screen, we need to write this command in terminal $ xclip - o.. It will paste the output of the uptime command and display it on the screen.

The same operations can also be performed using the following commands: $ uptime | xsel -i that copys the uptime command into the clipboard and $ xsel – o that pastes the output on the screen.

Copying uptime command into the clipboard

Pasting the uptime command from the clipboard on screen

Copy Text to/From the Clipboard in Linux

Suppose we need to copy some text into a clipboard. We can do that by using xclip or xsel. To copy text into a clipboard, you need to use a command like $ echo "Hello World" | xclip - i. It stores the text into a clipboard.

To paste the output, we need to use the $ xclip - o command, and it will copy the text from the clipboard and paste out the stored output Hello World on the screen.

copying text into the clipboard

pasting the text from the clipboard

Copy File to/From the Clipboard in Linux

We use the xclip-copyfile filename command to copy the file into a clipboard. This command will store the file into a clipboard.

To paste the file stored into the clipboard into your desired location, we use the xclip-pastefile command.

How to perform this operation? Following are the steps.

For example, we have a file named file.txt, and we want to copy the file into a clipboard.

First, we need to use this command, $ xclip-copyfile file.txt., that will store the file named file.txt into a clipboard.

Now we want to paste out the file from a clipboard, and we want to paste the file into the desktop location. We need to change the directory and use the cd Desktop command. It will take us to the Desktop directory.

We need to use the command $ xclip-pastefile to paste the file named file.txt into the Desktop location from where we copied the file.

copying file into the clipboard

changing directory to desktop

pasting file from the clipboard into Desktop

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn