Ever felt that copy-pasting using the mouse gets tiresome? Especially when you’re more comfortable using the terminal and the keyboard, switching to the mouse can be counterproductive. The xclip command will help you stay on your keyboard when you want to copy and paste content.
What Is xclip?
xclip is a command-line utility used on Linux systems with an X11 implementation to interact with the X selection (also known as the clipboard). It allows you to copy text from files or standard input and make it available for pasting in other X applications. xclip can also print the contents of the X selection to standard output. It has many useful options and use cases to explore.
Even though it sounds like xclip is only meant for systems that use the X window manager. That’s not the case, as you’ll see how I use it on aWayland system.

In this quick guide, we’ll dive deep into how you can use the xclip command to copy contents from a file, different useful options, and when it’s useful to whip out xclip. For demonstration purposes, I’ll be using Ubuntu 24.04 LTS. If you’re using a different distro, you can still follow along, but you may need to tweak some commands slightly.
When Is xclip Most Useful?
The best use case I can think of is when you want to copy large files. It can be tedious to use the mouse to keep selecting lines after lines. Quite often, long lines get cut off in some text editors, such as nano. In that case, it gets even more difficult to select and copy the file content. In such situations, xclip can be a convenient tool to use. You can copy the file content with a single command and paste it anywhere you want.
How to Get and Use xclip
xclip usually doesn’t come pre-installed on Linux distributions. So, in that case, you’ll have to install it yourself. you may either install it using yourdistro’s package manageror get the source fromits GitHub repository. For Debian, Ubuntu, and their derivatives, install it using:
If you’re using Red Hat, Fedora, or the likes, then run:

For Arch Linux and Manjaro users, use:
To confirm the installation, you can check its version by running:

The most basic way to use the xclip command is to copy the content from the standard input or a file. For example, if you want to copy a file’s content to the clipboard instead of displaying it on the terminal, simply pipe the output to the xclip command. Let me create a simple file to show you how.
First, I’ll create a test file and put some dummy text in it.

Now, to read the contents of the file and display the output on the command line, we use the cat command. We’ll do the same here, but pipe the output to the xclip command like this:
The result isn’t obvious here. That’s because we don’t have any output or anything. So, how do you know that the copy was successful? To display the current clipboard content, simply run:

If you don’t want to copy the content of a specific file and simply want to put some text in the clipboard, then the echo command is helpful. We’ll pipe the commands in the same way we did earlier.
you may also copy content from a file without piping the output. To do that, pass the file name as an argument.

There’s another way you can copy file content. Here’s the full command:
There’s a subtle difference between just passing the file name and using the -sel clip option. The -sel clip option is actually the short form of -selection clipboard. When you use this option, instead of the X window clipboard, you interact with the system clipboard. So what does that mean? If you’re using the first command (passing only the file name), you can paste the copied content using the middle button of your mouse. In the second case, you need to paste the content from the right-click context menu or by pressing Ctrl+Shift+v.

When copying content from a file, sometimes you’d want to ignore the newline character. To do that, use:
Another interesting option is the looping option. When you use this option, you basically tell xclip to load the contents of the file into the X11 clipboard, and let it be pasted a specific number of times into applications like terminals or text editors that read from the X selection before exiting. By default, the number is zero, so xclip can take infinite requests. Here’s an example:

Using xclip, you can also paste the content to a file usingredirection.
xclip doesn’t only copy the content of text files, it can also copy output from other commands. Say you want to copy the current directory content. To do so, run:

Besides all these, xclip also comes with a few more utilities such as xclip-copyfile, xclip-pastefile, and xclip-cutfile. you may use these commands to copy and move files between different directories and even machines. You can learn more about it using theman pageor -h option.
The xclip tool is wonderful for quickcopy-pasting work. If youprefer using the command lineand keyboard for most things, it can help you avoid the mouse for faster workflows. There are many moreuseful Linux commandslike this out for you to try.