Home Beginner's Guide Guide to Zipping and Unzipping .xz Files in Linux

Guide to Zipping and Unzipping .xz Files in Linux

This guide will walk you through the process of compressing (zipping) and decompressing (unzipping) .xz files using the command line in Linux.

by Divya Kiran Kumar
xz files in linux

It’s always a thrill to share my Linux experiences with you. Today, we’re diving into a very specific topic that might seem a bit challenging if you’re new to the world of Linux. I promise to keep it simple, just like how I enjoy my morning cup of coffee – black, no sugar. So, without further ado, let’s delve into how to unzip XZ files in Linux, a topic that may seem trivial, but often causes a frown or two.

Introduction to XZ files

Firstly, it’s crucial to understand what XZ files are. XZ is a lossless data compression file format that incorporates the LZMA/LZMA2 compression algorithms. Don’t let the technical jargon intimidate you! In essence, XZ files are similar to other compression formats like .zip or .rar that you might be more familiar with. But in my personal experience, XZ offers a better compression ratio, although it can be slightly slower. That’s a trade-off I’m willing to make, especially when I need to squeeze every last bit of space out of my precious SSD.

Tools for unzipping XZ files

To unzip XZ files in Linux, we need a tool that can handle the LZMA/LZMA2 compression. The most common tool for this task is xz-utils. It is typically preinstalled on many Linux distributions. I personally appreciate xz-utils for its efficiency and simplicity, but admittedly, I sometimes wish it had a progress bar, which can be handy for larger files.

To check if xz-utils is installed on your system, open your terminal (Ctrl + Alt + T is your friend here!) and type:

xz --version
checking if xz is installed

Checking if XZ is installed

If it returns the version information, then you’re all set. If not, don’t fret! Here’s how you can install it:

For Ubuntu/Debian:

sudo apt-get install xz-utils

For CentOS/RHEL:

sudo yum install xz

For Fedora:

sudo dnf install xz

See, not too hard, right? Now we’re ready to unzip those XZ files!

How to unzip XZ files

Alright, let’s get to the juicy part. Navigate to the directory with the XZ file. For example, if you have a file named example.xz in your Downloads directory, you would use the cd command like this:

cd ~/Downloads

To extract the FOSSLinux.xz file, type:

unxz FOSSLinux.xz

That’s it! Your file has been decompressed. You might find the lack of visual feedback during the unzipping process a bit disconcerting, but trust the process and give it a few moments. If the operation completes without any output, that usually indicates success. You can verify by using the ls command to check your files.

unzipping xz file

Unzipping XZ file

Compressing a folder to an XZ file in Linux

One of the great things about Linux (besides its inherent charm that grows on you) is that once you learn how to unzip an XZ file, it’s almost a cakewalk to do the reverse. I say ‘almost’ because compressing a folder requires a tiny bit more finesse. But worry not! I’ll guide you through it.

Let’s assume you have a folder named FOSSLinux_tuts that you want to compress into an XZ file.

First things first, you need to understand that the xz command, similar to gzip or bzip2, can only compress single files. You might be wondering, “Wait, didn’t we just say we’re compressing a folder?” Yes, we did, and yes, we will. That’s where the magic of tar comes in.

The tar (tape archive) command can bundle many files and directories into one. Now, we can use the combined powers of tar and xz to compress a folder. So, our strategy is simple: bundle the files with tar and then compress them with xz.

Here’s the command to do that:

tar -cJf FOSSLinux_Tuts.tar.xz FOSSLinux_Tuts/
compressing a folder to xz file

Compressing a folder to XZ file

Let’s break this down:

  • tar is the command we’re using.
  • -c stands for create, as we’re creating a new archive.
  • J specifies that we want to compress the archive using xz.
  • -f allows us to specify the name of the archive file we’re creating, which is FOSSLinux_Tuts.tar.xz in this case.
  • Finally, FOSSLinux_Tuts/ is the directory we’re compressing.

This command creates an XZ compressed tar archive of your folder. If you ever miss the visual feedback, rest assured that your terminal is working its magic in the background. It’s like a silent guardian, always working, but only speaking when necessary.

You can now distribute this FOSSLinux_Tuts.tar.xz file or store it for later use. To access the files, you’ll need to decompress and extract the contents, which we’ve already covered.

Unzipping multiple XZ files

So, what if you have multiple XZ files that you want to unzip at once? The concept is the same, just extended. The command would look something like this:

unxz *.xz

The asterisk (*) here is a wildcard character that represents any character or group of characters. This command will unzip all the XZ files in the current directory.

Compressing multiple folders to an XZ file in Linux

With Linux, you can take the power of compression even further by compressing multiple folders into a single XZ file. This process mirrors the steps we’ve already discussed, with a minor tweak.

Let’s assume you have two folders, myFolder1 and myFolder2, that you want to compress into a single XZ file.

Firstly, you would use the tar command to bundle these folders together. Then, you would compress this bundle using xz. Here’s how you do it:

tar -cJf myArchive.tar.xz myFolder1/ myFolder2/

As before:

  • tar is the command we’re using.
  • -c stands for create, as we’re creating a new archive.
  • J specifies that we want to compress the archive using xz.
  • -f allows us to specify the name of the archive file we’re creating, which is myArchive.tar.xz in this case.

Finally, myFolder1/ and myFolder2/ are the directories we’re compressing.
That’s it! You now have a single XZ file, myArchive.tar.xz, which contains the contents of both myFolder1 and myFolder2.

