Extract a tar.xz file on CentOS and RedHat

This guide will show you how to extract a tar.xz file on CentOS or RedHat, and probably other flavors of Linux as well. Traditionally compressed archive files are distributed on Linux systems as tar.gz files which use gzip for compression. Extracting them is as simple as passing xzf to tar.

tar -xvf filename.tar.gz

If you try the same thing using a tar.xz file, you’ll find that it doesn’t work.

tar -xzf filename.tar.xz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error exit delayed from previous errors

On newer versions of tar, you can simply replace the z with a J to use the correct (de)compression library, but if you have version 1.15.1 or earlier, you’ll find that this doesn’t work either. Note that this is a capital “J” and not a lowercase “j” which would be used to specify bzip2 compression.

tar -xJf gnutls.tar.xz
tar: invalid option -- J
Try `tar --help' or `tar --usage' for more information.

Getting around this is as simple as using the xz binary to first decompress the file, and then tar to extract it. If you don’t already have it, you can install xz using yum.

yum -y install xz
unxz filename.tar.xz
tar -xf filename.tar

You may also like...

7 Responses

  1. Arun George says:

    It works thankyou

  2. Very good website – bookmarked

  3. pawan says:

    thanks , worked good

  4. indra says:

    when i “unxz MPlayer-1.1.1.tar.xz”

    the terminal say”no such file or directory”

    what happen?

    • Justin Silver says:

      Hi Indra,

      There are a few things that could cause the binary to not be found. First make sure that it is installed (on CentOS run yum remove -y xz && yum install -y xz). That should remove/reinstall xz and set the proper file permissions and attributes, but if not make sure that the unxz is in your page (or use the full path to the binary) and double check that it has execute permissions with chmod +x unxz. It could also be that use are using a 32 bit binary on a 64 bit system, or vice versa.

      Good luck!

  5. haithem Rahmani says:

    you can do that as follows:

    tar xJf filename.tar.xz // the “J” is uppercase.

    • Justin Silver says:

      You are correct – it’s mentioned in the second code section above – but unfortunately this option is not supported on `tar` 1.15.1 or earlier. If you have one of these older version like I do on some of my legacy servers, the easiest thing to do is combine `tar` with `unxz`.

Leave a Reply

Your email address will not be published. Required fields are marked *