Tutoriel Linux

Compressing a file with xz on Linux without losing the original

Débutant6 min de lecture

You must compress a disk image, a SQL dump, a CSV export, or a raw backup before transferring. xz often reduces size better than gzip, but it consumes more CPU and can require a lot of memory at higher levels.

Use xz for a single, large, and poorly compressed file. Avoid it for a JPEG photo, an MP4 video, a .zip archive, a .deb package, or a file whose name already ends with .gz, .xz, or .zst.

The steps below cover creating an .xz archive, retaining the source file, integrity testing, choosing the compression level, and verifying a remote copy.

Compressed Linux archive with xz and Tux in front of a server
Compressing a Linux archive with xz before transfer or storage.

Check if xz is available

Most distributions install xz by default via the xz-utils or xz package. Check the command before integrating it into a backup procedure.

xz --version

On Debian, Ubuntu, and Linux Mint, install the package if the command is absent.

sudo apt update
sudo apt install xz-utils

On Fedora, AlmaLinux, or Rocky Linux, install the package with dnf.

sudo dnf install xz

Choose xz, gzip, or zip

xz compresses a single file with LZMA2. It is suitable for a SQL dump, a text export, an uncompressed disk image, a consolidated log file, or a pre-created .tar archive.

  • Choose xz when size gain matters more than speed.
  • Choose gzip when you want fast compression in a script, pipeline, or log rotation.
  • Choose zip when you need to bundle several files in a format easy to open on Windows.
  • Choose tar before xz when you need to preserve a Linux directory structure with its directories, names, and permissions.

xz alone does not bundle multiple files. To compress a folder, first create a tar archive, then compress it with xz.

tar -cf projet.tar projet/
xz -k projet.tar

The tar command can also directly create projet.tar.xz.

tar -cJf projet.tar.xz projet/

Check the file before compression

Look at the file type and size before a long compression. A file already compressed may hog the CPU for almost no gain.

file sauvegarde.sql
ls -lh sauvegarde.sql

If file indicates gzip compressed data, XZ compressed data, Zip archive data, JPEG image data, or ISO Media, first test on a small sample.

Compress while keeping the source file

Without options, xz sauvegarde.sql replaces sauvegarde.sql with sauvegarde.sql.xz. Add -k to keep the original during checks.

xz -k sauvegarde.sql

Add -v to display progress, ratio, and useful information during execution.

xz -k -v sauvegarde.sql

Then check that both files exist and compare their sizes.

ls -lh sauvegarde.sql sauvegarde.sql.xz

Do not delete the original just because the .xz file exists. Test the archive and validate the restoration before any deletion.

Choose a compression level

The default level of xz is -6. It often gives a good ratio, but it is not always suitable for a low-powered machine or an urgent restore.

  • Use -1 to -3 when speed matters and maximum gain is not a priority.
  • Keep -6 for general use on a comfortable machine.
  • Reserve -7 to -9 for large files that you archive rarely and that you can also decompress on a sufficiently equipped machine.
xz -3 -k export.csv
xz -6 -k sauvegarde.sql
xz -9 -k archive.img

High levels can increase the memory required for compression and decompression. Avoid -9 for an archive that needs to be opened on a small VPS, an old machine, or a minimal rescue system.

Limit CPU usage on a server

The option -T sets the number of threads. -T0 uses all available cores, which can slow down a database, a web server, or active containers.

xz -6 -T2 -k sauvegarde.sql

On a production machine, start with -T1 or -T2. Monitor the load, then increase only if the server remains responsive.

uptime
top

For planned compression, run it outside of peak hours and check free space before launching. The -k option keeps two files and requires more disk space.

df -h .

Test the integrity of the archive

Always test the archive with xz -t before deleting the original or validating a backup. The command reads and decompresses the data in memory without creating an output file.

xz -t sauvegarde.sql.xz
echo $?

A return code 0 indicates that the xz test was successful. Any other code signals an error and requires retaining the source file.

Then display the metadata of the archive to check the uncompressed size, the compressed size, the ratio, and the type of integrity check.

xz -l sauvegarde.sql.xz

xz -t checks that the archive can be read. It does not prove that a remote copy is identical to the local file.

Verify a transfer with SHA-256

Calculate a SHA-256 hash before the transfer, copy the archive, then recalculate the hash on the destination. Both lines should show the same value.

sha256sum sauvegarde.sql.xz
scp sauvegarde.sql.xz admin@server:/srv/archives/
ssh admin@server 'sha256sum /srv/archives/sauvegarde.sql.xz'

Keep the hash with the archive if it goes to an external disk, object storage, or another server.

sha256sum sauvegarde.sql.xz > sauvegarde.sql.xz.sha256

To verify later from the same folder, use the .sha256 file.

sha256sum -c sauvegarde.sql.xz.sha256

Decompress without losing the archive

unxz sauvegarde.sql.xz deletes the archive after extraction. Add -k to keep the .xz file until you have checked the restored content.

unxz -k sauvegarde.sql.xz
ls -lh sauvegarde.sql sauvegarde.sql.xz

The equivalent command with xz is useful in scripts that already call this binary.

xz -d -k sauvegarde.sql.xz

If the output file already exists, xz refuses to overwrite it without -f. Do not force extraction before comparing the path, date, size, and actual need for replacement.

ls -lh sauvegarde.sql sauvegarde.sql.xz
stat sauvegarde.sql

For cautious restoration, decompress into a separate folder and compare the obtained file before replacing a production file.

mkdir -p /tmp/test-xz
cp sauvegarde.sql.xz /tmp/test-xz/
cd /tmp/test-xz
unxz -k sauvegarde.sql.xz
ls -lh sauvegarde.sql

Read an archive without creating a file

To quickly inspect a compressed text file, send the decompressed content to standard output with xzcat. This method avoids writing a large temporary file.

xzcat sauvegarde.sql.xz | head -20

To search for a string in a compressed text file, use xzgrep instead of decompressing the entire archive.

xzgrep -n "CREATE TABLE" sauvegarde.sql.xz

Do not use these commands on a confidential dump if your terminal logs the output or if you are working in a shared session.

Recommended procedure for a backup

  • Check the file type with file and its size with ls -lh.
  • Check available space with df -h . before using -k.
  • Compress with xz -k -v and a level suited to the machine.
  • Limit threads with -T1 or -T2 on an active server.
  • Test the archive with xz -t and require a return code of 0.
  • Calculate a sha256sum hash before and after each transfer.
  • Test extraction in a separate folder before replacing an important file.

xz is useful when disk space or bandwidth costs more than CPU time. It does not replace a backup strategy, a hash kept with the archive, and a restoration test completed before an incident.