Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Copy/Pasting output from the terminal
saransh kataria
saransh kataria

Posted on • Originally published atwisdomgeek.com

     

Copy/Pasting output from the terminal

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

pwd | pbcopy
Enter fullscreen modeExit fullscreen mode

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

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

cat ~/Desktop/example.txt | pbcopy
Enter fullscreen modeExit fullscreen mode

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

pbpaste > ~/Documents/example.txt
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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

cat example.txt | clip
Enter fullscreen modeExit fullscreen mode

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

powershell get-clipboard > example.txt
Enter fullscreen modeExit fullscreen mode

Linux

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

These need to be installed first:

sudo apt-get install xclipsudo apt-get install xsel
Enter fullscreen modeExit fullscreen mode

Using xsel, the command for copying something is

xsel --clipboard --input
Enter fullscreen modeExit fullscreen mode

So, copy the current directory, we can use:

pwd | xsel --clipboard --input
Enter fullscreen modeExit fullscreen mode

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

cat example.txt | xsel --clipboard --input
Enter fullscreen modeExit fullscreen mode

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

xsel --clipboard --output > example.txt
Enter fullscreen modeExit fullscreen mode

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.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A curious full stack developer who loves challenges and getting things done.
  • Location
    Seattle
  • Work
    Senior Software Developer
  • Joined

More fromsaransh kataria

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp