How to extract XZ compressed archive on Linux

XZ is a widely used compression format that provides high compression ratios. It is commonly used for compressing files in Linux environments due to its efficiency and compatibility. Extracting XZ compressed archives on Linux can be accomplished using various command-line tools. In this article, we will guide you through the detailed steps of extracting XZ compressed files on a Linux system.

In this tutorial you will learn:

  • How to check if XZ utilities are installed
  • How to install XZ utilities if not already installed
  • How to list contents of a tar.xz archive
  • How to extract XZ compressed files
  • How to extract specific files from a tar.xz archive
How to Extract XZ Compressed Archive on Linux
How to Extract XZ Compressed Archive on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distribution
Software XZ Utils
Other Internet connection for installing software
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

Checking for XZ Utilities

Before extracting XZ compressed archives, ensure that the XZ utilities are installed on your system. The xz command-line tool is essential for handling XZ files.

  1. Check if XZ is installed: Open your terminal and type the following command to check if xz is installed.
    $ xz --version

    If XZ utilities are installed, you will see the version information. If not, you need to install it.

  2. Install XZ utilities: If xz is not installed, you can install it using your package manager. For example, on Debian-based systems (like Ubuntu), use the following command:
    # apt install xz-utils

    For Red Hat-based systems (like CentOS), use:

    # yum install xz

    This will install the XZ utilities on your system.

  3. Listing Contents of a tar.xz Archive: Before extracting files, you might want to see the contents of the tar.xz archive. You can list the contents using the tar command with the -t option.
    $ tar -tf archive.tar.xz

    This command will display a list of files contained in the archive without extracting them.

  4. Extracting XZ Compressed Files: Once XZ utilities are installed, you can extract XZ compressed files using the xz command followed by the -d option (which stands for decompress).
    $ xz -d filename.xz

    This command will decompress filename.xz and remove the original .xz file, leaving you with the decompressed file.

    If you want to keep the original compressed file, use the following command:

    $ xz -dk filename.xz

    The -k option tells xz to keep the original file.



  5. Extracting Specific Files from a tar.xz Archive: If you have a tar.xz archive and want to extract only specific files, you can do so using the tar command. First, list the contents of the tar.xz archive to see the files it contains:
    $ tar -tf archive.tar.xz

    This command will display a list of files in the archive.

    Next, extract the specific files you need by specifying their names with the -x option:

    $ tar -xvf archive.tar.xz path/to/specific/file

    Replace path/to/specific/file with the actual path of the file you want to extract. You can specify multiple files separated by space if needed.

    For example, to extract file1.txt and file2.txt, use:

    $ tar -xvf archive.tar.xz file1.txt file2.txt

    This command will extract only the specified files from the tar.xz archive.

    Extracting Specific Files from a tar.xz Archive
    Extracting Specific Files from a tar.xz Archive

Conclusion

Extracting XZ compressed files on Linux is straightforward with the right tools. By following the steps outlined in this tutorial, you can easily manage XZ archives on your system. Make sure to verify the installation of XZ utilities and use the appropriate commands to decompress your files effectively. Additionally, listing the contents of a tar.xz archive before extracting and extracting specific files from the archive provides flexibility in handling large archives.

Troubleshooting

When dealing with XZ compressed files, you may encounter some common errors. Here are the most frequent error messages and their solutions:

Error: tar (child): xz: Cannot exec: No such file or directory
Solution: This error occurs when the xz utility is not installed on your system. To resolve this, install the XZ utilities as shown in the tutorial:

# apt-get install xz-utils

or

# yum install xz

Error: tar (child): Error is not recoverable: exiting now
Solution: This error indicates a problem with the archive file itself. Ensure that the file is not corrupted and that you have the correct permissions to read the file. You can check the integrity of the archive with:

$ xz -t filename.xz

This command tests the archive for integrity. If the file is corrupted, you will need to download or transfer it again.

Error: tar: Child returned status 2
Solution: This error is related to the previous errors and generally occurs when the xz utility is missing or the file is corrupted. Ensure xz is installed and the archive file is intact.

By following these solutions, you can troubleshoot and resolve common issues encountered while handling XZ compressed files on Linux.



Comments and Discussions
Linux Forum