Using the pax Archiving Tool

Using the pax Archiving Tool

Over the years, a variety of UNIX operating systems have arisen, resulting in a variety of similar but incompatible file archiving formats. Even tools that go by the same name may use slightly different storage formats on different systems. This can lead to big problems when trying to archive and retrieve data in a multiplatform environment. Fortunately, there is a solution.

The pax program is a POSIX standard utility that can read and write a wide variety of archive formats. An RPM package for pax is included with Red Hat Linux. If it is not already installed, copy the pax-* RPM file from your distribution media (CD #1), or download it from a Red Hat Linux FTP site, and then use the rpm command to install it.

# rpm -i pax-*

Remember you need to be logged in as root when installing software with the rpm command.

Pax takes a variety of command-line options. The last parameter is usually the file or directory to archive. You may use wildcard characters such as "*" or "?" to specify multiple files or directories. The options you will use most often include the -r and -w parameters for specifying when you are reading or writing an archive. These are usually used in conjunction with the -f parameter, which is used to specify the name of the archive file.

By using pax parameters in different combinations, it is possible to extract an archive, create an archive, list the contents of an archive, or even copy an entire directory hierarchy from one location to another. Table 13-8 shows a few examples of the pax command in action.

Table 13-8: Examples of pax Use

Pax Command


pax -f myfiles

List the contents of the archive named myfiles.

pax -r -f myfiles

Extract the contents of the archive named myfiles.

pax -w -f myfiles /etc

Create an archive named myfiles containing everything within the /etc directory.

pax -w -f myfiles *.txt

Archive all of the files in the current directory that have a .txt file extension.

pax -r -w /olddir /newdir

Copy the entire contents of the directory /oldir into a new directory called /newdir.

pax -w -B 1440000 -f /dev/fd0 *

Archive the contents of the current directory onto multiple floppy disks.

pax -w -x cpio -f myfiles *

Archive the contents of the current directory into an archive file named myfiles using the cpio format.

pax -r -U mary -f backups

Extract all of the files owned by user mary from the archive named backups.

Note that by leaving off both the -r and -w options, you cause pax to simply list the contents of the archive. If you specify both the -r and -w options, then you should leave off the -f option and supply source and destination directories instead. This will cause the source directory to be completely cloned in the specified destination directory.

You can use additional parameters to further modify pax's behavior. For example, you may use the -x option in conjunction with the -w option to specify the specific archive type to create, or you may use the -B option to specify the number of bytes to write to each volume of a multi-volume archive.

Table 13-9 briefly describes the many optional parameters to the pax command.

Table 13-9: Options to pax

Pax Options



Read files from an archive.


Write files to an archive.


Append files to a previously created archive.

-b blocksize

Specify the archive's data block size. It must be a multiple of 512.


Match all files except those that match the specified pattern.


Match filename wildcards against file or directory names only, not the complete path.

-f archive

Specify the name of the archive.


Interactively rename files when archiving.


Do not overwrite existing files.


Link files with hard links when in copy mode (-r -w).


Match only the first file that matches the supplied pattern.

-o options

Extra options specific to the archiving format used.

-p string

Specify the file characteristics to retain when archiving or copying. Read the pax man page for more information on this option.

-s replstr

Modify the archived filenames using the supplied regular expression.


Preserve the access times of archived files.


Do not overwrite files with older versions.


Provide verbose output when running.

-x format

Specify format of the archive. Valid formats include cpio, bcpio, sv4cpio, sv4crc, tar, and ustar. The default is to use ustar when creating an archive. Pax will automatically determine the correct file type when reading an archive.


Indicates that gzip should be used to compress/decompress the archive.

-B bytes

Specify the number of bytes per archive volume. Use this option to create multivolume archives on removable media.


Do not overwrite existing files with files that have an older inode modification time.

-E limit

Limit the number of times pax will retry on encountering a read or write error.

-G group

Select files based on a group name or GID. To select by GID, place a # sign in front of the group number.


Follow only command-line symbolic links while performing a physical file system traversal.


Follow all symbolic links when traversing a directory hierarchy.


Do not follow symbolic links. This is the default.

-T time

Select files based on their modification time. Read the pax man page for complete discussion of this parameter's syntax.

-U user

Select files based on the owner's user name, or by UID with a # sign in front of it.


Do not traverse into directories that reside on a different device.


This option is the same as the -D option, except that the inode change time is checked using the pathname created after all the filename modifications have completed.


This option is the same as the -u option, except that the modification time is checked using the pathname created after all the filename modifications have completed.

As you can see, pax is a very flexible and powerful archiving tool. It can be particularly helpful in migrating data from older legacy systems to your new Linux system. When you are faced with the task of recovering archived data from an antiquated or even nonfunctioning UNIX system, the multiple file format support of pax can be a literal lifesaver.

Part IV: Red Hat Linux Network and Server Setup