Updated April 18, 2023
Introduction to Linux gzip
Linux gzip is a compression algorithm to reduce the file size and keep the original file size, timestamp and ownership. The Linux gzip is also called a gunzip. The gzip is a software application or utility to compress or decompress the file with existing file property. In the Linux operating system, we have different compression and decompression technics but the gzip having much faster compression speed is 43.95 in MB per second and decompression speed is 88.25 in MB per second.
gzip [ option ]... [ file | path of directory ]...
As per the above gzipsyntax, we need to provide the different options in gzip command and provide the file or directory to whom we need to compress.
How Linux gzip Command Works?
Basically gzip algorithm is based on “DEFLATE” algorithm. It is a combination of LZ77 and Huffman coding. While compression of any data or file, we need to consider different things like a 10-byte header, magic number (1f 8b), the DEFLATE compression method, 1-byte of header flags, 4-byte timestamp, compression flags and the operating system ID. If needed the extra headers are allowing by the header flags. In the body, the compressed payload containing a DEFLATE. In the footer, it is containing a CRC-32 checksum, 8-byte footer value and the length of the original uncompressed data. With the help of header, body and footer it will help to compress the data or file.
Essentially gzip is normally used to compress the single files. But if you want to compress the archival data or collection of data then we can compress in a single tar archival. It is also known as the tarball.
Examples to Implement of Linux gzip
Below are the examples of Linux gzip:
1. Compress File
Simply we can compress the single file with any option. After compression, the original file will remove and converted in compress format.
Explanation: In the sample directory, we are having the samle.txt file (refer screenshot 1 (a)). We are compressing the file by the default method of gzip. The gzip keeps the original file information of timestamp, ownership and name of the file.
Screenshot 1 (a)
Screenshot 1 (b)
2. Keep Original Fine
If you want to keep the original file while compressing then we can use the “-c” keyword with gzip command.
gzip -c file.txt > file.txt.gz
Explanation: In the sample directory, we are having the file.txt file (refer screenshot 2 (a)). We are compressing the file but keeping the original file as it is. As per the above command, we are using the “-c” keyword which tells gzip command to write on the standard output and redirect to the output file (refer screenshot 2 (b)).
Screenshot 2 (a)
Screenshot 2 (b)
3. Verbose Output
If you want to output the output with name and percentage process for each file of compression or decompression then we will use the “-v” option with gzip command.
gzip -v file1.txt
Explanation: In the sample directory, we are having the file1.txt file. Now we are compressing the file1.txt file using verbose option “-v”.
4. Compress Multiple Files
In gzip, we are having the functionality to compress the multiple files.
gzip f1 f2 f3 f4 f5
Explanation: In the sample directory, we are having the multiple files (f1, f2, f3, f4 and f5) (refer screenshot 4 (a)). We are compressing all the files(refer to screenshot 4 (b)).
Screenshot 4 (a)
Screenshot 4 (b)
5. Compress Directory Files
The gzip will recursively compress the whole directory structure and compress all the files in the directory and it is subdirectories as well.
To compress the directory, we need to use the “-r” option in gzip command.
gzip -r log
In the sample directory, we are having a log directory. In the log directory, we are having the multiple files and subdirectories in it (refer screenshot 5 (a)). Using the “-r” option in gzip command, we can compress all the files and subdirectories in it (refer screenshot 5 (b)).
Screenshot 5 (a)
Screenshot 5 (b)
6. Compression Level
In gzip, there are different compression rations. As per our requirement, we can change in compression ratio and speedup the compression speed. By default, the compression ratio is -6. In gzip, the compression ratio and speed of compression are inversely proportional to each other i.e. if the compression ratio is high then the compression speed is less or the compression ratio is less than the compression speed is high. Using “-1” is the high compression speed but low compression ratio. The option “-9” having the high compression ration but the compression speed is less.
gzip -9 sample.txt
Explanation: As per the compression speed and compression ratio, we can use the different options in the gzip command. In the above example, we have used “-9” option. It is having a high compression ratio but less compression speed.
Screenshot 6 (a)
Screenshot 6 (b)
7. Decompressing Files
In gzip, we can compress the multiple files as well as directories. In such a way, we can decompress the “.gz” file. We are having the functionality to decompress the data compress data via gzip.
To decompress the data, we can use the “-d” option.
gzip -d sample.txt.gz
Explanation: In the sample directory, we are having the compressed file “sample.txt.gz” (refer screenshot 6 (a)). With the help of the “-d” option, we can decompress the “sample.txt.gz” file into original file format “.txt”. After decompression of the file, we can able to do the normal file operation.
Screenshot 7 (a)
Screenshot 7 (b)
We have seen the uncut concept of “Linux gzip” with the proper example, explanation and command with different outputs. The gzip software is for compression and decompression of file and directories. It will helpful for proper clean-up of the operating system and keep unused data or files in compress format. If in future we can decompress it and use the same compress files.
We hope that this EDUCBA information on “Linux gzip” was beneficial to you. You can view EDUCBA’s recommended articles for more information.