Using uuencode/decode for inline file transfer during terminal sessions

Uuencode as a perl one-liner

perl -ple  'BEGIN{use File::Basename;$/=undef;$sn=basename($ARGV[0]);} $_= "begin 600 $sn\n".(pack 'u', $_)."`\nend" if $_' /some/file/to_encode.gz


perl -ple  'BEGIN{$/=undef;$s=`basename "$ARGV[0]"`;chop $s;} $_="begin 600 $s\n".(pack 'u', $_)."`\nend" if $_' /some/file/to_encode.gz

Alternatively you can just download this shell scripts which contains the perl one-liner: uuencode
A "nicer" version of that uuencode perl script can be found here, uuencode written in perl.

This is very useful if you want to copy/paste small binary files between servers via a terminal session. You copy the encoded ascii into a text editor and save it (as e.g encoded.uu). Now run
uudecode encoded.uu
and you get you binary file back. It's in the current working directory. uudecode/uuencode are part of the sharutils package in most linux distributions. The perl one-liner is useful on a remote server where you don't have rights to install sharutils and you want to copy/paste a binary file to your local machine or another server where uudecode is available. There is as well a script.

Uudecode the data from a terminal log

Here is another script that you can use to extract files from a terminal session log. In linux you can log your terminal session quite easily to file by using the script command before you ssh to some other place. In Windows you can use putty and that has as well a log function.
script -f log.txt
ssh ...WhatEver...
Now you have a log file that contains all the printouts from the remote site. If you run uuencode on the remote machine then you have the encoded data in log.txt. Now take the perl script and run it on the log file. It will prompt you for all the uuencoded files found in the log and ask which one to decode. Those uuencoded section may have other data such a shell commands in-between.

This method works even for files of several megabyte where copy/paste would be too complicated. It's as well much easier and faster.


Both scripts are under 500 bytes and depend on nothing else than perl itself. You can just copy/paste them and use them via any terminal connection.


You can use the linux command zssh or a terminal that supports the zmodem protocol such as SecureCRT. On the target system you need to have the package lrzsz installed. This package gives you two commands: rz and sz. To send a file from the remote system to your computer you use "sz file.whatvever" and to receive a file from the remote end you use the command rz.
Note that rz/sz use a binary transfer mode and that is very efficient. The terminal must "binary clean" for this to work. Most terminals and ssh connections support it but some cooperate remote access systems use a "man in the middle" to log ssh terminal sessions and those might not be binary clean.

Many linux systems have the command base64. You can use it to copy/paste binary files.
Encode a file to stdout: base64 file.something
Copy/paste the output to a file (e.g t.txt) on the remote system and then decode it like this at the remote end:
base64 -d t.txt > file.something

© Guido Socher,