Best Linux Gzip Command (Gzip Compression) With Examples

In this article, e are going to learn how to use Linux gzip command (gzip compression) to compress files and directories. gzip command is a very useful tool in Linux like zip command and bzip2 command used to compress files and directories.

Best Linux Gzip Command (Gzip Compression) With Examples
Best Linux Gzip Command (Gzip Compression) With Examples

Best Linux Gzip Command (Gzip Compression) With Examples:

Compress a File using Linux gzip command:

We can compress a file using Linux gzip command. Here I am compressing a file test.txt. Refer to the command below.

[root@localhost data]# gzip test.txt    # Compress a File
[root@localhost data]# ls
test.txt.gz

You can use the argument -v with gzip command for Verbose output.

[root@localhost data]# gzip -v test.txt 
test.txt:         0.0% -- replaced with test.txt.gz

Compress Multiple files

To compress multiple files using the gzip command refer to the below command. Here I am compressing three files i.e. test1.txttest2.txttest3.txt

[root@localhost data]# gzip test1.txt test2.txt test3.txt   # Compress Multiple Files
[root@localhost data]# ls
test1.txt.gz  test2.txt.gz  test3.txt.gz

Compress a File with Keep the Original File unchanged

By using only the gzip command the original file gets compressed but if you want to compress a file by keeping the original file unchanged then you can do so by using the gzip command with argument -c. Refer to the below command.

[root@localhost data]# gzip -c /root/install.log > install.log.gz  # Compress a File by Keep original File unchanged
[root@localhost data]# ls
install.log.gz

Uncompress/Extract a gzip (.gz) File

To Uncompress OR Extract a file you can use the gunzip command.

[root@localhost data]# gunzip test.txt.gz   # Uncompressed OR Extract a gzip (.gz) File
[root@localhost data]# ls
test.txt

OR you can use the gzip command with argument -d to Uncompress a .gz file. Here I am Uncompressing test.txt.gz.

[root@localhost data]# gzip -d test.txt.gz   # Uncompress a gzip (.gz) File
[root@localhost data]# ls
test.txt

Compress a Directory using the gzip command

By using only Linux gzip command you cannot compress a directory as gzip only able to compress files but there is a way by using we can compress a directory using gzip command and i.e. with tar command.  Refer to the command below.

[root@localhost data]# tar -czvf files.tar.gz files/  # Compress a Directory By using tar command with gzip command
files/
files/test4.txt
files/test2.txt
files/test5.txt
files/test1.txt
files/test3.txt
[root@localhost data]# ls
files  files.tar.gz

Where :

  • c – To create a tar file
  • z – For gzip compression of the File
  • v – For verbose Output
  • f – for File

Also Read : How To Install 7Zip (7z) Archive Tool In Ubuntu

Extract a gzip compressed Directory

To Extract a Tar with Linux gzip-compressed directory refer to the below command.

[root@localhost data]# tar -xzvf files.tar.gz   # To Extract a Tar with gzip compressed File
files/
files/test4.txt
files/test2.txt
files/test5.txt
files/test1.txt
files/test3.txt
[root@localhost data]# ls
files  files.tar.gz

Where : 

  • x – To extract a Tar File

Perform the gzip Fast Compression using Linux gzip command

For fast compression of a file, you can use the gzip command with argument -1.

[root@localhost data]# gzip -1 test.txt   # Fast compression using gzip Command
[root@localhost data]# ls
test.txt.gz

Perform the Best Compression using the gzip command

For the best compression of a file, you can use gzip compression with argument -9.

[root@localhost data]# gzip -9 test.txt   # Best compression using gzip Command
[root@localhost data]# ls
test.txt.gz

Read the Content of a gzip-compressed File

To check the content of a gzip (.gz) compressed file without extracting you can use zcat command.

[root@localhost data]# zcat test.txt.gz   # Check the Content of a gzip compressed File without extracting it
Welcome to itsmarttricks.com

Validate a gzip (.gz) Compressed File

You can check if the gzip file is valid or not by using the gzip command with argument -t. If the file is a valid gzip file then you will not get any output.

[root@localhost data]# gzip -t install.gz   # Test Valid gzip file (Check Compressed File Integrity)

Let’s create a gzip (.gz) file using the touch command. and then validate the file. You will get the below error as it’s not created using the gzip command hence the file doesn’t have to gzip signatures.

[root@localhost data]# touch test.txt.gz   # Checking if the gzip file is Valid
[root@localhost data]# gzip -t test.txt.gz 

gzip: test.txt.gz: unexpected end of file

Compress the Content of a Directory  and Subdirectory Recursively

We can compress all the contents of the directory with contents of all it’s subdirectories recursively by using the gzip command with argument -r. For Example, I have a directory named data with some content and subdirectory names files with some content. Refer to the sample output below.

[root@localhost ~]# ls data/
files  test1.txt  test2.txt  test3.txt
[root@localhost ~]# ls data/files/
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

To compress the content of the directory recursively using the below command.

[root@localhost ~]# gzip -r data/   # Compress the Content of a Directory Recursivey
[root@localhost ~]# ls data/
files  test1.txt.gz  test2.txt.gz  test3.txt.gz
[root@localhost ~]# ls data/files/
file1.txt.gz  file2.txt.gz  file3.txt.gz  file4.txt.gz  file5.txt.gz

You can use zless and zmore command to check the long Linux gzip-compressed files. Refer to the commands below.

Example of zless Command :

[root@localhost ~]# zless install.log.gz 

Examples of zmore Command :

[root@localhost ~]# zmore install.log.gz

For more help on gzip command, you can refer to the below command.

[root@localhost data]# gzip --help   # For more Help on gzip command
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

OR refer to the manual page of the gzip command using the below command.

[root@localhost data]# man gzip   # gzip command manual page

To check the Software license details of the Linux gzip compression you can use the gzip command with argument -L.

[root@localhost data]# gzip -L   # To check License details of gzip Command
gzip 1.3.12
Copyright (C) 2007 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

You can check the version of installing the gzip package use the gzip command with argument -V. Refer to the command below.

[root@localhost data]# gzip -V   # To check Version of Installed gzip Package
gzip 1.3.12
Copyright (C) 2007 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

Also Read : TAR Command Examples in Linux

We tried to include all gzip command (gzip compression) with their examples. If something missed out then please mention in the comment box below.

That’s all, In this article, we have explained the Best Linux Gzip Command (Gzip Compression) With Examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

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