By now you’ve probably seen this comic:
In 2001 the IEEE, promulgators of the POSIX standard, set out to fix this problem,1 and came up with a solution: pax.
pax, like tar, is both the name of a file archive format and of a program used to create and unpack them; but the pax program, by default, produces and unpacks tar files, with a much simpler interface than tar: it’s quite easy to commit the basics of it to memory.
pax was standardized in the POSIX standard of 2001, and is included with most Unix variants; if it doesn’t come with yours, it’s very probably available in the package manager under the name pax.
tar talks about ‘creating’ and ‘extracting’ archives; pax talks about ‘writing’ and ‘reading’ them.2 pax does not do file I/O to read or write an archive file itself: you always use it in a pipeline. With this in mind, let’s begin with an example:
pax -r < archive.tar
This invokes pax to read (-r) the file archive.tar and extract it to the current directory. The < is a Unix shell idiom for creating a pipe from a file to a program; it’s functionally equivalent to
cat archive.tar | pax -r, which, apart from being longer to type, is also a classic Useless Use of Cat.
If a file is compressed with gzip, bzip2, xz, etc., you just add the decompression program to the pipeline — or use the (non-standard, but supported by most implementations) -z and -j options for gzip and bzip2, respectively:
gzip -d | pax -r < archive.tar.gz
pax -rz < archive.tar.gz
Here’s an example that shows how to create an archive:3
pax -w <<< directory > archive.tar
This tells pax to write (-w) an archive containing directory to the file archive.tar. Again, you can add compression programs to the chain, or use the -z and -j options:
pax <<< directory | gzip > archive.tar.gz
pax -wz <<< directory > archive.tar.gz
That’s about all you need to know in terms of making and unpacking archives with pax. It has a few more tricks up its sleeve, though.
You can list the names of the files in an archive by piping an archive to it without passing -r:
pax < archive.tar
gzip -d < archive.tar.gz | pax
pax -z < archive.tar.gz
You can also use pax to copy directory hierarchies by using -r and -w together. You could use cp -R for this, but there are some pitfalls to that approach. (My personal preference is to run a local-to-local rsync job, but pax is there, if you want it.)
pax -rw dest_dir <<< source_dir
You now know all you need to start using pax. The mental burden of all those fiddly tar options is now over for you. Go forth and archive!
echo directory | pax -w > archive.tar) ↩