The ability to compress multiple folders into a single XZ file makes data distribution and backup significantly easier. Instead of dealing with numerous files and folders, you can handle a single XZ file. It’s one of the many reasons why I love working with Linux. The functionality and efficiency never cease to amaze me!

Top 10 Frequently Asked Questions (FAQs)

I’ve put together the top 10 FAQs based on my experiences and those of other Linux enthusiasts I’ve interacted with.

What is an XZ file?

XZ is a lossless data compression file format that uses the LZMA/LZMA2 compression algorithms.

What tools can I use to unzip XZ files in Linux?

You can use xz-utils to unzip XZ files in Linux.

How can I install xz-utils on my Linux distribution?

The installation command varies depending on your Linux distribution. For Ubuntu/Debian, it’s sudo apt-get install xz-utils.

For CentOS/RHEL, it’s sudo yum install xz. For Fedora, it’s sudo dnf install xz.

How do I unzip a single XZ file?

Use the command unxz filename.xz.

How do I unzip multiple XZ files?

Use the command unxz *.xz.

How can I verify if the XZ file was unzipped successfully?

Use the ls command to list the files in your current directory.

Can I zip files into XZ format?

Yes, you can use the xz command followed by the filename.

Why do we use XZ files?

XZ files offer a better compression ratio than many other formats, making them ideal for maximizing storage space.

Can XZ files be opened on Windows or Mac?

Yes, there are several software options available, such as 7-Zip for Windows and Keka for Mac.

Is there a GUI for xz-utils?

Yes, several archive managers like File Roller (GNOME) and Ark (KDE) support XZ files.

XZ vs ZIP: Unraveling the differences

Ah, the classic debate in the compression world: ZIP vs XZ. If you’ve ever found yourself in a quandary over which file format to use, I can assure you, you’re not alone.

Let’s explore these two popular compression formats and analyze their key differences. By the end, you should be able to make a more informed decision on which one to use based on your needs.

What is ZIP?

ZIP, born in the early 90s, is one of the most widely recognized compression formats. It uses the DEFLATE compression algorithm, which is a combination of LZ77 and Huffman coding. ZIP is ubiquitous due to its ease of use and universal compatibility. It is supported by many software utilities across various platforms like Windows, macOS, and Linux.

What about XZ?

XZ, a relatively new kid on the block, is a compression format that uses the LZMA/LZMA2 (Lempel-Ziv-Markov chain Algorithm) compression algorithm. It’s known for its impressive compression ratio, often outperforming other formats. While not as universally recognized as ZIP, XZ has gained significant popularity among the Linux crowd.

The key differences

Compression Ratio: The most prominent difference between ZIP and XZ is the compression ratio. XZ generally provides a higher compression ratio than ZIP, meaning it can make files smaller. However, this comes at the cost of speed. XZ compression is slower compared to ZIP. But if storage space is your primary concern, XZ is a clear winner.

Speed: ZIP shines in terms of speed. It is faster at both compressing and decompressing files. So, if you’re working with time-sensitive tasks or less powerful hardware, ZIP might be a better choice.

Compatibility: ZIP is more universally recognized and supported across various platforms out of the box. This means that if you’re sharing files with others who may not be using Linux, ZIP could be the safer choice. XZ, while it is natively supported on Linux, might require additional software on other platforms.

Multiple Files: ZIP can compress multiple files or directories into a single archive without the need for additional tools. With XZ, you need to use tar to bundle multiple files or directories before compressing.

In essence, does XZ reduce file size? Yes, it does! In fact, it often compresses files to a smaller size than ZIP. However, there’s more to consider than just the compression ratio. Your specific needs, including factors like speed, compatibility, and whether you’re compressing multiple files or directories, should guide your choice.

Like many aspects of Linux, the choice between ZIP and XZ isn’t a one-size-fits-all scenario. It’s about picking the right tool for the job, and now, you’re well-equipped to do just that!

Troubleshooting common issues

Sometimes things don’t go as smoothly as we wish. Here are some common issues that you might run into while unzipping XZ files, along with their solutions.

Issue: Command not found error.

Solution: Ensure xz-utils is installed. Install it using the commands I provided above if it’s not.

Issue: Permission denied error.

Solution: Make sure you have read permissions for the file. Use sudo if necessary, but remember, with great power comes great responsibility!

Issue: No space left on device error.

Solution: Check your storage space. Delete unnecessary files or move your XZ file to a directory with enough space.

Issue: File format not recognized.

Solution: Verify that your file is indeed an XZ file. You can use the file command for this: file example.xz.

Wrapping up the compression journey

And there you have it, dear Linux enthusiasts, our exploratory voyage into the realm of compressing and decompressing XZ files has reached its harbor. We’ve charted the waters of what XZ files are, why they’re beneficial, how to handle them in various scenarios, and answered some of the most frequently asked questions.

Through this journey, we’ve discovered that Linux, while seemingly intimidating, is a treasure trove of powerful and efficient tools. By comprehending the tar and xz commands, we’ve gained the ability to control our data in a precise, compact, and efficient manner.

Just remember, every step you take in Linux is part of your journey, from the simplest command to the most complex script. Don’t shy away from the challenges; instead, embrace them, learn from them, and above all, enjoy the process. Like a skillful sailor navigating the vast ocean, you too will become adept at navigating the seemingly endless ocean of Linux commands.

I hope this guide has left you with not only the knowledge to zip and unzip XZ files in Linux but also the confidence to explore further, to ask more questions, and to continue delving deeper into the fascinating world of Linux.

You may also like

Leave a Comment


FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews, FOSS Linux is the go-to source for all things Linux. Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX