Linux gzip Command Examples

Filed Under: UNIX/Linux

Linux offers an array of tools for compressing and decompressing files. In this tutorial, we take a look at the Linux gzip command tool.

Gzip is a file format as well as a software application which was created by Mark Adler and Jean-Loup Gailly and has been in use since the early Unix Systems.

Linux gzip command uses the (LZ77) Lempel-Ziv coding during its operations. Let’s dive in and see the various examples of how Gzip can be used.

Compress a File using Linux gzip Command

Compressing a file with gzip command is a walk in the park. Simply pass the file name as the argument as shown.

$ gzip [filename]

The final compressed file acquires a suffix .gz.

For example, I have 4 text files in my current working directory as shown below.

Text Files present in a directory

To compress file1.txt using gzip, the syntax will be.

$ gzip file1.txt

To view the compressed file, use the ls command.

Output

Linux gzip File

It’s worth noting that the original text file has now been compressed and bears the .gz compression suffix.

Force gzip not to replace/delete the original file

As we have seen in the previous example, gzip replaces the original file and turns it into a compressed file.
However, you can opt to retain the original file using the -k option as shown.

gzip -k [file-name]

For example, to compress file1.txt to a .gz file whie retaining the same file execute the command.

$ gzip -k file1.txt

Be sure to verify using the ls command and note that the original file1.txt file is still present.

Gzip K Option

Recursively compress files using gzip

To compress all files recursively inside a directory, use the -r option as shown.

$ gzip -r *

For example, there are 4 text file inside the test folder as shown.

Text Files inside a folder

To compress all the files at a go run:

$ gzip -r *

Output

Gzip Fies Recursively in a directory

Uncompress a file using gzip

If you want to uncompress a file, use the -d option a shown in the syntax below.

$ gzip -d [compressed file-name]

For instance, to uncompress file1.txt.gz run:

$ gzip -d file1.txt.gz

Output

Uncompress A File using gzip command

As you can see, the original compressed file disappears.

List details of a compressed file using gzip

To list details of a compressed file make use of the -l option as shown.

$ gzip -l [compressed-file-name]

For example,

$ gzip -l file1.txt.gz

Output

List Compressed File Details

How to Regulate Speed of gzip Compression

Gzip command also allows you to regulate the speed of compression. You can achieve this by specifying a hyphen (-) followed by a digit or number between 1 (Fastest) to 9 (Best). The default compression speed is 6.

gzip -5 [file-name]

For instance:

gzip -5 file1.txt

You can use any digit/number between 1 and 9, where 1 denotes the fastest compression rate and 9 denotes the slowest compression rate but the most preferred.

And that wraps up the gzip command usage. Feel free to share your thoughts on this. Your feedback is most welcome.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages