-1

I have a very simple script aliased in my .bashrc on a remote host that I frequently SSH into for work. It enables me to enter "pth" as a command, and that aliases to "bash /user/your/home/pathprinter.sh", which is as follows:

#!/bin/bash

NOWDIR=`pwd`
echo 'scp  myname@remote.host.institution.name:'"$NOWDIR" 

This enables me to fairly quickly scp a file from my local machine to the remote host or vice versa without having to type very much, because it echos the output, which I then click and drag to highlight, then copy and paste into another terminal window on my local machine.

If this script were stored on my local machine, I could write:

NOWDIR=`pwd`
echo 'scp  myname@remote.host.institution.name:'"$NOWDIR" | pbcopy

and it would go straight to the clipboard. But if I most frequently am on the remote host when executing this command, and if I do not have sudo powers on remote host (I can still install things on a home directory or elsewhere, however) are there still ways to accomplish this? Anything to save a few seconds!

Vincent Laufer
  • 705
  • 10
  • 26
  • 1
    I think it only works if you forward X11, then pbcopy could also run remote. I dont think there is another option (could not find a ANSI escape code to set the clipboard). – eckes Oct 17 '15 at 21:39
  • thank you. i will look into X11 forwarding. – Vincent Laufer Oct 18 '15 at 00:12

1 Answers1

1

Instead of echoing the command, the script could run it, so you do not need to copy paste anything.

You can have two scripts, one to copy from remote to local

#!/bin/bash
remote_path="${1:?Missing remote path}"
local_path="${2:?Missing local path}"
scp "myname@remote.host.institution.name:$remote_path" "$local_path"

and another to copy from local to remote

#!/bin/bash
local_path="${1:?Missing local path}"
remote_path="${2:?Missing remote path}"
scp "$local_path" "myname@remote.host.institution.name:$remote_path"

You can execute them as

copy-remote-to-local <path copied from the other terminal> .
copy-local-to-remote file.txt <whatever path you want on remote system>

Or, even better, configure ssh to have a short name for that host

Host serv
HostName remote.host.institution.name
User myname

and then you can use directly scp like this

scp serv:<remote path> <local path>
scp <local path> serv:<remote path>

With this, if you have public key login with that host, the remote path will be completed when you press tab, just as if it was a local path!

Alvaro Gutierrez Perez
  • 3,669
  • 1
  • 16
  • 24