Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I pretty often need some file from some server when I'm on my laptop. But if I don't know where that file is, I have to ssh into the server, look around, exit, and then scp server:file .. If I'm working with my desktop and my server, both of which have static IPs, I can just SCP the file in reverse (scp desktop:~ file), but I can't do that for my laptop. Is there any nice way to SCP a file backwards over an SSH connection? So that the computer I connect to with SSH sends a file backwards to the client?

share|improve this question
Why not use mDNS? – Ignacio Vazquez-Abrams Mar 19 '11 at 4:52
I believe that this question has already been correctly answered here: – shellster Jan 27 '12 at 13:40
up vote 14 down vote accepted

This would be a lot easier with SFTP, which is an extension to SSH that supports more complex file operations than SCP. Virtually all modern Unix and Linux distributions support it. To use it, just run this command to connect to the server:

sftp server

Then you can use the ls and cd commands to browse around and find the file you're looking for. Once you've found it, use the get command to download it. For instance, to download file.txt in your current working directory on the server to your current working directory on your local machine, just run:

get file.txt

To download /home/pavpanchekha/textfiles/file.txt on the server to ~/textfiles/ on your local machine, instead run:

get /home/pavpanchekha/textfiles/file.txt ~/textfiles/

Conversely, you can also upload files in this manner. To upload file.txt from your local current working directory to the current working directory of the server, type:

put file.txt

You can also use full paths for each like you can with get:

put ~/textfiles/file.txt /home/pavpanchekha/textfiles/file.txt

For a full list of available SFTP commands, just run help at the sftp> prompt.

share|improve this answer
This is pretty cool, thanks. Is there any way to launch one in reverse if I'm already in an SSH connection? Otherwise I find myself ssh'ing somewhere, realizing that I want to transfer a file, quitting the SSH connection, making an SFTP connection, quitting that, and going back to SSH. – pavpanchekha Mar 20 '11 at 7:10
@pav: The SSH_CLIENT environment variable contains the IP address of the connecting host among other information. You can use that to create a shell alias to easily SFTP back to the originating host. Just add this line to ~/.bashrc on the server: alias sftpback="sftp $(echo $SSH_CLIENT | awk '{print $1}')" Then disconnect and reconnect from SSH so the change takes effect, or run that as if it were a command so it takes effect immediately. From there on, you can just run sftpback to SFTP back to the client you're connecting from, regardless of which one it is. – Patches Mar 20 '11 at 8:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .