But what if I'm already SSHed into the remote machine? Can I do something like this

ssh remote
cd /some/file
scp path local:~

where local somehow resolves to the machine I SSHed from. This would of course be possible if the local machine is running an SSH server and the remote is configured to connect to it, but that isn't always an option.

I second the sftp suggestion. However I would also suggest not using really complicated & annoying paths. If they are annoying to type why type them? Name them something easier to type. Finally, I would suggest using single quotes instead of double backslashes because double slashes are harder to read. Instead of ...\\\ \\\&..., you could just do '...\ \&...'. See how much more readable that is?
–
jw013Apr 23 '14 at 20:18

This is one specific use case, and I appreciate your suggestions for alternatives but I'm really looking for an answer to my specific question.
–
MaxApr 23 '14 at 20:33

Another, rather… hacky approach would be abusing port-forwarding capabilities of SSH, combined with nc (a.k.a. netcat). nc is the "swiss army knife of networking" (the smaller one… there's also socat, which is more the MultiTool® ;) — a kind of cat with networking capabilities.

The idea: tell ssh to forward a remote port of your remote machine to a local port on the client side and use this connection to pipe along your data using nc:

Connect to $remote establishing remote port forwarding:

client:~> ssh -R 8888:localhost:8888 $remote

This establishes a normal connection to $remote, but additionally tells ssh to listen on port 8888 on the remote side. If something connects to port 8888 on the remote side, ssh will open a connection to localhost, port 8888 on the client side (thus, the client) and forward anything sent to remote-side's port 8888 to this port.

Listen on client side's port 8888 (so ssh has something to connect to), using nc. We pipe the output (client-side) nc receives into tar, since we'll send a tar stream to it later:

client:~> nc --recv-only -l -p 8888 | tar xvf -

On the remote side, change your current working directory anywhere you wanted.

If you're on the right spot, use tar to gather the files you want to copy and pipe them into (remote-side) nc, connecting to the remote-side opened port:

Note: There are at least three different implementations of nc out there, all with a slightly different syntax; --send-only and --recv-only are AFAIK only supported by netcat6. If you've an implementation that does not support those (or similar) options, you'll have to look whether tar exited and hit Ctrl-C on the sender side. nc usually waits forever and does not handle EOF (--send-only tells it to do and automatically close the connection).

Another note: You could also use nc directly, but by using ssh's port forwarding, your data goes through the SSH connection, avoiding two problems: first, your data is still sent encrypted, and second, this also works if your client is behind a NAT'ing firewall or otherwise not directly reachable from your $remote.