Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
LPI Linux Essentials Exam 010-160 - Topic 3.1 - Compression with xz
xz Overview
The xz
command is a Linux utility used for file compression and decompression. It employs the LZMA (Lempel-Ziv-Markov chain-Algorithm) compression algorithm to reduce the size of files. When you use the xz
command to compress a file, it replaces the original file with a compressed version, typically having a .xz
extension. This compression process significantly reduces the file's size, which can be advantageous for conserving storage space and accelerating file transfers.
Create a Practice File
To create a file for practicing xz
with in up coming exercises, follow the below:
Exercise: Creating files for practice with the xz
command:
journalctl > xz-test-default.txt
ls -lR /etc /usr/bin /usr/share/doc >> xz-test-default.txt 2> /dev/null
cp xz-test-default.txt xz-test-fast.txt
cp xz-test-default.txt xz-test-best.txt
Note down the original size of the xz-test-default.txt
file as this will be used to compare file sizes post compression in later exercises.
ls -lh xz-test-default.txt
60M in my case.
-rw-rw-r-- 1 dtvlinux dtvlinux 60M Oct 23 08:27 xz-test-default.txt
Compression Using xz
When using xz
you can use different flags to regulate the speed of compression (-#
, --fast
, & --best
). Where -1
or --fast
indicates the fastest compression method, (less compression), and -9
or --best
indicates the slowest compression method, (best compression). The default compression level is -6
(which is biased towards high compression at expense of speed).
Using the -v
flag will show the compression percentage as well as the new file name.
Exercise: Using xz
with default compression:
xz -v xz-test-default.txt
Exercise: Using xz
with fastest compression:
xz -v --fast xz-test-fast.txt
Exercise: Using xz
with the best compression:
xz -v --best xz-test-best.txt
Exercise: Comparing compressed file sizes:
ls -lh xz-test-*.txt.xz
In my case we can see that --best
had a slight improvement over the default but a significant improvement over the --fast
option.
-rw-rw-r-- 1 dtvlinux dtvlinux 2.3M Oct 23 08:27 xz-test-best.txt.xz
-rw-rw-r-- 1 dtvlinux dtvlinux 2.4M Oct 23 08:27 xz-test-default.txt.xz
-rw-rw-r-- 1 dtvlinux dtvlinux 6.6M Oct 23 08:27 xz-test-fast.txt.xz
Decompression Using xz/unxz
There are two ways to decompress files that were compressed using xz
. You can either use xz
itself with the -d
, --decompress
or --uncompress
options, or you can use the unxz
command.
Using the -v
flag will show the compression percentage as well as the new file name.
Exercise: Using xz -d
to decompress a file:
xz -v -d xz-test-default.txt.xz
Exercise: Using xz --decompress
to decompress a file:
xz -v --decompress xz-test-fast.txt.xz
Exercise: Using gunzip
to decompress a file:
unxz -v xz-test-best.txt.xz
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.