Gzip Command in Linux
The gzip command in Linux is used to compress files efficiently, reducing their size to save storage space and speed up file transfer without losing data.
- Uses the Lempel-Ziv (LZ77) compression algorithm for speed and efficiency.
- Replaces the original file with a compressed version ending in
.gz. - Can decompress files using the
-doption or thegunzipcommand.
Example 1: How to compress a file using the gzip command in Linux
gzip mydoc.txt- Creates mydoc.txt.gz in the same directory and removes mydoc.txt by default; use -k to keep the original.
Example 2: How to Decompress a gzip File in Linux
gzip -d mydoc.txt.gzThis command decompresses the specified gzip file, leaving the original uncompressed file intact.
Output:

Here,
- touch mydoc.txt: Creates an empty file.
- gzip mydoc.txt: Compresses it into mydoc.txt.gz and deletes the original.
- gzip -d mydoc.txt.gz: Decompresses it back to mydoc.txt and deletes the .gz file.
Syntax of the gzip Command
The basic syntax of the gzip command is straightforward:
gzip [Options] [filenames]- This syntax allows users to compress a specified file. Now, let's delve into some practical examples to illustrate the usage of the gzip command.
Options Available in gzip Command
Here are some real-world examples that demonstrate the versatility of the gzip command for different tasks:
1. Keeping the Original File Using gzip Command in Linux
- By default, gzip removes the original file after compression. To retain the original file, use the
-koption:
gzip -k example.txtThis command compresses "example.txt" and keeps the original file intact.

Here,
- touch example.txt: Creates an empty file named example.txt.
- gzip -k example.txt: Compresses it into example.txt.gz while keeping the original file.
- ls: Shows both files: example.txt (original) and example.txt.gz (compressed).
2. Verbose Mode Using gzip Command in Linux
To obtain more details during compression or decompression, the -v option is employed:
gzip -v example1.txtVerbose mode provides information such as file sizes and progress during the compression or decompression process.

3. Force CompressionUsing gzip Command in Linux
In cases where the compressed file already exists, the -f option forcefully overwrites it:
gzip -f example2.txtThis command compresses "example.txt" and overwrites any existing "example.txt.gz" file

4. Compressing Multiple Files Using gzip Command in Linux
Gzip can compress multiple files simultaneously by providing their names as arguments:
gzip file1.txt file2.txt file3.txtThis command compresses "file1.txt," "file2.txt," and "file3.txt" individually.

List of Common gzip Commands and Use Cases
Options | Description |
|---|---|
-f | Forcefully compress a file even if a compressed version with the same name already exists. |
-k | Compress a file and keep the original file, resulting in both the compressed and original files. |
-L | Display the gzip license for the software. |
-r | Recursively compress all files in a folder and its subfolders. |
-v | Display the name and percentage reduction for each file compressed or decompressed. |
-d | Decompress a file that was compressed using the gzip command. |
gzip vs zip Commands: When to Use
The gzip and zip commands are both used for file compression in Linux, but they differ in functionality, compression method, and usage scenarios.
| Feature | Gzip | Zip |
|---|---|---|
Compression Algorithm | Uses the DEFLATE algorithm. | Uses various compression algorithms, including DEFLATE, LZ77, and others. |
File Format | Typically appends ".gz" to compressed files. | Uses ".zip" extension for compressed archives. |
Archiving Approach | Common practice is to use tarball (.tar) before compression. | Compresses individual files and then adds them to the archive. |
File Extraction | Requires decompression of the entire file before extracting specific files. | Allows direct extraction of individual files without full decompression. |
Compression Efficiency | Generally offers better compression, especially for a large number of files. | Compression efficiency may vary, and it might be less effective than Gzip for certain scenarios. |
Extraction Time | Takes longer to extract a specific file from a compressed archive. | Allows quicker extraction of individual files from the archive. |
Ideal Use Case | Well-suited for compressing a large number of files into a single archive. | Suitable for compressing and archiving individual files with a focus on easy extraction. |
Redundancy Utilization | Efficiently utilizes redundancy in files to reduce overall file size. | May result in larger archive sizes, especially when compressing identical files multiple times. |