xz compression has been rising in popularity because it offers smaller file sizes than
bzip2. You’re still likely to see all three on a Linux system, but you may want to start opting for xz if you want smaller file archives.
In this guide, we’re going to introduce you to xz compression, starting from basic examples to more specific and advanced usage. If you’ve worked with compressed tar files or
gzip compression (files with the
.tar.gz extension, for example) in the past, you’ll find that
xz feels very familiar.
In this tutorial you will learn:
- How to create xz compressed archives from command line or GUI
- How to decompress xz archives from command line or GUI
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Compressing and decompressing xz archives on command line
The simplest way to create an xz archive is by invoking the
xz command and specifying the name of the file you wish to compress.
$ xz file1.txt
As you can see,
file1.txt has now become
file1.txt.xz. This is the same as using the
-z (compress) option. To decompress the file, there are a few different options we can use, but these are the simplest.
$ xz -d file1.txt.xz OR $ xz --decompress file1.txt.xz OR $ unxz file1.txt.xz
Personally, I like using
unxz because it’s easy to remember. But choose whichever you want.
If you want the original file to persist after being compressed, you can use the
-k option. This will produce a
file1.txt.xz file but also leave behind the original
$ xz -k file1.txt
xz has different levels of compression, numbered 0 through 9. The higher the number, the more compression and space savings you will receive, but at the cost of increased memory and CPU usage. Finding a good compression level will depend on your machine’s hardware and the type of file you’re compressing. Some file types may see a huge benefit, and some may not.
Here are some examples of how to use compression levels.
$ xz -2 file1.txt # fast compression ... $ xz -5 file1.txt # good compression ... $ xz -9 file1.txt # best/slowest compression
If you want to combine multiple files into an xz archive, we’ll need to call on the
tar command to help us out. The
-J option tells
tar to use
$ tar cfJv archive.tar.xz example-dir/
As you might have noticed, using
tar won’t allow us to specify the level of compression we want with
xz. In this case, we can use an environment variable to assist us. This command will do the same thing as above, except use maximum compression.
$ XZ_OPT=-9 cfJv archive.tar.xz example-dir/
To extract the contents of a
.tar.xz file, use the following command syntax.
$ tar xJvf archive.tar.xz
To see the contents of a
.tar.xz file, without extracting them, use the
ft flags, like in this example.
$ tar ft archive.tar.xz
If you’re looking to split a
.tar.xz file into multiple blocks, you can check our guide on splitting tar archives into multiple same size blocks.
Compressing and decompressing xz archives in GUI
Creating or decompressing archives on GUI is going to vary a little, depending on which desktop environment you’re running. In the screenshots below, we’re using GNOME on Ubuntu. The instructions should carry over to other systems, but may require a tiny bit of improvisation.
To create an
.xz archive (of an individual file), or a
.tar.xz archive (of multiple files), highlight the files you wish to compress, right click, and click on ‘Compress.’
Make sure you select the option for
.tar.xz and name your archive. Then click ‘Create.’
To extract the contents of the archive, open it with your disto’s archive manager, highlight the files you want to extract, and click on ‘Extract.’
In this guide, we saw how to use xz compression on command line and GUI to create and decompress xz archives. xz compression offers users a lot of flexibility, with its integration into tar and the different levels of compression that it’s capable of. This tutorial should be enough to get you started using it, but if you want to delve into its other options, you can check the utility’s man page.
$ man xz