Archiving with Pax

In today's article, I'd like to finish up the archiving series with the
pax utility. It's unfortunate that this utility never seems to get the
coverage that tar and cpio do. I've found that it combines the best
qualities of both utilities into one easy-to-use and fairly intuitive
utility.

The name of the utility stands for "portable archive exchange," as it was
designed specifically to allow portability between different versions of
Unix. There's also a bit of wry humor in the name, as pax attempts to
bring some "peace" to the long-standing battle over which is better: tar or
cpio. The pax utility can be used to create either type of archive, and
during a restore, it automagically detects the type of archive for you. And it
doesn't matter what type of Unix that archive happened to be created on,
meaning you can back up files from your FreeBSD system and restore them to,
say, a SCO system.

Let's start with some examples of basic pax usage, then move on to some
fancier stuff. To back up the contents of your home directory, invoke write
mode using the w switch:

cd
pax -wf home.pax .

In this example, I went to my home directory with cd, then told pax to
write (w) to a file (f) named home.pax the contents of the current directory ("."). When you use pax, it's very important to remember to
include that f switch to indicate the name of the archive you'd like to
create. If you forget the f, weird characters will be sent to your screen, accompanied by horrible, pained noises. Also, if you want to watch as
pax does its thing, simply add the v, or verbose, switch to the switch
portion of the command.

To see what type of file you've just created, use the file command:

file home.pax
home.pax: POSIX tar archive

To see the contents of that archive, tell pax which archive file you'd
like to view, using the f switch:

pax -f home.pax |more

Since my archive file is rather large, I piped this output to the more
command so I could read the contents of the archive one page at a time.
If you also include the v switch, you'll get an "ls -l"-type output
of the archive contents. Again, don't forget to specify the name of the
archive with the f switch, or nothing will happen, except that you'll lose
your prompt until you press CTRL-C.

The pax utility does support compression, so I could have performed a compressed backup by including the z switch:

Let's do another example. This time I'll back up the /etc directory to a
floppy:

cd /etc
su
Password:
pax -wf /dev/fd0 .

You'll note that I became the superuser in this example. This was
necessary for two reasons. First, the files in /etc are owned by root, since they are the configuration files for the system. Second, by default,
only the superuser has permission to back up to a floppy drive. Also notice
that I specified the floppy as the name of the archive file (/dev/fd0).

A couple of notes about backing up to a floppy: the pax utility
is intelligent enough to realize when it fills up a floppy, and will prompt
for another one if the archive file is too large to fit onto one floppy.
However, pax does not support compression to a floppy; if you try adding
the z switch to the above example, you'll receive this error message:

gzip: stdout: Invalid argument

You should also be aware that, by default, when you back up to a floppy, you
will lose any previous data stored on that floppy. If you would like to
append to a previous archive, use the a switch:

cd
pax -wvf /dev/fd0 jpegs
pax -wavf /dev/fd0 pdfs
pax -f /dev/fd0

The above example will back up the jpegs directory to a floppy, then append
the pdfs directory to the backup. When I list the archive, the contents of
both the jpegs and pdfs directories will be on the floppy.

To restore (or use read mode on) an archive, first cd into the destination
directory, then use the r switch. For example, I'll restore the backup
named home.pax into the test subdirectory of my home directory: