Development

Copy/Pasting output from the terminal

Advertisements

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is more convenient to use commands to do so. And we can save the effort by using the built-in commands.

Mac

We can use pbcopy and pbpaste to copy and paste from the Mac terminal.

We can pipe the output of a command to copy its output to the clipboard. For example, to copy the current directory path, we can use

pwd | pbcopy
Bash

Or if we want to copy the contents of a file:

cat ~/Desktop/example.txt | pbcopy
Bash

Similarly, to paste the clipboard output in a file, we can use:

pbpaste > ~/Documents/example.txt
Bash

Windows

The commands to use on a Windows machine are clip and powershell get-clipboard.

For copying the standard output to the clipboard, we use the command line command:

<some command> | clip
Bash

For copying the current directory, we need to convert it into a command. Since  %cd% is the environment variable that stores that value, we can echo that value and pipe it into the clip command.

echo %cd% | clip
Bash

Similarly, for copying a file’s contents to the clipboard, we can use

cat example.txt | clip
Bash

To paste the clipboard into a file, we can use the powershell get-clipboard command

powershell get-clipboard > example.txt
Bash

Linux

For Linux machines, we can use the terminal commands xclip or xsel.

These need to be installed first:

sudo apt-get install xclip
sudo apt-get install xsel
Bash

Using xsel, the command for copying something is

xsel --clipboard --input
Bash

So, copy the current directory, we can use:

pwd | xsel --clipboard --input
Bash

And for copying the contents of a file to the clipboard:

cat example.txt | xsel --clipboard --input
Bash

And for pasting the clipboard contents in a file, we can use:

xsel --clipboard --output > example.txt
Bash

That is all there is to copy/pasting output from the terminal to the system clipboard. If you have any questions, let us know in the comments below.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

4 weeks ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

8 months ago
Advertisements