How to Use the gzip Command in Linux

Suraj Joshi Feb 02, 2024
  1. gzip Syntax:
  2. Compress Files Using gzip
  3. Decompress Files Using gzip
  4. List Details of Compressed Files Using the gzip Command
How to Use the gzip Command in Linux

gzip is a command-line utility that allows us to create and extract .gz archives. The gzip command creates a compressed file for each file. If we want to compress multiple files or a directory into a single compressed file, we need to create a .tar archive using the tar command and then compress the .tar archive using the gzip command. It is not recommended to compress audio files, image files, and other binary files using the gzip command as they are already compressed.

gzip Syntax:

gzip [OPTION]... [FILE]...

[FILE]... represents files to be compressed, and [OPTION]... represents options to customize the compression or decompression process.

Compress Files Using gzip

The command to compress files using gzip is

gzip filename

This compresses filename using gzip into filename.gzip and deletes the original file.

If we want to keep the original file, we use the -k option along with the command.

gzip -k main.py

It compresses the file main.py into main.py.gz and keeps the original file.

Another option to retain the original file is to use the -c option, which directs the gzip command to write output on standard output, and we redirect the standard output to the .gzip file.

gzip -c main.py > main.py.gz

It also compresses the file main.py into main.py.gz by retaining the original file.

To get the verbose output, we use the -v option.

gzip -v main.py

Output:

main.py:         18.6% -- replaced with main.py.gz

From the output, we can see the percentage reduction of the file’s size during the compression.

Compress Multiple Files at Once Using gzip

To compress multiple files at once using gzip, we use the gzip command, followed by file names to be compressed separated by a space.

gzip main.py file.py process.py

It compresses the files main.py, file.py and process.py into respective .gz files as main.py.gz, file.py.gz, and process.py.gz.

Compress All the Files Inside a Directory Using gzip

To compress all the files inside a directory using gzip, we use the -r option with the gzip command.

gzip -r test_dir

It compresses every individual file inside test_dir to their respective compressed files ending with .gz.

Decompress Files Using gzip

To decompress a .gz file using gzip, we use the -d option with gzip command.

gzip -d filename.gz

It decompresses filename.gz using gzip into filename and deletes the compressed file.

We can also decompress the .gz files using the gunzip command.

gunzip main.py.gz

It decompresses main.py.gz using gzip into main.py and deletes the compressed file main.py.gz.

If we want to retain the compressed file also during decompression, we use the -k option along with the command.

gzip -dk main.py.gz

It decompresses the file main.py.gz into main.py by retaining the decompressed file.

Decompress Multiple Files at Once Using gzip

To compress multiple files at once using gzip, we use the gzip command with -d option followed by file names to be decompressed separated by a space.

gzip -d main.py.gz file.py.gz process.py.gz

It decompresses the files main.py.gz, file.py.gz and process.py.gz into individual files as main.py, file.py, and process.py.

Decompress All the Files Inside a Directory Using gzip

To decompress all the files inside a directory using gzip, we use the -r option along with the -d option in gzip command.

gzip -dr test_dir

It decompresses every individual compressed file inside test_dir to their respective decompressed.

List Details of Compressed Files Using the gzip Command

The gzip command can also be used to get a compressed file’s details when used with the -l option.

gzip -l main.py.gz

Output:

         compressed        uncompressed  ratio uncompressed_name
                 28                   0   0.0% main.py
                 

It lists all the details about the main.py.gz file.

To get more details, we add the -v option.

gzip -l main.py.gz

Output:

method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 00000000 Sep  8 22:15                  28                   0   0.0% main.py
Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn