How to Copy the contents of a file into the clipboard without displaying its contents

While working on the terminal, we copy a lot of text from the file content to the clipboard and then paste the content where it needs to be pasted.

If I ask you how do you copy the content of the file from your terminal to your clipboard, you may say that I usually display the content of the file using the cat command and then I select the text that I want to copy.

Absolutely correct, and nothing is wrong with the approach, but why make an unnecessary effort when you can use the xclip command to copy the content of a file directly into the clipboard?

If you don’t know what xclip is and how to use it, then this article can be very handy for your future use.

Install xclip on your Linux distributions

To get started with the xclip command, first of all, you need to install it on your Linux distribution by executing the below command as per your available distribution.

$ sudo apt install xclip                      [Ubunut/Debian]
$ sudo dnf -y install xclip                   [RHEL/Fedora/AlmaLinux]
$ sudo pacman install xclip                   [ArchLinux/ManjaroLinux/GarudaLinux]

Once the installation is complete, you follow the below section to learn the usage of the xclip command.

Copy the content of the file using xclip

There are multiple ways through which you can save file content to your clipboard, and one of them is to use the redirection operator, and the second is to use the (|) pipe operator to get data from the cat command and save it directly to the clipboard without displaying file content on a terminal.

Copy the content of the file using the redirection operator

First, get a glimpse of how you can use the redirection operator to copy file content to the clipboard. For example, suppose you have stored a sample file in your current directory and want to copy the file content to the clipboard, then run the below command.

$ xclip -selection clipboard < sample 

The output of the above command:

Copy the content of the file using xclip

The above command will fetch the data from the “sample” file and put it into your clipboard for you, without opening a file in the terminal, and for paste, you can use your regular shortcut.

You can also use a shorter version of the above command.

$ xclip -sel clip < sample 
$ xclip -se cl < sample 
$ xclip -se c < sample 

Copy the content of the file using the pipe operator

An alternative to the above command is to use the pipe operator, which will also do the same work. I think the below command is self-explanatory, so I don’t think you need any explanation for it.

$ cat sample | xclip -selection clipboard

The output of the above command:

Copy content of the file using pipe operator
Copy content of the file using pipe operator

Copy command output to the clipboard using pipe and xclip

The problem with the previous command was that it put the whole content to the clipboard, but what to do when you just want to copy a specific line of the file content to your clipboard?

In that case, you can use tail, head, sed, awk, and grep commands to filter your request and copy the text content to the clipboard.

For example, you have multiple lines of text stored in your “sample” file, and you want to copy the last line to the clipboard, so to do that you can use tail, which will read the content from the bottom.

$ tail -1 sample | xclip -selection clipboard

The output of the above command:

Copy command output to the clipboard using pipe and xclip
Copy command output to the clipboard using pipe and xclip

You can use the same command with some tweak to copy a string from the echo command:

$ echo -n "i'm_in_your_clipboard" | xclip -selection clipboard

Clear clipboard data in Linux using xclip

By using the above command, you have learned how to get data into the clipboard, but how do I clear out or delete data that is stored in the clipboard? Simple. Just invoke the below command, which will clear out the data that your clipboard is holding.

$ xclip -selection clip < /dev/null 

[Bonus] Add a basic function to your.bashrc file to copy file content to the clipboard

Above, we have discussed how we can use the xclip command to copy the content to the clipboard. While using it, I found it useless to type the whole command, again and again, to copy the output to the clipboard.

As a result, I added a basic function to .bashrc which automatically puts file content to the clipboard.

xcp()
{
  get_content="$1"
  if [[ -f $get_content ]]; then
     xclip -selection c <  $get_content 
  else
    printf '%s\n' "You forget to pass file, or provided invalid file" >&2
  fi
}

So whenever I want to copy data from a specific file, I just run the below syntax in the terminal.

$ xcp [FILE-PATH]

For example, if I want to copy operating system information from the /etc/os-release, then I’ll execute the below code:

$ xcp /etc/os-release

Wrap up

That’s all for now!

In this article, you have learned how to copy string or command output to a clipboard without displaying it to your terminal for easy access.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments