How to compress/decompress LZMA/LZMA2 files in Linux

Default featured post

This post is an extension to a three years old post about working with compressed files in Linux in which the explanation about .xz file extension missing. In this article, I go through how to compress/decompress LZMA/LZMA2 files in Linux.

LZMA/LZMA2 compression algorithms usually identifiable by the .xz extension. In Linux several tools are available to support .xz files. The most famous one is tar. tar indeed is a multi-purpose tool that supports different compression algorithms including gzip, bzip2, LZMA, etc.

To be able to work with .xz files however, you first need to install xz-utils.

In Ubuntu you can install using apt command.

$ sudo apt install xz-utils

To create a .xz file use the tar command like this,

$ tar -cvJf xz_file_name.tar.xz file_names

To extract contents of a .xz file run,

$ tar -xvJf xz_file_name.tar.xz

Besides tar you may want to use xz command. Usage is identical except xz command by default does not support .tar.xz. It supports only .xz extension.

To compress,

$ xz -zv file_name

The above command create a file_name.xz file.

For extraction,

$ xz -d file_name.tar.xz

We can also combine xz and tar commands together to handle .tar.xz files.

To extract a .tar.xz file,

$ xz -dc tar_xz_file_name.tar.xz | tar x

To create a .tar.xz file,

$ tar -cf tar_file_name.tar file_names | xz - > tar_xz_file_name.tar.xz

Keep in mind that the above command creates two files, one is .tar and another is .tar.xz. So don’t get confused 🙂