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?


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.

  • 1
    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
  • 2
    @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
  • This is best!!! Thank you for saving me from NANO (editing file online) it makes the process so simple. – Mohsin Nov 3 '16 at 12:04

The console file manager Midnight Commander (mc) has a VFS system that allows SFTP connection/file managing to remote servers. It also runs in Cygwin on Windows. The default interface shows two independent panels; we could open the SFTP remote directory in one of them and transfer files to/from the other panel easily. Includes a file editor/viewer (mcedit) with syntax highlighting.

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.