28

To solve this problem I always have to use scp or rsync to copy the file into my local computer to open the file and simply copy the contents of the text file into my local clipboard. I was just wondering if there is a more clever way to do this without having the need of copying the file.

3
  • I changed the title a bit, because I kept the reading "the local clipboard of a file" and wondering since when files had clipboards. – Anthon Jun 18 '15 at 17:48
  • Thats fine thanks @Anthon English is not my first language so sometimes I need some help with it thanks. – VaTo Jun 18 '15 at 17:55
  • When all else fails (e.g. you are going through jumphosts), just one other option: use nl catalina.out which prefixes each line with numbers so you can be sure you don't miss any out with mouse highlighting to copy and paste. – Sridhar Sarnobat Jun 22 '20 at 21:22
29

Of course you have to read the file, but you could

</dev/null ssh USER@REMOTE "cat file" | xclip -i

though that still means to open a ssh connection and copy the contents of the file. But finally you don't see anything of it anymore ;)

And if you are connecting from an OS X computer you use pbcopy instead:

</dev/null ssh USER@REMOTE "cat file" | pbcopy

PS: Instead of </dev/null you can use ssh -n but I don't like expressing things in terms of software options, where I can use the system to get the same.

PPS: The </dev/null pattern for ssh is extremely usefull for loops

printf %s\\n '-l user host1' '-l user host2' | while read c
do </dev/null ssh $u "ip address; hostname; id"
done
3
  • This is great, I though this wasn't possible. Although I had to use pbcopy because I'm connecting from a mac computer to a CentOS box. I just had to change xclip to pbcopy and it worked. Pretty nice! – VaTo Jun 18 '15 at 17:50
  • -e none would only be need to prevent the special handling of ~ from the keyboard (not from the output of commands) in interactive sessions only. It's not needed here. – Stéphane Chazelas Aug 29 '17 at 10:02
  • @StéphaneChazelas you are right. Today I would use </dev/null ssh USER@REMOTE "cat file" Though this </dev/null is not needed, it might be usefull in loops, or in background operations. There is also the option -n, but if you don't go interactive, you should use such a </dev/null pattern! – ikrabbe Sep 1 '20 at 18:02
0

Can I assume you are running the X Window System and some window manager (KDE/gnome/etc.)? There are a number of terminal applications (Konsole for example) that have a built in menu that allows copy/paste functions. So you could:

  1. user@machine:~$ ssh root@172.x.x.x
  2. open the file on the remote machine
  3. highlight the contents of the file with mouse and select copy from the edit menu on your local machine.

Or did I misunderstand your real needs?

2
  • 3
    I could do this with small files but with big files if I try to scroll up or down it goes off of the file (you can try it for yourself to see that this approach not always work.) – VaTo Jun 18 '15 at 17:53
  • with big files, you can take more time on copy than rewrite the script manually in the local editor xD – Evhz Aug 16 '18 at 13:03
0

Using Putty as your SSH client, ensure you max out lines of scrollback in your configuration. When you connect, you can clear screen and scrollback and then cat the file and when I right click on the title bar, I can select "copy all to clipboard". Then I can paste into an editor on my local machine.

0

Far Manager Linux port supports synchronizing clipboard between local and remote host. You just open local far2l, do "ssh somehost" inside, run remote far2l in that ssh session and get remote far2l working with your local clipboard.

It supportes Linux, *BSD and OS X; I made a special putty build to utilize this functionality from windows also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.