UNIX Hints & Hacks

ContentsIndex

Chapter 6: File Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

6.1 Copy Files with Permissions and Time Stamps

 

6.5 Finding Files with grep

 

6.8 Moving and Renaming Groups of Files

 

6.11 Splitting Files

6.2 Copy Files Remotely

 

6.6 Multiple grep

 

6.9 Stripping the Man Pages

 

6.12 Limit the Size of the Core

6.3 Which tmp Is a Good Temp?

 

6.7 Executing Commands Recursively with find

 

6.10 Clean Up DOS Files

 

6.13 uuencode and uudecode

6.4 Dealing with Symbolic Links

 

 

 

 

 

 

 

6.13 uuencode and uudecode

6.13.1 Description

6.13.1 Description

There is still a need for UNIX administrators to understand the conversion process of uuencoding binary files into ASCII text and uudecoding them back to binary.

Example One: Encoding a Binary File

Flavors: AT&T, BSD

Syntax:

uuencode [file] name

When encoding a binary file into an ASCII file, three filenames get passed to the uuencode command. The first is the name of the actual file being uuencoded. The second is the name that the file will become after the uuencoded file is decoded. This can be the same as the first name. The third is the filename the uuencoded file is stored as. The last filename usually has the .uu extension tacked to the end of the file. It might all sound confusing, but it'll all make sense in a minute.

For example, suppose there's a new compiled version of a program called top in binary format. You would have to uuencode the file before you could email it to a friend. It works like this:

rocket 82% uuencode top top.new > top.uu

