Copy Files Through an SSH Wormhole!

Here is a fun and useful Linux console hack you might like.

If you use ssh A LOT, maybe you encounter situations where you have a shell that is three or more hops removed your local workstation. And maybe you've been in the situation where there is some file that you need to get from "there" to "here" or vice versa. And you really are annoyed by the thought of scp-ing the file over 3+ hops (and not so lucky that all the keys and permissions in the pipeline enable easy piping through ssh).

Lucky you! In a moment of inspiration, I concocted this ultra-unixy hack to copy the file in ONE hop (more or less). Assuming the file you need is named "file.sql", do:

< file.sql bzip2 --stdout | base64 --wrap 0

This will dump a pile of base64 characters on your console, hopefully fitting all on one screen. If the file was too big for all the data to fit on one screenful, you can get extra hacky by paging it through "less" or even decreasing the font size on your console.

Now copy all the base64 characters with your nice mouse cursor, or however you usually do it. Then in the console on the other end of your ssh tunnel do:

echo "paste here" | base64 --decode | bzip2 -d --stdout > file.sql

Your file has arrived at the other end of the wormhole.

Happy hacking!