2024-03-20
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 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
xz
offers numerous options for fine-grained control over the compression process. Some notable ones include:
-k
: Keep the original file after compression. Useful for testing or as a safety measure.-T
: Specify the number of threads for parallel compression. This can speed up compression on multi-core systems. Example: xz -T4 *.txt
uses 4 threads.-9
: Set the compression level (0-9). Higher levels generally result in better compression but take longer. The default is 6.--verbose
: Provides more detailed output during compression and decompression.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.