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

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

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.


A quick snippet to print such output for pasting:

while [ $# -ne 0 ]; do
    printf '%s@%s:%s\n' "${SCPUSER}" "${SERVER}" "$(readlink -f "${1}")"

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.

| improve this answer | |
  • Thank you, readlink is a very good hint, although the rest is hacking. If nobody comes with some already-built-in solution (without need of copying this script to any server), I will accept this answer. – eumiro Jun 11 '12 at 8:48

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.