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.

© Guido Socher,