Copy file content to clipboard in terminal

If you are working in terminal/console most of your time, you may already know how to copy a file content to clipboard. But if not, here today we will explore how can we copy a file content to clipboard in terminal.

Installation

Arch/Manjaro

sudo pacman -S xclip

Ubuntu

sudo apt install -y xclip

Alias

We will add an alias for it to make it easier to use.

Zsh

Add it to .zshrc file in terminal.

echo "alias ccopy='xclip -sel clip <'" >> ~/.zshrc

Note: I have used ccopy as a command, you can use any. Just make sure to not use any of the current ones your system may have.

We have ccopy alias above, to use it right away without login or restart.

source ~/.zshrc

Bash

echo "alias ccopy='xclip -sel clip <'" >> ~/.bashrc

Reload .bashrc:

source ~/.bashrc

Usage

Using it is very simple.

ccopy /var/www/html/footer.php

Content of footer.php is now available in clipboard.

Let’s copy our system public ssh key to clipboard.

ccopy ~/.ssh/id_rsa.pub