Skip to content

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 gzip

gzip Overview

gzip is a file compression and decompression tool used in Linux systems. The gzip command reduces the size of the named files using Lempel-Ziv coding (LZ77). gzip replaces the original file with a compressed version that has a .gz extension. Compressed files can be significantly smaller in size, saving storage space and reducing file transfer times.

Create a Practice File

To create a file for practicing gzip with in up coming exercises, follow the below:

Exercise: Creating files for practice with the gzip command:

journalctl > gzip-test-default.txt
ls -lR /etc /usr/bin /usr/share/doc >> gzip-test-default.txt 2> /dev/null
cp gzip-test-default.txt gzip-test-fast.txt
cp gzip-test-default.txt gzip-test-best.txt

Note down the original size of the gzip-test-default.txt file as this will be used to compare file sizes post compression in later exercises.

ls -lh gzip-test-default.txt

58M in my case.

-rw-rw-r-- 1 dtvlinux dtvlinux 58M Oct 21 15:53 gzip-test-default.txt

Compression Using gzip

When using gzip 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 gzip with default compression:

gzip -v gzip-test-default.txt

Exercise: Using gzip with fastest compression:

gzip -v --fast gzip-test-fast.txt

Exercise: Using gzip with the best compression:

gzip -v --best gzip-test-best.txt

Exercise: Comparing compressed file sizes:

ls -lh gzip-test-*.txt.gz

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 7.7M Oct 21 15:53 gzip-test-best.txt.gz
-rw-rw-r-- 1 dtvlinux dtvlinux 7.8M Oct 21 15:53 gzip-test-default.txt.gz
-rw-rw-r-- 1 dtvlinux dtvlinux 9.4M Oct 21 15:53 gzip-test-fast.txt.gz

Viewing Compressed Data Using zcat

zcat uncompresses either a list of files on the command line and writes the uncompressed data to standard output (typically the screen). This is useful if you want to view the file but you want it to remain compressed afterwards.

Exercise: View the contents of a compressed file using zcat:

zcat gzip-test-default.txt.gz

Without using a pager such as less or more the whole file will be displayed but too quick to read. Therefore run the command again, but pipe the output through the less command:

zcat gzip-test-default.txt.gz | less

Now you can press Space to page through the file. These practice files may be fairly large so you may wish to quit out of the pager by pressing Q.

Decompression Using gzip/gunzip

There are two ways to decompress files that were compressed using gzip. You can either use gzip itself with the -d, --decompress or --uncompress options, or you can use the gunzip command.

Using the -v flag will show the compression percentage as well as the new file name.

Exercise: Using gzip -d to decompress a file:

gzip -v -d gzip-test-default.txt.gz

Exercise: Using gzip --decompress to decompress a file:

gzip -v --decompress gzip-test-fast.txt.gz

Exercise: Using gunzip to decompress a file:

gunzip -v gzip-test-best.txt.gz

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.