rocket 83% more top.uu
begin 755 top.new
M?T5,1@$"`0`````````````"``@````!$``9=````#0``'C0(```)``T`"``
M!P`H`!@`%P````8````T$```-!```#0```#@````X`````0````$`````P``
M`100``$4$``!%````!$````1````!`````%P```"```!*!```2@0``$H````
.
.
.
M`*$````(`````Q`!:"```&@@```%Y```````````````$`````````%M<```
M'@@```````````!P````!S````````````````$````````!E`````,`````
<````````=S````&>``````````````````````$`
`
end

You can see that the uuencoded file top.uu can be viewed because it is now an ASCII file. The file is also marked by begin and end markers. Following the begin marker, on the first line of the file is the permission that the file top.new has when it is decoded. This filename (top.new) is the same as the second name that was used at the time uuencode was run on the binary file. The file is now ready to be emailed to a friend, posted to a Usenet newsgroup, or about anything else you want to do with it.

rocket 84% mail friend@foo.com < top.uu

It is possible to uuencode a file and email it all in one command. As you might remember from section 4.5, "Mail a Process," you can pipe the output of a command or process to an email address.

rocket 85% uuencode top top.new | mail friend@foo.com

When the file arrives at the destination email address it is ready to be uudecoded, or the email client interprets it for the recipient.

It is possible to uuencode a series of files within a particular directory by creating a quick script that processes each file and uuencodes it.

vi uuall


#! /bin/sh
for uufile in `ls -1` do uuencode $uufile $uufile > $uufile.uu done

Line 1: Define the shell to be used.

Line 2: Process each file in the current directory.

Line 4: While processing through each file, uuencode it with its current name and place a .uu extension on the uuencoded file.

When the uuall script is executed, it progresses through a directory and all the files are uuencoded.

rocket 64% ls -al
total 458
drwxr-xr-x    2 ugu user         512 Jan 2 16:46 ./
drwxr-xr-x   31 ugu user        2048 Jan 2 16:45 ../
-rw-r--r--    1 ugu user       45898 Jan 2 16:46 ugu.gif
-rw-r--r--    1 ugu user       81920 Jan 2 16:46 uguboard.tar
-rwxr-xr-x    1 ugu user        3854 Jan 2 16:46 ugugrab*
rocket 65% uuall
rocket 66% ls -la total 1084 drwxr-xr-x 2 ugu user 512 Jan 2 16:48 ./ drwxr-xr-x 31 ugu user 2048 Jan 2 16:45 ../ -rw-r--r-- 1 ugu user 45898 Jan 2 16:46 ugu.gif -rw-r--r-- 1 ugu user 71042 Jan 2 16:48 ugu.gif.uu -rw-r--r-- 1 ugu user 81920 Jan 2 16:45 uguboard.tar -rw-r--r-- 1 ugu user 112899 Jan 2 16:48 uguboard.tar.uu -rwxr-xr-x 1 ugu user 3854 Jan 2 16:46 ugugrab* -rw-r--r-- 1 ugu user 5336 Jan 2 16:48 ugugrab.uu

Example Two: Decoding a Binary File

Flavors: AT&T, BSD

Syntax:

uudecode [file]

Most of the email clients today can interpret an encoded message that was made with the uuencode command and translate the ASCII text back into a binary format. For those readers that can't, you have to do it manually.

The manual process is a simple task of writing the email message out to a file, stripping the header information and running uudecode. After you write out the email message it should have all the header information attached to the uuencoded file.

planet 20% vi /usr/tmp/top.tmp
>From xinu@ugu.com Sat Nov 28 23:34:44 1998 Received: from mail.ugu.com (0@mail.ugu.com [204.128.192.15]) by foo.com (8.8.8/8.7.6) with ESMTP id XAA07347 for <friend@foo.com>; Sat, 28 Nov 1998 23:34:42 -0500 (EST) Received: from ugu.com (ugu.com [206.18.19.10]) by mail.ugu.com (8.9.1/8.9.1) with ESMTP id UAA12667 for <friend@foo.com>; Sat, 28 Nov 1998 20:34:41 -0800 (PST) Received: (from xinu@localhost) by ugu.com (8.9.1/8.9.0) id UAA110274 for friend@foo.com; Sat, 28 Nov 1998 20:35:05 -0800 (PST) Date: Sat, 28 Nov 1998 20:35:05 -0800 (PST) From: UGU <xinu@ugu.com> Message-Id: <199811290435.UAA110274@ugu.com> Status: O begin 755 top.new M?T5,1@$"`0`````````````"``@````!$``9=````#0``'C0(```)``T`"`` M!P`H`!@`%P````8````T$```-!```#0```#@````X`````0````$`````P`` M`100``$4$``!%````!$````1````!`````%P```"```!*!```2@0``$H```` . . . M`*$````(`````Q`!:"```&@@```%Y```````````````$`````````%M<``` M'@@```````````!P````!S````````````````$````````!E`````,````` <````````=S````&>``````````````````````$` ` end

Bring the email message file into an editor, and delete all email header information that exists before the line that starts with the word begin.

begin 755 top.new
M?T5,1@$"`0`````````````"``@````!$``9=````#0``'C0(```)``T`"``
M!P`H`!@`%P````8````T$```-!```#0```#@````X`````0````$`````P``
M`100``$4$``!%````!$````1````!`````%P```"```!*!```2@0``$H````
.
.
.
M`*$````(`````Q`!:"```&@@```%Y```````````````$`````````%M<```
M'@@```````````!P````!S````````````````$````````!E`````,`````
<````````=S````&>``````````````````````$`
`
end

Now that the email message looks like a uuencoded file, write the file out with the .uu extension appended to the end.

:w! /usr/tmp/top.uu

From the vi editor the w! forces the file to be written out as /usr/tmp/top.uu, as long as you have the permission in the directory to do so.

planet 21% ls -l top*
-rw-------  1 xinu         44694 Nov 28 23:59 top.tmp
-rw-r--r--  1 xinu         43963 Nov 29 00:00 top.uu

Run the uudecode command on the file /usr/tmp/top.uu to decrypt the file into its original binary state.

planet 22% uudecode top.uu
planet 23% ls -al top*
-rwxr-xr-x  1 xinu         31888 Nov 29 00:00 top.new*
-rw-------  1 xinu         44694 Nov 28 23:59 top.tmp
-rw-r--r--  1 xinu         43963 Nov 29 00:00 top.uu

When the file is uudecoded the file binary file takes on the name and the permissions passed on to it from the uuencode command.

Note - In some flavors of UNIX, the uudecode command strips out the header information for you. Try it on your flavor first.


There is a nice tool available on the Internet called uudeview that not only strips out any header information and performs a uudecode, but also handles various decompression formats.

Reason

Many administrators forget about the use and benefit of uuencoding files. Because newer mail clients handle the translation automatically, the newer administrators get lazy and forget about the uuencode and uudecode commands.

Real World Experience

There are no real tools in DOS to extract a uuencoded file. When binary files are emailed across the network they are uuencoded. Decoding the file back to its original state is all handled by the mail client. Most administrators working in the PC world are unaware that these files are actually encrypted by uuencoding them. They understand only that it is a binary program that has been encrypted into an ASCII format. If a malformed email ends up hanging your SMTP mail gateway, a UNIX system administrator would have to figure out that a 50MB movie file of a pornographic nature is holding up the entire company from getting its email.

The uuencode command can work well by adding another level of security. You can take a compressed binary file and uuencode it, split the file every 1000 lines (see section 6.11 "Splitting Files"), and email all the split pieces to a destination address. The file will have to be appended back together in the correct order, uudecoded, and uncompressed back to its original state.

Other Resources

Man pages:

uuencode, uudecode, mail

World Wide Web:

uudeview--http://www.informatik.uni-frankfurt.de/~fp/uudeview/

UNIX Hints & Hacks

ContentsIndex

Chapter 6: File Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

6.1 Copy Files with Permissions and Time Stamps

 

6.5 Finding Files with grep

 

6.8 Moving and Renaming Groups of Files

 

6.11 Splitting Files

6.2 Copy Files Remotely

 

6.6 Multiple grep

 

6.9 Stripping the Man Pages

 

6.12 Limit the Size of the Core

6.3 Which tmp Is a Good Temp?

 

6.7 Executing Commands Recursively with find

 

6.10 Clean Up DOS Files

 

6.13 uuencode and uudecode

6.4 Dealing with Symbolic Links

 

 

 

 

 

 

 

© Copyright Macmillan USA. All rights reserved.