This is our second post on compression and archiving series. Today we will see how to use gzip and gunzip commands with examples. Gzip(GNU zip) is a compress tool which is available in most of the Linux/Unix based operating systems. Until recent years gzip and bzip2 are most commonly used data compression tools in Linux/Unix. Though gzip compress ratios are not good when compared to bzip2 but it is popular among masses. Gzip software uses Lempel-Ziv coding (LZ77) algorithm for compressing data. In this post we will see how to install, use as well as tips about gzip command.

13 Zip and Unzip command examples in Linux/Unix

How to install gzip and gunzip(GNU unzip) command in Linux?

On Redhat, Centos and Fedora based machines

yum -y install gzip gunzip

On Ubuntu and Debian based machines

apt-get install gzip gunzip

Learn gzip command with examples

Example1: Syntax for zipping a file with gzip command.
Syntax:

gzip file1 file2 file3

Example:

surendra@linuxnix:~/test/test$ ls
2  3  4   dump.doc  file1  test.sh
surendra@linuxnix:~/test/test$ gzip dump.doc file1 test.sh 
surendra@linuxnix:~/test/test$ ls
2  3  4  dump.doc.gz  file1.gz  test.sh.gz

Note: Above gzip command will create files dump.doc.gz, file1.gz and test.sh.gz respectively by replacing original files.

To avoid deleting of original files, use -k(keep) option with zgip command

Example2: How to retain original files after individual file compressions? use keep(-k) option.

gzip  -k dump.doc file1 test.sh 

Output:

surendra@linuxnix:~/test/test$ gzip -k dump.doc file1 test.sh 
surendra@linuxnix:~/test/test$ ls
2  3  4  dump.doc  dump.doc.gz  file1  file1.gz  test.sh  test.sh.gz

Example3: How to zip group of files to a gzip single compressed file? Here we should know one behavior of gzip. Gzip don’t know how to add files to a single comprss file and it just compress each file individually by default. In order to compress a group of files to a single compress file use cat command as shown below

cat dump.doc file1 test.sh | gzip > all.gz 

Output:

surendra@linuxnix:~/test/test$ cat dump.doc file1 test.sh | gzip > all.gz
surendra@linuxnix:~/test/test$ ls
2  3  4  all.gz  dump.doc  file1  test.sh
surendra@linuxnix:~/test/test$ ls -lh all.gz 
-rw-rw-r-- 1 surendra surendra 346 May 23 07:12 all.gz

Note: We cannot unzip these files back individually. All the files are clubbed in to one single file called all.

Example4: How to compress a folder? As said earlier gzip is not meant to compress files in to single file. For compressing a folder use tar command first and then compress that tar file as shown below.

tar cf - test/ | gzip > test.tar.gz

or easiest way is

tar cvfz test.tar.gz test/

Note: tar do not require – when specifying options.

-c for create

-v for verbose, so that the output is shown when the command is running.

-f for mentioning the output file or target file where your compressed data is stored.

-z for specifying compress with GNUzip.

Output:

surendra@linuxnix:~/test$ tar cf - test/ | gzip > test.tar.gz
surendra@linuxnix:~/test$ ls
test  test.tar.gz
surendra@linuxnix:~/test$ ls -l
total 636
drwxrwxr-x 5 surendra surendra   4096 May 23 07:16 test
-rw-rw-r-- 1 surendra surendra 646612 May 23 07:20 test.tar.gz

Example5: To know compression ratio use -l option

gzip -l all.gz

Output:

surendra@linuxnix:~/test/test$ gzip -l all.gz 
         compressed        uncompressed  ratio uncompressed_name
                346                 731  55.1% all

Example6: Unzip the gzip file

gunzip all.gz 

Output:

surendra@linuxnix:~/test/test$ ls
2  3  4  all.gz  dump.doc  file1  test.sh
surendra@linuxnix:~/test/test$ gunzip all.gz 
surendra@linuxnix:~/test/test$ ls
2  3  4  all  dump.doc  file1  test.sh

Example7: How to unzip a tar.gz or tgz files in Linux?

Linux tar(gnu tar) have capability of unzipping tar.gz and .tgz files directly by using g options as shown below.

tar xvfz test.tar.gz

or

tar xvfz test.tgz

Extracted data/files are stored in present working directory.

In our next post we will see how to use bzip file compression.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.