2

I have two terminal windows with bash. One is local on the client computer, another one has an SSH-session on the server. On the server, I am in a directory and seeing a file I would like to copy to my client using scp from the client.

On the server I see:

user@server:/path$ ls filename
filename

I can now type scp in the client shell, select and copy the user@server:/path from the server shell and paste to the client shell, then type slash and copy and paste the filename and append a dot to get:

user@client:~$ scp user@server:/path/filename .

to scp a file from the server to the client.

Now I am searching for a command on the server, that would work like this:

user@server:/path$ special_ls filename
user@server:/path/filename

which would give me the complete scp-ready string to copy&paste to the client shell.

Something in the form

echo $USER@$HOSTNAME:${pwd}/$filename

working with relative/absolute paths.

Is there any such command/switch combination or do I have to hack it myself?

Thank you very much.

eumiro
  • 983

1 Answers1

4

A quick snippet to print such output for pasting:

#!/bin/sh
SCPUSER="myuser"
SERVER="myserver"
while [ $# -ne 0 ]; do
    printf '%s@%s:%s\n' "${SCPUSER}" "${SERVER}" "$(readlink -f "${1}")"
    shift
done

The while; shift construction enables you to give multiple arguments which will be printed, separated by newlines.

This is straightforward; the "trick" above that you are probably looking for is probably just readlink.

You could also e.g. set up either ssh-agent or passwordless login via keys (if it is a local server and security is not too strict for this) to wrap scp server side to avoid the pasting step and directly scp files from the server.