Saturday 16 March 2019

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.

Gzip Command, Linux Tutorial and Material, Linux Certifications, Linux Guides

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.

Gzip Command, Linux Tutorial and Material, Linux Certifications, Linux Guides

Options:


1. -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

2. -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”.

3. -L option: This option displays the gzip license.

$ 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.

4. -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.

5. -[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

6. -v option: This option displays the name and percentage reduction for each file compressed or decompressed.

$ gzip -v mydoc.txt

OUTPUT:

new.txt:       18.2% -- replaced with new.txt.gz

7. -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.

Related Posts

0 comments:

Post a Comment