Learn Gzip Command in Linux with Examples

2 min


This article contains helpful information and examples of Gzip command in Linux.

Gzip command in Linux is a lossless compression algorithm based on encoding LZ77 (Lempel-Ziv of 1977). It is also known as LZ1 compression, which is the basis for many lossy formats.

Compressing files using gzip creates a compressed archive with the extension .gz and preserves the file permissions, ownership modes (read/write/execute) and access/modify date timestamps. It compresses regular files and ignores symbolic links. The same program, gzip, can compress and decompress files in Linux.

Let’s take a look at some working examples.

gzip Command in Linux

Syntax

gzip [OPTIONs] [FILEs]

By definition, gzip compresses only one file, creating a compressed file with the extension .gz or .z. However, you should create a Tar archive first, if you want to compress multiple files or directories. Hence, the compressed files should be with extension .tar.gz or .tgz.

When to use gzip?

It is recommended that you should use gzip only to compress text stream files, and archives. And don’t use gzip to compress PDF, audio or video files because those files are already compressed according to their own formats.

Compress files with gzip

The most simple compress command with gzip is below, compressing one single file.

gzip file1

The above command will compress file1 and creates a file1.gz. The command also deletes the original file.

If you want to keep the original file, then use the -k switch

gzip -k file1

Redirect to standard output

Gzip also provides switches to write directly to the standard output using option -c. Here’s an example of creating a redirection and writing it into a different compressed file.

gzip -c file1 > file1_copy.gz

Compressing multiple files

Although gzip compresses one single file, you can pass multiple files as an argument and make separate compressed files. The following example creates three different compressed files based on the input.

gzip file1 file2 file3

The above command will create three compressed files file1.gzip, file2.gzip and file3.gzip.

Compression percentage

Like the zip command we discussed in the prior article, gzip also allows a range of compression levels. The value range is 1 to 9. And the default value is 6. The value 1 means fast compression with minimum compression ratio, and a value of 9 means slower compression with maximum compression ratio.

gzip -1 file1
gzip -9 file1

Standard input

You can also pass a file as standard output via a pipe to Gzip and compress it.

{your command} | gzip -c > file1.gz

Compress entire directory

Using the recursive switch of gzip, you can compress all the files and folders of a directory to a gz file.

gzip -r documents

Extracting files with gzip

The gzip also provides a -d switch to decompress the compressed files. All the above options can be combined with the -d switch to get desired output. Here are some of the decompress examples:

The basic command is below:

gzip -d file1.gz

The above command will extract the file1.gz and delete the .gz file.

If you want to keep the compressed file, use the -k option with -d.

gzip -dk file1.gz

Likewise, you can decompress multiple files, as shown below.

gzip -d file1.gz file2.gz file3.gz

Furthermore, to recursively decompress all the files and folders in a directory, use the -r option.

gzip -dr documents
Gzip example compressing a folder
Gzip example compressing a folder

View compressed file information

Using the -l switch, you can see the details about a compressed file, such as compression ratio, file name, etc.

gzip -lv file1.gz

Closing Notes

Gzip can comp and decompress files and folders via the command line in Linux. You can also use this program in your shell scripts to automate backups and many use cases.

To learn more, visit the manual of Gzip.

If you have any questions, drop a comment below.


Arindam

Creator and author of debugpoint.com. Connect with me via Telegram, 𝕏 (Twitter), or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments