
pax Command in Linux
The pax command in Linux reads and writes file archives and copies directory hierarchies while preserving metadata. It is a versatile archiving utility that reads, writes, and extracts file archives in multiple formats such as tar, cpio, and ustar. It also supports directory hierarchy copying with metadata preservation.
Table of Contents
Here is a comprehensive guide to the options available with the pax command −
- Installation of pax Command in Linux
- Syntax of pax Command
- pax Command Options
- Examples of pax Command in Linux
Installation of pax Command in Linux
By default, the pax command may not be available on Linux. To install it on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command −
sudo apt install pax
To install it on Cent OS (version 8+, for older use yum), use the command given below −
sudo dnf install pax
To install pax on Fedora, use the following command −
sudo dnf install pax
To verify the installation of the pax command, check its binary file using the command given below −
which pax

Syntax of pax Command
The syntax of the pax command in Linux is as follows −
pax [mode] [options] [file]
In the above syntax, the [mode] field is used to specify the mode of command functionality such as read (-r), write (-w), or read/write (-r -w). However, mode is not always required. The [options] field is used to specify various options to modify the command's behavior. The [file] field is used to specify the file to be processed.
pax Command Options
The options for the pax command are listed below −
Option | Description |
---|---|
-r | Reads and extracts archive members from standard input. Automatically detects archive format. |
-w | Writes an archive of specified files to standard output. Includes entire directory hierarchies. |
-r -w | Copies files to a destination directory. Behaves like writing to an archive and then extracting. |
-a | Appends files to an existing archive. Archive format must match the existing archive. |
-0 | Uses NUL (\0) as a pathname terminator instead of newline (\n). |
-b blocksize | Specifies block size for writing archives (multiples of 512 bytes, max 64512). |
-c | Matches all files or archive members except those specified by patterns. |
-d | Matches only directories, not their hierarchies, during copy or archive operations. |
-f archive | Specifies the pathname of the input or output archive. |
-i | Interactively renames files or archive members during processing. |
-k | Prevents overwriting existing files. |
-l | Creates hard links between source and destination files during copy operations. |
-n | Selects only the first archive member matching each pattern. |
-o options | Specifies format-specific options for extracting or writing archives. |
-p string | Preserves or discards file characteristics (a, e, o, m, p). |
-s replstr | Modifies file or archive member names using substitution expressions. |
-t | Resets access times of files or directories to their original values. |
-u | Ignores files older than pre-existing files or archive members with the same name. |
-v | Produces verbose output during list, read, write, or copy operations. |
-x format | Specifies the archive format such as cpio, tar, or ustar. |
-z | Compresses or decompresses archives using gzip. Incompatible with -a. |
-B bytes | Limits the number of bytes written to a single archive volume. |
-D | Checks file inode change time instead of modification time during copy operations. |
-E limit | Limits the number of consecutive read faults when processing a flawed archive. |
-G group | Selects files based on group name or numeric GID. |
-H | Follows only command-line symbolic links during file system traversal. |
-L | Follows all symbolic links during file system traversal. |
-O | Forces the archive to be one volume without prompting for new volumes. |
-P | Performs a physical file system traversal (default, does not follow symlinks). |
-T [from_date][,to_date][/[c][m]] | Selects files based on modification or inode change times within a specified range. |
-U user | Selects files based on user name or numeric UID. |
-X | Prevents descending into directories with different device IDs during traversal. |
-Y | Checks inode change time after all filename modifications are applied. |
-Z | Checks modification time after all filename modifications are applied. |
Examples of pax Command in Linux
In this section, the usage of the pax command in Linux will be discussed with examples −
- Listing the Contents of an Archive
- Extracting an Archive
- Creating an Archive
- Specifying the Block Size while Creating an Archive
- Creating an Archive with a Specific Archive Format
- Appending Files to an Existing Archive
- Preserving or Discarding the Specific Characteristics
- Modifying and Extracting the Names of Files in the Archive
- Compressing an Archive
- Getting Verbose Output
Listing the Contents of an Archive
To list the contents of a tar file, use the following command −
pax -f archive.tar

In the above syntax, the -f option is used to specify the archive's file name.
Extracting an Archive
To extract an archive, use the -f option in the read mode (-r) −
pax -r -f archive.tar

Creating an Archive
To create an archive file of a directory, use the pax command with -w option −
pax -w -f archive.tar mydir

The above command creates an archive of all the contents in the /mydir directory and creates a tar (archive.tar) file in the current working directory.
Specifying the Block Size while Creating an Archive
To specify the block size while creating an archive, use the -b option −
pax -b 1024 -w -f archive.tar mydir
The above command writes an archive of files from /mydir to archive.tar, using a block size of 1024 bytes.
Creating an Archive with a Specific Archive Format
To create an archive with a specific archive format, use the -x option with the format type. For example, to set the format as cpio, use the following command −
pax -x cpio -w -f archive.cpio mydir
Other formats are tar or ustar.
Appending Files to an Existing Archive
To append files to an existing archive, use the -a option in write mode (-w) −
pax -aw -f archive.tar file3.txt file4.txt

Preserving or Discarding Specific Characteristics
To preserve or discard the specific characteristics, use the -p option. For example, to preserve the permissions without ownership(user ID and group ID) while extracting, use the p characteristic with the specific string −
pax -r -p p -f archive.tar
All features that can be preserved are listed below −
Characteristic | Description |
---|---|
a | Does not preserve file access times. By default, access times are preserved when possible. |
e | Preserves everything: user ID, group ID, file mode bits, access time, and modification time. Equivalent to o + p. Intended for root users. |
m | Does not preserve file modification times. By default, modification times are preserved when possible. |
o | Preserves user ID and group ID. |
p | Preserves file mode bits but not ownership. Regular users can use this to maintain file permissions without preserving ownership. |
Modifying and Extracting the Names of Files in the Archive
To change the name of files in the archive while extracting, use the -s option in the read (-r) mode −
pax -s '|file1.txt|myfile.txt|' -r -f archive.tar

The above command extracts files from archive.tar, replacing occurrences of file1.txt with a new name.
Compressing an Archive
To compress an archive with gzip, use the -z option. The following command creates a gzip-compressed archive −
pax -z -w -f archive.tar.gz mydir
Getting Verbose Output
To get verbose output, use the -v option −
pax -v -z -w -f archive.tar.gz mydir

Conclusion
The pax command in Linux is a versatile tool for handling file archives and copying directory hierarchies while preserving metadata. It supports multiple archive formats like tar, cpio, and ustar, allowing files to be listed, extracted, and created efficiently. Various options customize its behavior, such as setting block size, modifying filenames, appending files, and compressing archives.