pax can do a lot of fancy stuff, feel free to contribute more awesome pax tricks!
===== Introduction =====
The POSIX archiver, ''pax'', is an attempt at a standardized archiver with
the best features of ''tar'' and ''cpio'', able to handle all common archive types.
However, this is **not a manpage**, it will **not** list all possible options,
it will **not** you detailed information about ''pax''. It's only an introduction.
This article is based on the debianized Berkeley implementation of ''pax'', but implementation-specific things should be tagged as such. Unfortunately, the Debian package doesn't seem to be maintained anymore.
===== Overview =====
==== Operation modes ====
There are four basic operation modes to //list//, //read//, //write// and
//copy// archives. They're switched with combinations of ''-r'' and ''-w''
command line options:
^ Mode ^ RW-Options ^
| List | //no RW-options// |
| Read | ''-r'' |
| Write | ''-w'' |
| Copy | ''-r -w'' |
=== List ===
In //list mode//, ''pax'' writes the list of archive members to standard
output (a table of contents). If a pattern match is specified on the
command line, only matching filenames are printed.
=== Read ===
//Read// an archive. ''pax'' will read archive data and extract the members to the
current directory. If a pattern match is specified on the command line, only matching
filenames are extracted.
When reading an archive, the archive type is determined from the archive data.
=== Write ===
//Write// an archive, which means create a new one or append to an
existing one. All files and directories specified on the command line are inserted into
the archive. The archive is written to standard output by default.
If no files are specified on the command line, filenames are read from ''STDIN''.
The write mode is the only mode where you need to specify the archive type
''pax'' is in //write// mode, the given filenames are packed into an
archive:
* ''README.txt'' is a normal file, it will be packed
* ''*.png'' is a pathname glob **for your shell**, the shell will substitute all matching filenames **before** ''pax'' is executed. The result is a list of filenames that will be packed like the ''README.txt'' example above
* ''data/'' is a directory. **Everything** in this directory will be packed into the archive, i.e. not just an empty directory
When you specify the ''-v'' option, ''pax'' will write the pathnames of the
files inserted into the archive to ''STDERR''.
When, and only when, no filename arguments are specified, ''pax'' attempts to
read filenames from ''STDIN'', separated by newlines.
This way you can easily combine ''find'' with ''pax'':
In this case, the day-name is an archive-file (you don't need a filename
extension like ''.tar'' but you can add one, if desired).
==== Changing filenames while archiving ====
''pax'' is able to rewrite filenames while archiving or while extracting from an archive. This example creates a tar archive containing the ''holiday_2007/'' directory, but the directory name inside the archive will be ''holiday_pics/'':
The option responsible for the string manipulation is the ''-s <REWRITE-SPECIFICATION>''. It takes the string rewrite specification as an argument, in the form ''/OLD/NEW/[gp]'', which is an ''ed(1)''-like regular expression (BRE) for ''old'' and generally can be used like the popular sed construct ''s/from/to/''. Any non-null character can be used as a delimiter, so to mangle pathnames (containing slashes), you could use ''#/old/path#/new/path#''.
The optional ''g'' and ''p'' flags are used to apply substitution **(g)**lobally to the line or to **(p)**rint the original and rewritten strings to ''STDERR''.
Multiple ''-s'' options can be specified on the command line. They are applied to the pathname strings of the files or archive members. This happens in the order they are specified.
==== Excluding files from an archive ====
The -s command seen above can be used to exclude a file. The substitution must result in a null string:
For example, let's say that you want to exclude all the CVS directories to create a source code archive.
We are going to replace the names containing /CVS/ with nothing, note the .* they are needed because we need to match the entire pathname.
This can also be done while reading an archive, for instance, suppose you have an archive containing a "usr" and a "etc" directory but that you want to extract only the "usr" directory:
Like ''cpio'', pax can read filenames from standard input (''stdin''). This provides great flexibility - for example, a ''find(1)'' command may select files/directories in ways pax can't do itself. In **write** mode (creating an archive) or **copy** mode, when no filenames are given, pax expects to read filenames from standard input. For example:
The ''-d'' option tells pax ''not'' to recurse into directories it reads (''cpio''-style). Without ''-d'', pax recurses into all directories (''tar''-style).
**Note**: the ''-0'' option is not standard, but is present in some implementations.
===== From tar to pax =====
''pax'' can handle the ''tar'' archive format, if you want to switch to the standard tool an alias like: