Cyberithub

5 Best Methods to Extract .gz File in Linux Using gunzip, gzip and tar tool

In this article, I will take you through 5 Best Methods to extract .gz file in Linux using gunzip, gzip and tar tool. If you need to send a very large file over a network or through some share network then it is always recommended to compress that file using .gz compression to reduce the size and then send it over. This greatly helps the sender in sending a large file.

At the receiver end same file needs to be extracted to the original file so that it will be used at the receiving end. To do the compression and extraction of the files, you can use various combinations of gzip, tar and gunzip tool. I have explained all the combinations in detail with the help of examples in below section which you can use as per tool availability and requirement.

5 Best Methods to Extract .gz File in Linux Using gunzip, gzip and tar tool 1

Extract .gz File in Linux using gzip, tar and gunzip command

Also Read: 17 Useful Linux chown command examples to change owner and group

Before going through the usage of gunzip command to extract .gz file you need to first understand the different methods through which .gz compression can be created. There are two popular tools which can be used to create .gz compression in Linux – gzip and tar tool. Similarly you can use gunzip, gzip and tar tool to extract .gz file.

Method 1:  Create .gz file using gzip command and extract .gz file using gunzip command

To demonstrate the usage of gzip tool to compress the file in .gz format and gunzip to extract .gz file we will use a small CentOS ISO Image of size 140M as shown below. Here size of CentOS.ISO image can be checked using du -sh CentOS.ISO command.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Now if you apply .gz compression on CentOS ISO Image using gzip -v CentOS.ISO command then you will see a file generated CentOS.ISO.gz replacing the source file CentOS.ISO.

[root@localhost ~]# gzip -v CentOS.ISO
CentOS.ISO: 4.4% -- replaced with CentOS.ISO.gz

-v : Verbose. Display the name and percentage reduction for each file compressed or decompressed.

As you can see above the generated CentOS.ISO.gz image will be 4.4% smaller than the source file CentOS.ISO. This you can verify by using du -sh CentOS.ISO.gz command as shown below.

[root@localhost ~]# du -sh CentOS.ISO.gz
134M CentOS.ISO.gz

If you want to extract the CentOS.ISO.gz file then you need to use below gunzip -v CentOS.ISO.gz command. This command will give you the Original Source File CentOS.ISO by replacing the previous compressed file as shown in the output.

[root@localhost ~]# gunzip -v CentOS.ISO.gz
CentOS.ISO.gz: 4.4% -- replaced with CentOS.ISO

-v : Verbose. Display the name and percentage reduction for each file compressed or decompressed.

Method 2: Create .gz file using tar command and extract .gz file using tar command

Like gzip you have another very useful tool available in most of the Linux based system is the tar tool. You can also compress any files to .gz extension using tar command in Linux. Here also we will use the same CentOS ISO image to demonstrate the extraction of .gz file. First you need to verify the size of the source ISO image file using du -sh CentOS.ISO command as shown below.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Then you can compress the ISO image using tar -czvf CentOS.ISO.gz CentOS.ISO command as shown below. This command won’t replace the Original file like in case of gzip tool instead it will create a separate output file CentOS.ISO.gz as you have specified in the command.

[root@localhost ~]# tar -czvf CentOS.ISO.gz CentOS.ISO
CentOS.ISO

-c : create a new archive.

-z : filter the archive through gzip.

-v : verbosely list files processed.

-f : use archive file or device ARCHIVE. More can be checked on tar command Man Page.

If you check the size of this .gz file using du -sh CentOS.ISO.gz command then you can see it is also compressed by 4.4% from the Source File.

[root@localhost ~]# du -sh CentOS.ISO.gz
134M CentOS.ISO.gz

Now if you want to extract .gz file then you can use same tar tool to extract the file by using tar -zxvf CentOS.ISO.gz command as shown below.

[root@localhost ~]# tar -zxvf CentOS.ISO.gz
CentOS.ISO

After extraction if you check the size of the Original File then it will show as 140M which is actual size when we compressed it in the beginning.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Method 3: Create .gz file using tar command and extract .gz file using gunzip command

Another method that can be used is that compress the source file to .gz using tar command and extract using gunzip command as described below. Like previous methods, you can check the size of Source File using du -sh CentOS.ISO command.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Then compress the source file in .gz format using tar -cvzf CentOS.ISO.gz CentOS.ISO command as shown below.

[root@localhost ~]# tar -cvzf CentOS.ISO.gz CentOS.ISO
CentOS.ISO

If you check the size of compressed file CentOS.ISO.gz image then it will be around 134M which will be 4.4% lesser than the Original File.

[root@localhost ~]# du -sh CentOS.ISO.gz
134M CentOS.ISO.gz

To extract the file in this method you can use gunzip -v CentOS.ISO.gz command as shown below. This command will extract the CentOS.ISO image and will replace the compressed ISO file.

[root@localhost ~]# gunzip -v CentOS.ISO.gz
CentOS.ISO.gz: 4.4% -- replaced with CentOS.ISO

If you again check the size of extracted ISO file then it will show around 140M which was the original size. Hence we extracted the file successfully.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Method 4: Create .gz file using gzip command and extract .gz file using tar command

You can also use a method where you can compress the source file to .gz format using gzip command and extract it using tar command as explained below. Like other methods here also we are first checking the size of CentOS.ISO image using du -sh CentOS.ISO command.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Then we are compressing the CentOS Image using gzip -v CentOS.ISO command as shown below.

[root@localhost ~]# gzip -v CentOS.ISO
CentOS.ISO: 4.4% -- replaced with CentOS.ISO.gz

As shown below, gzip compression created a CentOS.ISO.gz file of size around 134M.

[root@localhost ~]# du -sh CentOS.ISO.gz
134M CentOS.ISO.gz

If you now extract the same image using tar -zxvf CentOS.ISO.gz command then you will get the original file of CentOS.ISO.

[root@localhost ~]# tar -zxvf CentOS.ISO.gz
CentOS.ISO

-x : extract files from an archive.

If you verify the size using du -sh CentOS.ISO command then it will show the size of around 140M which is the original size of the file.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Method 5: Create .gz file using gzip command and extract .gz file using same gzip command

You can also use gzip tool to compress the file in .gz format and then extract .gz using -d option as described below. Like before, first we need to check the size of CentOS.ISO image using du -sh CentOS.ISO command.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

Then we need to apply .gz compression using gzip -v CentOS.ISO command as shown below.

[root@localhost ~]# gzip -v CentOS.ISO
CentOS.ISO: 4.4% -- replaced with CentOS.ISO.gz

If you again check the size of the compressed file using du -sh CentOS.ISO.gz then you can see it is compressed by 4.4% from its original size.

[root@localhost ~]# du -sh CentOS.ISO.gz
134M CentOS.ISO.gz

Now to decompress the file using gzip tool you need to run gzip -v -d CentOS.ISO.gz command as shown below.

[root@localhost ~]# gzip -v -d CentOS.ISO.gz
CentOS.ISO.gz: 4.4% -- replaced with CentOS.ISO

-d : Decompress.

Once you extracted the file you can check the size of the Original file using du -sh CentOS.ISO command as shown below.

[root@localhost ~]# du -sh CentOS.ISO
140M CentOS.ISO

 

 

 

 

 

Popular Recommendations:-

Solved: Errors in Crontab File, Can’t Install – Unix and Linux (RHEL/CentOS 7/8)

How to Transfer Files to AWS EC2 Instance Using WinSCP in 3 Easy Steps

12 Popular Unix/Linux uname command examples(How to Get Kernel Version)

Learn HTML Tables(v5) with Best Examples

Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7

C# data types with Best Examples (.NET v4.7)

How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps

Learn HTML Image Maps(v5) with Best Examples

Leave a Comment