Compress and Decompress Files with gzip (with examples)
Introduction
gzip is a command-line tool used for compressing and decompressing files using the LZ77 compression algorithm. It is widely used for reducing file sizes and saving storage space. In this article, we will explore different use cases of the gzip command with code examples to compress and decompress files.
Use Case 1: Compress a File, Replacing it with a Gzipped Compressed Version
gzip file.ext
Motivation: Compressing files can be useful when transferring or storing them, as it reduces their size. This command compresses the specified file and replaces it with a gzipped compressed version.
Explanation: The command gzip file.ext
compresses the file named “file.ext” using gzip compression and replaces it with a gzipped compressed version. The original file is removed, and only the compressed file remains.
Example Output: Assuming “file.ext” has a size of 10 MB, after running the command, the file will be compressed, resulting in a smaller file size, such as 2 MB. The original “file.ext” will be replaced with “file.ext.gz”, which is the compressed version.
Use Case 2: Decompress a File, Replacing it with the Original Uncompressed Version
gzip -d file.ext.gz
Motivation: Sometimes, we need to decompress a file that has been compressed using gzip. This command allows us to decompress the file and replace it with the original uncompressed version.
Explanation: The command gzip -d file.ext.gz
decompresses the file named “file.ext.gz” and replaces it with the original uncompressed version. The resulting file will have the same name as the original file.
Example Output: Assuming “file.ext.gz” is a gzipped compressed file with a size of 2 MB, the command will decompress it and replace it with “file.ext”, which will have the original size of 10 MB.
Use Case 3: Compress a File, Keeping the Original File
gzip --keep file.ext
Motivation: In some cases, we may want to keep the original file after compressing it, instead of replacing it with the compressed version. This command allows us to compress the file while retaining the original copy.
Explanation: The command gzip --keep file.ext
compresses the file named “file.ext” using gzip compression and keeps the original file intact. It creates a new file named “file.ext.gz” that contains the compressed version.
Example Output: Assuming “file.ext” has a size of 10 MB, after running the command, the file will be compressed, resulting in a smaller file size, such as 2 MB. Additionally, the original “file.ext” will still exist alongside the compressed version as “file.ext.gz”.
Use Case 4: Compress a File, Specifying the Output Filename
gzip -c file.ext > compressed_file.ext.gz
Motivation: Sometimes, we may want to specify a custom output filename for the compressed file. This command allows us to compress a file and save it with a different name.
Explanation: The command gzip -c file.ext > compressed_file.ext.gz
compresses the file named “file.ext” using gzip compression and saves the compressed version with the specified output filename “compressed_file.ext.gz”.
Example Output: Assuming “file.ext” has a size of 10 MB, after running the command, the file will be compressed, resulting in a smaller file size, such as 2 MB. The compressed file will be saved as “compressed_file.ext.gz”.
Use Case 5: Decompress a Gzipped File, Specifying the Output Filename
gzip -c -d file.ext.gz > uncompressed_file.ext
Motivation: Similarly to the previous use case, we might want to specify a custom output filename when decompressing a file. This command allows us to decompress a gzipped file and save it with a different name.
Explanation: The command gzip -c -d file.ext.gz > uncompressed_file.ext
decompresses the gzipped file named “file.ext.gz” and saves the uncompressed version with the specified output filename “uncompressed_file.ext”.
Example Output: Assuming “file.ext.gz” is a gzipped compressed file with a size of 2 MB, the command will decompress it and save the uncompressed version as “uncompressed_file.ext”.
Use Case 6: Specify the Compression Level
gzip -9 -c file.ext > compressed_file.ext.gz
Motivation: Compression levels define the trade-off between compression ratio and compression speed. By specifying the compression level, we can control the balance between file size reduction and compression time.
Explanation: The command gzip -9 -c file.ext > compressed_file.ext.gz
compresses the file named “file.ext” using the highest compression level (-9) and saves the compressed version with the specified output filename “compressed_file.ext.gz”.
Example Output: Assuming “file.ext” has a size of 10 MB, after running the command with the highest compression level (-9), the file will be compressed to achieve the best possible compression ratio, resulting in a smaller file size, such as 1.5 MB. The compressed file will be saved as “compressed_file.ext.gz”.
Conclusion
In this article, we explored various use cases of the gzip command with code examples. We learned how to compress and decompress files, replace or keep the original files, specify output filenames, and control the compression level. Understanding these different use cases will enable you to effectively use gzip for file compression and decompression tasks.