Pax command examples on Linux

pax is an archive utility somewhere between cpio and tar. This is just because it is independent of the specific archive format, and supports a wide variety of different archive formats. It can perform simple tasks as creating a compressed archive of a selected directory or it can as much easily create a daily incremental backup.

In this tutorial, we will see how to protect our daily work by creating an incremental backup with pax on a Linux system. On certain Linux distributions, this command may not be installed by default. In this case, you’ll need to install it manually. We’ll include instructions for that below.

In this tutorial you will learn:

  • How to install pax on major Linux distros
  • How to use pax on Linux
Pax command examples on Linux
Pax command examples on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software pax
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install pax in Linux




In case pax is not already installed on your system by default, you can use the appropriate command below to install it with your system’s package manager.

To install pax on Ubuntu, Debian, and Linux Mint:

$ sudo apt install pax

To install pax on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install pax

To install pax on Arch Linux and Manjaro:

$ sudo pacman -S pax

Frequently Used Options

Once you have some files that need to be backed up, you can use the pax command to create an initial backup, and then use it again in the future to only back up the incremental changes. This saves a lot of disk space when compared to full backups, and is much faster and more efficient.

See some examples below for an idea on how to get started with pax.

pax Command in Linux Examples

  1. To make a backup of a directory, we will supply the -w, -v, and f options. -w means write, telling pax that we wish to write a backup. -v is just verbose, so that we can see the backup process unfolding as pax processes our request. And -f allows us to specify the path to our backup file we create.
    $ pax -wvfz mybackup.pax ~/myfiles
    

    This command has created a backup named mybackup.pax of the directory ~/myfiles.

    Using the pax command in Linux to create a backup
    Using the pax command in Linux to create a backup



  2. To create an incremental backup, we can use the -T option to instruct pax command to copy only files changed and created since midnight. You can continue to do an incremental backup indefinitely. However, it is recommended to do a full back at least once a week or month, but the frequency will depend on the nature of your work. Note we also add the date to our incremental backup in the command below.
    $ pax -T 0000 -wvf mybackup-$(date +%Y%m%d).pax ~/myfiles
    
  3. You can also add the -z option to create compressed archives if you are concerned about disk space.
    $ pax -wvfz mybackup.pax ~/myfiles
    
  4. We can use the -r command to read our pax file and extract its contents. We will still keep the -v and -f options in our command in order to see verbose output and specify the pax file we wish to open.
    $ pax -rvf mybackup.pax ~/myfiles
    
    Similar output to our command before, but now it says it has read data instead of written it (extracted).
    Similar output to our command before, but now it says it has read data instead of written it (extracted).
NOTE
You can always use the man command to read more about the pax command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Closing Thoughts




In this tutorial, we saw how to install and use the pax command on a Linux system. Pax is a handy tool, similar to other staples on Linux like tar and cpio. It works especially well for creating incremental backups because it lets us specify how old a file should be in order to be part of the backup.