BEST LINUX GZIP COMMAND (GZIP COMPRESSION) WITH EXAMPLES
Introduction
In this article we are going to learn the 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.
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 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 Linux gzip command refer the below command. Here I am compressing three files i.e. test1.txt, test2.txt, test3.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 gzip command the original file get compressed but if you want to compress a file by keep the original file unchanged then you can do so by using gzip command with argument -c. Refer 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 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 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 Linux 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 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 :
- COMPLETE UNIX COMMANDS AND BASIC LINUX COMMANDS WITH EXAMPLES FOR BEGINNERS
- BEST LINUX DU COMMAND WITH EXAMPLES
Extract a gzip compressed Directory
To Extract a Tar with Linux gzip compressed directory refer 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 Linux 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 gzip command
For 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 elinuxbook.com
Validate a gzip (.gz) Compressed File
You can check if the gzip file is valid or not by using Linux 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 touch command. and then validate the file. You will get the below error as it’s not created using Linux gzip command hence the file doesn’t have 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 Sub directory Recursively
We can compress all the contents of the directory with contents of all it’s sub directories recursively by using gzip command with argument -r. For Example I have a directory named data with some content and a sub directory names files with some content. Refer 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
So compress the content of the directory recursively using 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 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 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 the manual page of gzip command using below command.
[root@localhost data]# man gzip # gzip command manual page
To check Software license details of the Linux gzip compression you can use Linux gzip command with argument -L.
[root@localhost data]# gzip -L # To check License details of Linux 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 version of installed gzip package use gzip command with argument -V. Refer 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.
We tried to include all Linux gzip command (gzip compression) with it’s examples. If something missed out then please mention in the comment box below.
If you found this article useful then Like Us, Share Us, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.