commands.page Website Logo

  • Home
  • Categories
  • Search

How to Compress a Single File Using XZ on Ubuntu Terminal

This is an article about compressing files using the xz command in the terminal of an Ubuntu system. In this guide, you will learn how to use the powerful and efficient xz utility to reduce the size of your data by applying advanced compression algorithms such as LZMA (Lempel-Ziv-Markov chain algorithm) or other supported methods. Read this article to find out about the benefits of using xz, its installation process, command-line options, and common use cases.

Introduction to XZ Compression

XZ is a general-purpose data compression software written by Lasse Collin as an open-source tool. It’s designed for maximum speed while achieving reasonable compression ratios, making it an ideal choice for compressing files on disk or transmitting them over the network. Unlike older tools like gzip, which uses DEFLATE algorithms, xz provides better compression efficiency through its support of multiple compression methods.

Advantages of XZ

  • High Compression Ratio: Achieves significantly higher compression ratios compared to other popular tools.
  • Multi-threading Support: Can utilize multiple CPU cores for faster compression and decompression times.
  • Extensive Configuration Options: Offers a wide range of options that allow you to tweak the level of compression, memory usage, and other parameters.

Installing XZ on Ubuntu

Before diving into using xz, ensure it’s installed on your system. The package can be easily added via the terminal:

sudo apt-get update sudo apt-get install xz-utils

This command installs the necessary tools including the core xz utility and additional commands like unxz.

Basic Usage of XZ

To start compressing a file, you need to know how to use basic options. The simplest way to create an .xz compressed archive is by using:

xz filename.txt

This command will produce a new file called filename.txt.xz, which contains the original content but with reduced size due to compression.

Decompressing Files

To decompress a file back to its original form, use the following command:

unxz filename.txt.xz

Or you can extract it using xz itself by specifying -d (decompress) option:

xz -d filename.txt.xz

Checking File Integrity

It’s also possible to check if a compressed file was created correctly without extracting it. Use the -tv options for this purpose:

xz -tv filename.txt.xz

Advanced XZ Commands and Options

Beyond basic operations, xz offers numerous command-line switches that provide control over various aspects of compression.

Setting Compression Level

You can adjust how aggressively xz compresses data by changing the compression level. The default setting is -6, but you might want to experiment with different values depending on your performance needs:

xz -5 filename.txt # lower than default for faster compression at a cost of smaller file size xz -9 filename.txt # higher than default for slower but more effective compression

Memory Usage Control

For large files, memory usage becomes critical. You can limit the amount of RAM used during compression with -m option followed by an appropriate value:

xz --memory=1024k filename.txt

Parallel Compression

To take advantage of multi-core processors for faster processing times, enable parallel execution using --threads=N, where N represents the number of threads to use:

xz --threads=4 filename.txt

Use Cases and Practical Examples

Understanding when to apply specific commands can greatly enhance your workflow. Here are some scenarios where you might find xz particularly useful.

Compressing Multiple Files

You often need to compress several files or directories simultaneously. This is straightforward with xz. Suppose you have a folder named “documents” containing multiple text files:

cd documents tar cvf - . | xz > documents.tar.xz

Here, tar packages all files together first before passing them through xz, resulting in an .tar.xz archive.

Creating Archives with XZ

Another common task involves creating archives containing multiple compressed files. For instance:

cd ~/pictures find . -type f -name "*.jpg" | xargs tar cf - | xz > photos.tar.xz

This command searches for JPEG images, then creates a tarball and compresses it into an .xz file.

Efficient Backups

For backing up data efficiently, xz combined with other utilities can be incredibly powerful. Consider creating daily snapshots of a specific directory:

cd /path/to/backup tar cvf - . | xz > backup_$(date +%Y%m%d).tar.xz

This command generates a tarball and compresses it, appending today’s date to the file name for easy identification.

Conclusion

Mastering xz in Ubuntu provides you with an indispensable toolset for managing large datasets effectively. Whether you’re dealing with huge log files, preparing backups, or simply needing high-quality compression on individual files, xz delivers superior performance and flexibility. With this guide, you now possess the knowledge needed to leverage its full potential right from your terminal.

Remember, practice makes perfect; experiment with different settings and explore more advanced features of xz. Happy compressing!

Last Modified: 23/03/2018 - 00:23:18