Reverse SCP over SSH connection

scpssh

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?

Best Answer

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.

Related Question