This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

gzip command is used to compress files in linux systems. Each single file is compressed into a single file. The compressed file consists of a GNU zip header and deflated data. By default original file will be replaced by the compressed file ending with extension (.gz).

To decompress a file you can use gunzip command and your original file will be back.

In this tutorial, we are using commands with examples. So, replace the “path” with the file path, “filename” with the name of the file and “.ext” with the extension of the file.

Installing gzip

Debian Based Systems

root@codesposts:~$ apt-get install gzip

Red-Hat Based Systems

root@codesposts:~$ yum install gzip

General Syntax

Following is the general syntax for the gzip command

 gzip [Options] [filenames.ext]

Compressing A Single File

If you want to compress a file using gzip command, you can run the command like this:

root@codesposts:~$ gzip filename.ext

Compressing Multiple Files

If you want to compress multiple files using gzip command, you can run the command like this:

root@codesposts:~$ gzip file1.ext file2.ext file3.ext

This command will compress all of the files in separated zip files having single file in each zip file

Compressing Multiple Files Into A Single Zip File

If you want to compress multiple files into a single zip file, you can use the cat and gzip command with pipe command.

root@codesposts:~$ cat file1.ext file2.ext file3.ext | gzip zipname.gz

Checking Compression Ratio While Compressing Files

If you want to display the compression ratio after the compression of the files, you can use the option -l with the gzip command

root@codesposts:~$ gzip -l filename.ext

Forcing The Compression Of A File

Sometimes a file cannot be compressed. Perhaps you are trying to compress a file called “filename” but there is already a file called “filename.gz”. In this instance, the gzip command won’t ordinarily work.
To force the gzip command to do its stuff simply use -f option:

root@codesposts:~$ gzip -f filename.ext

Keeping The Source File After Compression

By default, when you compress a file using gzip command, you end up having a file with the extension gz and the original source file is lost. But if you want to keep the source file after the compression, you can use the option -k with the gzip command.

root@codesposts:~$ gzip -k filename.ext

Displaying License Of gzip

If you want to display the license of the gzip compressed file, you can use the option -L with the gzip command.

root@codesposts:~$ gzip -L zipname.gz

Compress All Files In A Directory

If you want to compress all the files in the directory and subdirectories, you can use the option-r with the gzip command.

root@codesposts:~$ gzip -r /path/to/directory

Set The Compression Level

If you want to set the compression level before compressing the file manually, you can use the option [1 to 9] with the gzip command. 1 for least compression level and 9 for maximum.

root@codesposts:~$ gzip -1 filename.ext

OR

root@codesposts:~$ gzip -5 filename.ext

OR

root@codesposts:~$ gzip -9 filename.ext

Displaying Percentage Reduction

If you want to display the name and percentage reduction for each file compressed or decompressed, you can use the option-v with the gzip command.

root@codesposts:~$ gzip -v filename.ext

filename.ext:      23.2% -- replaced with filename.ext.gz

Test Validity Of A Compressed File

If you want to test the validity of a compress file, you can use option-t with the gzip command.

root@codesposts:~$ gzip -t filename.gz

If the file is valid, there will be no output

Compress A Directory

The gzip command will not be able to compress a directory because it can only compress a single file. To compress a directory you have to use tar command.

root@codesposts:~$ tar cf - [directory] | gzip > [zipname]  

Decompress Zip File Using gzip

If you want to decompress a zip file using the gzip command, you can use the option-d with the gzip command.

root@codesposts:~$ gzip -d zipname.gz