Gzip Command in Linux
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.
gzip [Options] [filenames]
$ gzip mydoc.txt
This command will create a compressed file of mydoc.txt named as mydoc.txt.gz and delete the original file.
- -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.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.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.gz
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 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.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.txt
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.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 firstname.lastname@example.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_up