Gzip And Gunzip – Linux File Compression And Decompression Tool

Gzip is one of the compression method in Linux.

It compresses the normal file and stores into single .gz extension file, this compression is much better compared with zip compression.

Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links.

By default, gzip keeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the -N option.

Compressed files can be restored to their original form using gzip -d or gunzip or zcat. If the original name saved in the compressed file is not suitable for its file system, a new name is constructed from the original one to make it legal.

Compression is always performed, even if the compressed file is slightly larger than the original.

How To Install gzip In Linux?

We can easily install it with help of package manager since the package is available in all the Linux distributions repository.

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install gzip.

$ sudo apt install gzip

For Fedora system, use DNF Command to install gzip.

$ sudo dnf install gzip

For Arch Linux based systems, use Pacman Command to install gzip.

$ sudo pacman -S gzip

For RHEL/CentOS systems, use YUM Command to install gzip.

$ sudo yum install gzip

For openSUSE Leap system, use Zypper Command to install gzip.

$ sudo zypper install gzip

Syntax:

Common syntax for gzip and gunzip.

[gzip (Compress) syntax]
# gzip [Files To Be Compressed]

[gzip (Decompress) syntax]
# gunzip [FileName.gz]

[gzip (Decompress) syntax]
# gzip -d [FileName.gz]

1) How to compress the file using gzip command

The below example compress the “test-gzip.txt” file to test-gzip.txt.gz file. See the output.

# File compression #
# gzip test-gzip.txt

# Checking compressed file #
# ls -lh /home/mageshm/test-gzip.txt.gz
-rw-r--r--.  1 root    root      92 Dec 27 13:31 test-gzip.txt.gz

2) How to view gzip/compressed file content without extracting

The below command is show the gzip/compressed file contents without extracting. See the output.

# gzip -l test-gzip.txt.gz
         compressed        uncompressed  ratio uncompressed_name
                 92                  60  13.3% test-gzip.txt

3) Unzip/Decompress the gzip file

The below command is Unzip/Decompress the gzip file.

# gunzip test-gzip.txt.gz
or
# gzip -d test-gzip.txt.gz

4) Compress the file/folder recursively

The below example compress the “link” folder recursively using gzip command.

# gzip -r links

Here i’m going to check the mentioned folder was compressed or not. It seems that the link folder is shown as a folder. I’m confused to move it inside the links directory as the gzip command compresses only the files recursively and stores their original location.

# ll -h
total 56K
drwx------.  6 mageshm mageshm 4.0K Dec 27 14:04 ./
drwx--x--x. 16 root    root    4.0K Dec 24 01:16 ../
-rw-------.  1 mageshm mageshm 1.2K Dec 26 14:09 .bash_history
-rw-r--r--.  1 mageshm mageshm   18 May 10  2012 .bash_logout
-rw-r--r--.  1 mageshm mageshm  176 May 10  2012 .bash_profile
-rw-r--r--.  1 mageshm mageshm  124 May 10  2012 .bashrc
-rw-r--r--.  1 root    root      26 Dec 27 12:25 liks
drwxr-xr-x.  3 mageshm mageshm 4.0K Dec 27 14:13 links/
drwxrwxr-x.  2 mageshm mageshm 4.0K Nov 16 10:14 perl5/
drwxr-xr-x.  2 mageshm mageshm 4.0K Nov 16 10:07 public_ftp/
drwxr-xr-x.  3 mageshm mageshm 4.0K Nov 16 10:07 public_html/
lrwxrwxrwx.  1 root    root      31 Dec  7 09:41 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx.  1 root    root      38 Dec  7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder
-rw-r--r--.  1 root    root      61 Dec 27 13:33 test1-gzip.txt
-rw-r--r--.  1 root    root      61 Dec 27 13:35 test2-gzip.txt
-rw-r--r--.  1 root    root      60 Dec 27 13:31 test-gzip.txt

Links folder contain 2 folders and 3 files recursively, the gzip command compress all the files recursively and stored their location. See the output.

# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./
drwx------. 6 mageshm mageshm 4.0K Dec 27 14:04 ../
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 soft-link/
-rw-r--r--. 1 root    root      92 Dec 27 14:10 test-gzip.txt.gz

[email protected] [/home/mageshm/links/soft-link]# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../
-rw-r--r--. 1 root    root      94 Dec 27 14:10 test2-gzip.txt.gz
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 test-soft-link-folder/

[email protected] [/home/mageshm/links/soft-link/test-soft-link-folder]# ll -h
total 12K
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../
-rw-r--r--. 1 root    root      94 Dec 27 14:10 test1-gzip.txt.gz

5) How to view the gzip/compressed file/folder recursively

Again i’m going to check the gzip/compressed file contents without extracting.

# less links
total 16
drwxr-xr-x. 3 mageshm mageshm 4096 Dec 27 14:14 ./
drwx------. 6 mageshm mageshm 4096 Dec 27 14:04 ../
drwxr-xr-x. 3 mageshm mageshm 4096 Dec 27 14:14 soft-link/
-rw-r--r--. 1 root    root      92 Dec 27 14:10 test-gzip.txt.gz
links (END)

The output clearly shows the file and folders recursively compressed.

6) How to unzip/compressed files/folders recursively

The below example is unzip the files/folders recursively.

# gunzip -r links

Once again i’m going to check whether the files/ folder unzip/de-compressed recursively.

# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./
drwx------. 6 mageshm mageshm 4.0K Dec 27 14:04 ../
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 soft-link/
-rw-r--r--. 1 root    root      92 Dec 27 14:10 test-gzip.txt

[email protected] [/home/mageshm/links/soft-link]# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../
-rw-r--r--. 1 root    root      94 Dec 27 14:10 test2-gzip.txt
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 test-soft-link-folder/

[email protected] [/home/mageshm/links/soft-link/test-soft-link-folder]# ll -h
total 12K
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../
-rw-r--r--. 1 root    root      94 Dec 27 14:10 test1-gzip.txt

Hope this article was useful for you. Kindly provide your valuable feedback/comments in the commenting section.

Stay tuned with us !!

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...