Copying data in tact via terminals and copy/paste - using base64

So you're logged in to two different servers, each one via it's own particular link of VPNs/secure gateways and each with their own set of firewall restrictions. All you want to do is copy a small file or patch between the two. One approach is to copy the file back to your workstation or some other shared system and then back over to the destination, another might be to set up ssh port forwards between various links.

But if the file/patch/data is small there's a really easy way using base64 (part of the GNU core utilities).

This is great also for plain text where you want to preserve the whitespace (ie. tabs where there are tabs, avoiding auto-indenting on pasting).

Example:

Generate the base64 version of some data:

bjdean@host1.example.org:~$ base64 test
R05VIGNvcmUgdXRpbHMgLQp1c2luZyBiYXNlNjQgdG8gY29weSBkYXRhIHZpYSBjb3B5LXBhc3Rl
Lgo=

Copy and paste that data into a file (eg. using cat> or a text editor), and get the content back with base64 -d:

bjdean@host2.example.org:~$ cat >test
R05VIGNvcmUgdXRpbHMgLQp1c2luZyBiYXNlNjQgdG8gY29weSBkYXRhIHZpYSBjb3B5LXBhc3Rl
Lgo=
^D

bjdean@host2.example.org:~$ base64 -d test
GNU core utils -
using base64 to copy data via copy-paste.

This is a very handy way to try out patches for code before committing code into a repository:

bjdean@host1.example.org:~$ diff -u test test2 | base64
LS0tIHRlc3QJMjAxNS0wNy0xMCAxNTo1MzoxNi45MjA3OTExODQgKzEwMDAKKysrIHRlc3QyCTIw
MTUtMDctMTAgMTY6MDA6NTEuMTkyNzgxMjE3ICsxMDAwCkBAIC0xLDIgKzEsMyBAQAogR05VIGNv
cmUgdXRpbHMgLQogdXNpbmcgYmFzZTY0IHRvIGNvcHkgZGF0YSB2aWEgY29weS1wYXN0ZS4KK0Fs
c28gaGFuZHkgZm9yIHBhdGNoaW5nIGZpbGVzLgo=

bjdean@host2.example.org:~$ cat | base64 -d
LS0tIHRlc3QJMjAxNS0wNy0xMCAxNTo1MzoxNi45MjA3OTExODQgKzEwMDAKKysrIHRlc3QyCTIw
MTUtMDctMTAgMTY6MDA6NTEuMTkyNzgxMjE3ICsxMDAwCkBAIC0xLDIgKzEsMyBAQAogR05VIGNv
cmUgdXRpbHMgLQogdXNpbmcgYmFzZTY0IHRvIGNvcHkgZGF0YSB2aWEgY29weS1wYXN0ZS4KK0Fs
c28gaGFuZHkgZm9yIHBhdGNoaW5nIGZpbGVzLgo=
^D
--- test        2015-07-10 15:53:16.920791184 +1000
+++ test2       2015-07-10 16:00:51.192781217 +1000
@@ -1,2 +1,3 @@
 GNU core utils -
 using base64 to copy data via copy-paste.
+Also handy for patching files.

bjdean@host2.example.org:~$ cat | base64 -d | patch
patching file test

bjdean@host2.example.org:~$ cat test
GNU core utils -
using base64 to copy data via copy-paste.
Also handy for patching files.

BradsWiki: Programming Notes/Command-Line One-Liners (last edited 2015-07-10 06:03:56 by BradleyDean)