Best Linux gzip command (gzip compression) with examples

This article shows you how to use the Linux gzip (gzip compression) command to compress files and directories. The gzip command is a very useful tool for Linux Post Code Command and command bzip2 for compressing files and directories.

Best Linux gzip command (gzip compression) with examples

Best Linux Gzip command (Gzip compression) with examples:

Compress a file using the Linux gzip command:

We can compress a file using the Linux gzip command. Here I compress a file test.txt, See the following command.

[[email protected] data]# gzip test.txt    # Compress a File
[[email protected] data]# ls
test.txt.gz

You can use the argument -v With the gzip command for detailed output.

[[email protected] data]# gzip -v test.txt 
test.txt:         0.0% -- replaced with test.txt.gz

Compress multiple files

To compress multiple files with the gzip command, run the following command. Here I compress three files, i.e. test1.txt. test2.txt. test3.txt

[[email protected] data]# gzip test1.txt test2.txt test3.txt   # Compress Multiple Files
[[email protected] data]# ls
test1.txt.gz  test2.txt.gz  test3.txt.gz

Compress a file with Keep the original file as is

If you only use the gzip command, the original file is compressed. However, if you want to compress a file by leaving the original file unchanged, you can use the gzip command with an argument -c, See the following command.

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

Decompress / extract a gzip file (.gz)

To decompress OR extract a file that you can use gunzip Command.

[[email protected] data]# gunzip test.txt.gz   # Uncompressed OR Extract a gzip (.gz) File
[[email protected] data]# ls
test.txt

OR you can use the gzip command with an argument -d a. decompress .gz File. Here I decompress test.txt.gz,

[[email protected] data]# gzip -d test.txt.gz   # Uncompress a gzip (.gz) File
[[email protected] data]# ls
test.txt

Compress a directory with the gzip command

If you only use the Linux gzip command, you cannot compress a directory because gzip can only compress files. However, there is a way to use gzip and d to create a directory. H. Compress with Tar command, See the following command.

[[email protected] 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
[[email protected] data]# ls
files  files.tar.gz

Where :

  • c – How to create a tar file
  • z – For gzip compression of the file
  • v – For detailed output
  • f – for file

Read also: How to install the 7Zip (7z) Archive Tool in Ubuntu 18.04

Extract a directory compressed with gzip

Follow the command below to extract a Linux gzip compressed directory.

[[email protected] 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
[[email protected] data]# ls
files  files.tar.gz

Where :

  • x – How to extract a tar file

Perform fast gzip compression using the Linux gzip command

You can use the gzip command with an argument to quickly compress a file -1,

[[email protected] data]# gzip -1 test.txt   # Fast compression using gzip Command
[[email protected] data]# ls
test.txt.gz

Do the best compression with the gzip command

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

[[email protected] data]# gzip -9 test.txt   # Best compression using gzip Command
[[email protected] data]# ls
test.txt.gz

Read the contents of a gzip compressed file

To check the contents of a gzip (.gz), compress the file without extracting it zcat Command.

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

Check a compressed gzip file (.gz)

You can check whether the gzip file is valid or not by using the gzip command with an argument -t, If the file is a valid gzip file, you will get no output.

[[email protected] data]# gzip -t install.gz   # Test Valid gzip file (Check Compressed File Integrity)

Let’s create a gzip file (.gz) with the touch command. and then validate the file. The following error message appears because it was not created with the gzip command. The file does not have to zip signatures.

[[email protected] data]# touch test.txt.gz   # Checking if the gzip file is Valid
[[email protected] data]# gzip -t test.txt.gz 

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

Recursively compress the contents of a directory and a subdirectory

We can recursively compress the entire contents of the directory with the contents of all of its subdirectories using the gzip command with an argument -r, For example, I have a directory called Data with some table of contents and subdirectory names files with some content. See the following sample output.

[[email protected] ~]# ls data/
files  test1.txt  test2.txt  test3.txt
[[email protected] ~]# ls data/files/
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

To recursively compress the contents of the directory, use the following command.

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

You can use zlos and zmore Command to check the long gzip compressed Linux files. Note the following commands.

Example of the zless command:

[[email protected] ~]# zless install.log.gz 

Examples of the zmore command:

[[email protected] ~]# zmore install.log.gz

For more help with the gzip command, see the following command.

[[email protected] 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 .

OR go to the manual page of the gzip command with the following command.

[[email protected] data]# man gzip   # gzip command manual page

To check the Linux GZIP compression software license details, you can use the gzip command with an argument -L,

[[email protected] 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 .
There is NO WARRANTY, to the extent permitted by law.

You can check the version of the gzip package installation with the gzip command with argument -V, See the following command.

[[email protected] 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 .
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

Read also: Examples of TAR commands on Linux

We tried to include examples with all gzip commands. If something is missed, please let us know 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 like this article. If you like this article, just share it. If you have any questions about this article, please comment.

Sidebar