Gzip Command in Linux

The Gzip is the popular compression algorithm to reduce the size of a file with keeping file in original mode, ownership and timestamp. It also known as the .gz file format. The gzip utility is used to compress and decompress the files. In this guide, we will show you how to use the gzip command.

gzip Command Syntax

The basic syntax for the gzip command is as follows:

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

Using gzip, you can compress single file and generate compressed file for each file. The compressed file will be end with the either .gz or .z extension.

If your requirement is to compress multiple files or folders in to one file, you should first create a Tar archive file and need you can compress the .tar file with Gzip.

Usually, the Gzip is used to compress the text files, Tar archives, and web pages. It’s useless to use gzip for compress images, audio, PDF documents, and other binary files as they are already compressed.

The regular files can be compressed by gzip and the symbolic links are ignored.

Compress Files with gzip

You can compress a single file using the gzip command followed by the filename:

gzip filename

It will delete the original file and creates a filename.gz file. Gzip keeps the original file timestamp, mode, ownership, and name in the compressed file.

Keep Original File

If you would like to keep the original file instead of delete after compression, use -k option with gzip command:

gzip -k filename

Verbose output

To see the percentage of reduction and the names of the files that are being processed, use the -v option:

gzip -v filename
filename: 9.5% -- replaced with filename.gz

Compress multiple files

To compress the multiple files, pass multiple file names as arguments. For example, to compress the files named filename1, filename2, filename3, you would run the following command:

gzip filename1 filename2 filename3

As an output the above command will create the three different files named with filename1.gz, filename2.gz, filename3.gz .

Compress all files in a directory

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

gzip -r directory

gzip will recursively traverse through the whole directory structure and compress all the files in the directory and it’s subdirectories.

Change compression level

You can specify the range if compression levels from 1 to 9. For fastest compression with minimal compression ratio, you can pass -1 or --fast option. For the maximum compression ratio and slowest compression you should use the -9 or --best option with the gzip command. By default, the compression level is -6.

For example, for maximum compression, type:

gzip -9 filename

Compression is a CPU-intensive task, the higher the compression level, the longer the process takes.

Using standard input

You can create a .gz file from the standard input, just pipe the output if the command to gzip. For example, to generate a Gzip MySQL database backup file, you would run:

mysqldump database | gzip -c > database.sql.gz

The gzip will consider input which is the output of the mysqldump command.

Decompressing Files with gzip

Use the -d option, to decompress a .gz file:

gzip -d filename.gz

You can use the gunzip command to decompress a Gzip file. This command is basically an alias of gzip -d:

Keep the compressed file

Same as when compressing a file, the -k option tells gzip to keep the input file, in this case, that is the compressed file:

gzip -dk filename.gz

Decompress multiple files

You can decompress multiple files in single command by passing the filenames as arguments:

gzip -d filename1.gz filename2.gz filename3.gz

Decompress all files in a directory

Use -d and -r options to decompress all files in a given directory recursively:

gzip -dr directory


Using Gzip you can reduce the size of a given file. The gzip command allows you to compress and decompress files. To learn more about the gzip command, visit Gnu gzip documentation page.

If you have any questions or feedback, leave a comment below.

Exit mobile version