Use of gzip command in Linux – with examples – Easy Guide series

gzip is basically a compression program which reduces size of file to create a new file with gz extension. This guide covers use of gzip command in Linux.. When a file is compressed using gzip a new file is created with same name with .gz file extension. Gzip use Lempel-Ziv (LZ77) algorithm to compress file.

It is worth noting that compression program will not give same result in all applications. Some file are already compressed by default such as jpeg, mp3. These files are better uncompressed you may archive these using tar command but slight or no improvement may be observed.

For detailed documentation, developments and downloads visit – GZIP Page

Examples – use of gzip command in Linux

Compress a file using gzip command

To compress a file using gzip command type gzip followed by file name

gzip filename

This will create a file with same file name but with gz extension.  Also original file will be removed.

Below command compresses test.txt file and create test.txt.gz

gzip test.txt
How to use gzip command
gzip command with verbose - syntax gzip -v
gzip -v

Above image shows usage gzip -v command, here it shows details regarding compression achieved by use of gzip command in Linux.

Compress mulitple file using gzip command

To compress multiple files type gz command followed by file names with single space separating file names. Below command compresses multiple files

gzip test1.txt test2.txt test3.txt test4.txt

This command will remove original files and create test1.txt.gz, test2.txt.gz, test3.txt.gz and test4.txt.gz

Get details of how much space saved by compressed file using gzip command

Using switch -l or –list information can be derived about how much is file size compressed and uncompressed file along with compression ratio.

gzip -l filename.gz
gzip -l command shows detailed information regarding size of compressed and uncompressed size of file
gzip -l

Compress a Directory using gzip command

Using –r compresses through directory structure recursively. gzip will move into directory and compress all the files present in directory.

gzip -r directoryname

As you can see above to compressed files are created inside folder. The original folder remains as such but files inside are replaced with their compressed version.

It does not create single file, better results can be achieved using tar command on directory. See it here How to use of tar command in Linux – compressing and archiving files

Change compression level in gzip command

gzip uses compression level 1 to 9, where 1 is least compressed and 9 uses best compression for gzip. It should be kept in mind low compression takes less time to complete than higher compression.

gzip -1 test.txt

The above command will compress file using least compression and command will be executed faster. While command below will compress file using higher compression rate and command will take more time for executing.

gzip -9 test.txt
gzip - how to use different compression ratio
Different compression ratio gzip

Above example you can see that when using compression level 1, 32.5% compression was achieved and with compression level -9, 33.1% compression is achieved. Try it with some of your files and check difference in different compression levels

Decompress a file using gzip command

To compress a file using gzip command type gzip followed by -d and file name

gzip -d test.txt.gz

This command will create test.txt and remove compressed file test.txt.gz