26

I'm wondering if it possible to copy to clipboard output of pwd command.

I often need to copy some path and I have to select it with mouse, do several clicks and that is annoying for me.

What I want is piping output of pwd command to something that will copy it to clipboard so I do not have to touch my mouse

Edit: it will be great if it is possible with some built-in

micgeronimo
  • 945
  • 4
  • 11
  • 18

2 Answers2

37

You can use a program called xclip. For installing it type:

sudo apt-get install xclip

and then do

pwd | xclip -- which copies output of pwd to xclip

Now to get the output type:

xclip -o

or to copy to the "standard" clipboard (to avoid having to use xclip -o)

pwd | xclip -selection clipboard

for more options use

man xclip

Eric Leschinski
  • 2,181
  • 1
  • 20
  • 23
bolzano
  • 1,540
  • 14
  • 24
  • how can I `cd` after that? `xclip -o | cd` do not work – micgeronimo Mar 17 '15 at 10:28
  • you can do it by `cd xclip-o` but use the `` to close the xclip -o..whatever code is inside `` in the terminal will post the output of that code.it will get the output of xclip -o and then cd will change directory to it – bolzano Mar 17 '15 at 10:39
  • @micgeronimo you could use one of either [`xclip -o | xargs cd`](http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/) or `cd $(xclip -o)` – evilsoup Mar 17 '15 at 14:47
  • 1
    I suggest using `cd "\`xclip -o\`"` if there are spaces in cwd... – alessandro Apr 18 '17 at 08:30
  • This doesn't work :/ if I look at a file `cat banana.txt | xclip` it copies fine. `pwd | xclip` doesn't copy anything. – Phill Jun 12 '20 at 12:16
10

You can use xclip or xsel, to install xsel just do:

sudo apt-get install xsel

and then do

pwd | xsel -b

to copy the output of pwd to the clipboard.

To get the content of the clipboard you can use:

xsel -b

Here you can read a review of xclip and xsel:

http://trembits.blogspot.com/2009/09/clipboard-from-command-line-xclip.html

Josue Abarca
  • 216
  • 1
  • 5