Gzip for Beginners : How to use Gzip to Compress Files in Linux/Unix Based Systems

Gzip (GNU Zip) is a command line tool used to compress and/or expand files. It comes with most of the Linux/Unix based Operating Systems. Gzip is a free software replacement for the compress program used in early Unix systems, intended for use by the GNU Project. gzip was created by Jean-Loup Gailly and Mark Adler. The compression method used by "gzip" is known as Lempel-Ziv (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. If, in case if the compressed file name is too long for its file system, gzip truncates it.

Now lets see some basics usage of Gzip, and how we can use to compress/decompress files. But firstly we need to check that the gzip utility is installed or not in your system.
 $ which gzip
If gzip is installed on your system then it will return the PATH of gzip /bin/gzip. or if it not installed then nothing will return. now to install gizp use the below command :
 $ sudo apt-get update
 $ sudo apt-get install gzip
 $ sudo apt-get install gunzip
1. Compress A file

We can compress a file with Gzip by below command :
 $ gzip [file-name]
example :
 $ gzip document.txt
Now after compression the file document.txt is now replaced by compressed version of it, which is document.txt.gz.
 $ ls
The gzip will replace the original file with compressed one, by default. Now in order to keep the original file, use the -k option :
 $ gzip -k document.txt
2. Decompress A file

We can decompress a file with -d option :
 $ gzip -d [compressed-file-name]
example :
 $ gzip -d document.txt.gz
To decompress a file without deleting compress file
 $ gzip -d -k [compressed-file-name]
To decompress files we can also gunzip tool, which is specifically used to decompress files.
 $ gunzip -d [compressed-file-name]
similarly for keeping the compressed file
 $ gunzip -d -k [compressed-file-name]
3. Compressing multiple files

To compress multiple files with gzip, just provide the file names :
 $ gzip file1 file2 file3
4. To see the details of a compressed file :

We can see the details of compressed file with -l option
 $ gzip -l [compressed-file-name]
The information returned by the above command is compressed size, uncompressed size, ratio as a percentage, uncompressed filename.

5. To forcefully compress/decompress files 

To force fully perform compression or decompression operation in gzip, use -f option 
 $ gzip -f [file-name]
6. Recursive compressing files with gzip :

With -r option we can recursively compress files in gzip. in recursive compression we can compress all files available with-in the main directory as well as sub-directories.
 $ gzip -r /dir_name/
 $ gzip -r *
7. To Test The Validity Of A Compressed File :

By using -t option, we can check that the a file is valid or not :
 $ gzip -t [compressed-file-name]
If the file is valid there will be no output.

8. To Change The Compression Level

In Gzip we can also regulate the compression level and speed of compression. In gzip we can compress a file in different ways, for instance smaller compression which will work faster or we  can go for maximum compression which takes longer time to complete. 

To get minimum compression at the fastest speed run the following command :
 $ gzip -1 [file-name]
To get maximum compression at the slowest speed run the following command :
 $ gzip -9 [file-name]
note that we can use any number between 1 and 9, where -1 indicates fastest compression while -9 indicates slowest but best.

And for manual page use below command :
 $ man gzip

Conclusion :

The gzip is very powerful tool to compress and/or decompress files. In this post we saw some basic usage of gzip command with examples. For more information about gzip you can check mannual page at here : https://www.gnu.org/software/gzip/manual/gzip.html