xz

2024-03-20

Compressing Files with xz

The core command for compressing files using xz is simply xz. Let’s look at many ways to use it:

Single File Compression:

To compress a single file, say my_large_file.txt, use the following:

xz my_large_file.txt

This creates a compressed file named my_large_file.txt.xz. Note the .xz extension automatically added by xz.

Specifying the Output Filename:

For more control over the output filename, use the -z option followed by the desired output:

xz -z my_large_file.txt -o my_archive.xz

This compresses my_large_file.txt and saves the result as my_archive.xz.

Compressing Multiple Files:

xz can efficiently handle multiple files simultaneously:

xz file1.txt file2.txt file3.txt

This compresses each file individually, creating file1.txt.xz, file2.txt.xz, and file3.txt.xz.

Using Wildcards:

Wildcards, like *, are useful for batch processing:

xz *.txt

This compresses all files ending in .txt in the current directory.

Decompressing Files with xz

Decompressing files is just as straightforward. The primary command is xz -d:

Single File Decompression:

To decompress my_archive.xz, use:

xz -d my_archive.xz

This restores the original my_large_file.txt.

Decompressing Multiple Files:

Similar to compression, you can decompress multiple files at once:

xz -d file1.txt.xz file2.txt.xz

Decompressing with Output Redirection:

If you want to decompress and simultaneously redirect the output to a new file:

xz -d my_archive.xz > my_restored_file.txt

Advanced xz Options

xz offers numerous options for fine-grained control over the compression process. Some notable ones include:

By mastering these commands and options, you can harness the power of xz for efficient and effective file management on your Linux system, optimizing storage space and streamlining your workflows. Remember to always back up your important data before performing any compression or decompression operations.