How to compress files in Linux using gzip command

gzip (stands for General file (de)compression) is one of the most popular compression algorithms that allows you to reduce the size of a file and free up some space when the file system is running out of disk space.

It compresses the file with a '.gz' extension and preserves file permissions, ownership patterns (RWX), access and modification timestamps. It compresses regular files and ignores symbolic links.

It is most popular in Linux and Unix operating systems and is still widely used today.

Make a Note: gzip compresses only single files and creates a compressed file for each given file, not for a directory. If you want to compress multiple files or directory into one file, you should use tar archive.

OptionDescription
-k –keepKeep (don’t delete) input files during compression or decompression.
-c –stdoutWrite output on standard output and keep original files unchanged.
-l –listTo displays the statistics of the compressed files.
-r –recursiveTravel the directory structure recursively and compress all the files.
-d –decompressTo decompress a file
-1 –fastTo get minimum compression ratio at the fastest speed
-9 –bestTo get maximum compression ratio at the slowest speed

Installing gzip in Linux

By default the gzip package would have already installed. However, if it is not installed, you can install it now using the package manager as shown below

yum install gzip     #RHEL 7
dnf install gzip     #RHEL 8/9, Fedora
apt install gzip     #Debian/Ubuntu
zypper install gzip  #OpenSUSE
pacman -S gzip       #Arch Linux

Syntax:

The general syntax for the gzip command is as follows:

gzip [Option] [File_Name]

Compressing files with gzip

To compress a file, use the filename followed by the ‘gzip’ command.

gzip myfile

In this example, gzip creates the file ‘myfile.gz’ and deletes the original file.

Keep the original file with gzip

By default, gzip removes the original file, but if you want to keep it, use the '-k' option:

gzip -k myfile

Also, you can use the '-c' option with gzip to keep the original files unchanged. It writes output on standard output and redirects output to a file.

gzip -c myfile > myfile.gz

To compress multiple files into a single gzip, run:

gzip -c myfile_1.txt myfile_2.txt > myfiles.gz

List the compressed file contents

gzip displays the statistics of the given compressed files with the '-l' option, but not the actual file contents.

gzip -l myfile.gz

         compressed        uncompressed  ratio uncompressed_name
                 41                  13   0.0% myfile

It shows, following details:

  • compressed size: size of the compressed file
  • uncompressed size: size of the uncompressed file
  • ratio: compression ratio (0.0% if unknown)
  • uncompressed_name: name of the uncompressed file

Add the '-v' option to get more information:

gzip -lv myfile.gz

method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 0762e828 Feb 24 00:55                  41                  13   0.0% myfile

Use the ‘zcat’ command to view the actual contents of a gzip compressed file.

zcat myfile.gz

This is a test file for 'gzip' article preparation

Compressing multiple files

You can also compress multiple files at once, but it will create separate compressed files for each given file.

For example: To compress the files named myfile1, myfile2, myfile3, you would run the following command and it will create three compressed files named myfile1.gz, myfile2.gz, myfile3.gz.

gzip myfile1 myfile2 myfile3

Compressing all files in a directory

To compress all files in a given directory, use the '-r' option:

It recursively go across the entire directory structure and compresses all files in the directory and its sub-directories.

gzip -r mydirectory

Understanding compression level

gzip allows to change the compression levels, from 1 to 9. The speed and level of compression vary based on levels. By default, it use compression level '-6'.

  • -1 or --fast indicates the fastest compression method (less compression ratio).
  • -9 or --best indicates the slowest compression method (best compression ratio).

To get maximum compression, run:

gzip -9 myfile

Viewing contents of gzip file

You can use any of the below commands to view the contents of a gzip file.

zcat myfile.gz
or
zmore myfile.gz
or
zless myfile.gz
Compress files in Linux using gzip command

Decompressing files with gzip

To decompress a file using the gzip command, use the '-d' option:

gzip -d myfile.gz

Alternatively, you can use the ‘gunzip’ command to decompress a file. This is an alias of ‘gzip -d’.

gunzip myfile.gz

Closing Thoughts

In this tutorial, we’ve shown you how to use gzip command to Compress and Decompress files in Linux.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *