Gzip Command in Linux

20 May, 2019
gzip command compresses files. Each single file is compressed into a single file. The compressed file consists of a GNU zip header and deflated data.If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes the original file. With no arguments, gzip compresses the standard input and writes the compressed file to standard output.Difference between Gzip and zip command in Unix and when to use which command
ZIP and GZIP are two very popular methods of compressing files, in order to save space, or to reduce the amount of time needed to transmit the files across the network, or internet.
In general, GZIP is much better compared to ZIP, in terms of compression, especially when compressing a huge number of files.
The common practice with GZIP, is to archive all the files into a single tarball before compression. In ZIP files, the individual files are compressed and then added to the archive.
When you want to pull a single file from a ZIP, it is simply extracted, then decompressed. With GZIP, the whole file needs to be decompressed before you can extract the file you want from the archive.
When pulling a 1MB file from a 10GB archive, it is quite clear that it would take a lot longer in GZIP, than in ZIP.
GZIP’s disadvantage in how it operates, is also responsible for GZIP’s advantage. Since the compression algorithm in GZIP compresses one large file instead of multiple smaller ones, it can take advantage of the redundancy in the files to reduce the file size even further.
If you archive and compress 10 identical files with ZIP and GZIP, the ZIP file would be over 10 times bigger than the resulting GZIP file.
Syntax :
 gzip [Options] [filenames]

Example:
$ gzip mydoc.txt

This command will create a compressed file of mydoc.txt named as mydoc.txt.gz and delete the original file.
Options :
-f option : Sometimes a file cannot be compressed. Perhaps you are trying to compress a file called “myfile1” but there is already a file called “myfile1.gz”. In this instance, the “gzip” command won’t ordinarily work.To force the “gzip” command to do its stuff simply use -f option: $ gzip -f myfile1.txtThis will forcefully compress a file named myfile.txt even if there already exists a file named as myfile.txt.gz-k option :By default when you compress a file using the “gzip” command you end up with a new file with the extension “.gz”.If you want to compress the file and keep the original file you have to run the gzip command with -k option: $ gzip -k mydoc.txtThe above command would end up with a file called “mydoc.txt.gz” and “mydoc.txt”.-L option : This option displays the gzip license.$ gzip -L filename.gzOUTPUT :Apple gzip 264.50.1 (based on FreeBSD gzip 20111009)
Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
All rights reserved.-r option : This option can compress every file in a folder and its subfolders.This option doesn’t create one file called foldername.gz. Instead, it traverses the directory structure and compresses each file in that folder structure.gzip -r testfolderThis will compress all the files present in the testfolder.-[1-9] option : It allows to change the compression level.A file can be compressed in different ways. For instance, you can go for a smaller compression which will work faster or you can go for maximum compression which has the tradeoff of taking longer to run.The speed and compression level can vary by levels using numbers between 1 and 9. $ gzip -1 mydoc.txtThis will get maximum compression at the slowest speed$ gzip -9 mydoc.txtTo get minimum compression at the fastest speed-v option: This option displays the name and percentage reduction for each file compressed or decompressed.$ gzip -v mydoc.txtOUTPUT :new.txt:       18.2% -- replaced with new.txt.gz-d option :This option allows to decompress a file using the “gzip” command.$ gzip -d mydoc.txt.gzThis command will unzip the compressed file named as mydoc.txt.gz.This article is contributed by Shubrodeep Banerjee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
-f option : Sometimes a file cannot be compressed. Perhaps you are trying to compress a file called “myfile1” but there is already a file called “myfile1.gz”. In this instance, the “gzip” command won’t ordinarily work.To force the “gzip” command to do its stuff simply use -f option: $ gzip -f myfile1.txtThis will forcefully compress a file named myfile.txt even if there already exists a file named as myfile.txt.gz
 $ gzip -f myfile1.txt
This will forcefully compress a file named myfile.txt even if there already exists a file named as myfile.txt.gz
-k option :By default when you compress a file using the “gzip” command you end up with a new file with the extension “.gz”.If you want to compress the file and keep the original file you have to run the gzip command with -k option: $ gzip -k mydoc.txtThe above command would end up with a file called “mydoc.txt.gz” and “mydoc.txt”.
 $ gzip -k mydoc.txt
The above command would end up with a file called “mydoc.txt.gz” and “mydoc.txt”.
-L option : This option displays the gzip license.$ gzip -L filename.gzOUTPUT :Apple gzip 264.50.1 (based on FreeBSD gzip 20111009)
Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
All rights reserved.
$ gzip -L filename.gz
OUTPUT :
Apple gzip 264.50.1 (based on FreeBSD gzip 20111009)
Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
All rights reserved.
-r option : This option can compress every file in a folder and its subfolders.This option doesn’t create one file called foldername.gz. Instead, it traverses the directory structure and compresses each file in that folder structure.gzip -r testfolderThis will compress all the files present in the testfolder.
gzip -r testfolder
This will compress all the files present in the testfolder.
-[1-9] option : It allows to change the compression level.A file can be compressed in different ways. For instance, you can go for a smaller compression which will work faster or you can go for maximum compression which has the tradeoff of taking longer to run.The speed and compression level can vary by levels using numbers between 1 and 9. $ gzip -1 mydoc.txtThis will get maximum compression at the slowest speed$ gzip -9 mydoc.txtTo get minimum compression at the fastest speed
 $ gzip -1 mydoc.txt
This will get maximum compression at the slowest speed
$ gzip -9 mydoc.txt
To get minimum compression at the fastest speed
-v option: This option displays the name and percentage reduction for each file compressed or decompressed.$ gzip -v mydoc.txtOUTPUT :new.txt:       18.2% -- replaced with new.txt.gz
$ gzip -v mydoc.txt
OUTPUT :
new.txt:       18.2% -- replaced with new.txt.gz
-d option :This option allows to decompress a file using the “gzip” command.$ gzip -d mydoc.txt.gzThis command will unzip the compressed file named as mydoc.txt.gz.
$ gzip -d mydoc.txt.gz
This command will unzip the compressed file named as mydoc.txt.gz.
This article is contributed by Shubrodeep Banerjee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
linux-command
Linux-file-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
curl command in Linux with Examples
Tail command in Linux with examples
Conditional Statements | Shell Script
TCP Server-Client implementation in C
Docker - COPY Instruction
scp command in Linux with Examples
UDP Server-Client implementation in C
echo command in Linux with Examples
Cat command in Linux with examples
touch command in Linux with Examples