gzip / bzip2 / zip / unzip
gzip, bzip2, and zip are commands for compressing and extracting files. Each uses a different compression algorithm, so you choose based on your use case and environment.
Syntax
Compress with gzip (the original file is removed and replaced by a .gz file).
gzip filename
Extract (decompress) with gzip.
gunzip filename.gz
Use the -k option to compress while keeping the original file.
gzip -k filename
Create a zip archive.
zip archive.zip file-or-directory...
Extract (decompress) a zip archive.
unzip archive.zip
Compress with bzip2.
bzip2 filename
Extract (decompress) with bzip2.
bunzip2 filename.bz2
Command / Option Reference
| Command / Option | Description |
|---|---|
| gzip file | Compresses a file with gzip (replaces the original file). |
| gzip -d / gunzip | Extracts a gzip file. |
| gzip -k | Compresses a file while keeping the original. |
| gzip -1 〜 -9 | Sets the compression level (1: fast, low compression; 9: slow, high compression). |
| gzip -l | Displays information about a compressed file (compression ratio, etc.). |
| zcat file.gz | Displays the contents of a gzip file without extracting it. |
| bzip2 file | Compresses a file with bzip2 (higher compression ratio than gzip). |
| bunzip2 / bzip2 -d | Extracts a bzip2 file. |
| bzcat file.bz2 | Displays the contents of a bzip2 file without extracting it. |
| zip -r archive.zip directory | Recursively compresses a directory into a zip archive. |
| zip -e archive.zip file | Creates a password-protected zip archive. |
| unzip archive.zip | Extracts a zip file. |
| unzip -l archive.zip | Lists the contents of a zip archive without extracting it. |
| unzip -d directory | Extracts a zip archive into the specified directory. |
Sample Code
Compress a log file with gzip. The original file is removed and replaced by a .gz file.
gzip /var/log/app.log ls /var/log/app.log* /var/log/app.log.gz
Compress a file while keeping the original.
gzip -k report.txt ls report.txt* report.txt report.txt.gz
Check information about a compressed file (compression ratio, etc.).
gzip -l app.log.gz
compressed uncompressed ratio uncompressed_name
45231 234567 80.7% app.log
View the contents without extracting. Using zcat, you can pipe the output to other commands.
zcat /var/log/app.log.gz | grep "ERROR" 2026-03-06 08:15:32 [ERROR] Connection timeout 2026-03-06 09:42:11 [ERROR] File not found
Bundle multiple files into a zip archive.
zip archive.zip file1.txt file2.txt file3.txt adding: file1.txt (deflated 62%) adding: file2.txt (deflated 58%) adding: file3.txt (deflated 71%)
Recursively compress a directory into a zip archive.
zip -r project.zip ./myproject/
List the contents of a zip archive without extracting it.
unzip -l project.zip
Archive: project.zip
Length Date Time Name
--------- ---------- ----- ----
1024 2026-03-06 12:00 myproject/index.php
2048 2026-03-06 12:00 myproject/css/style.css
--------- -------
3072 2 files
Extract a zip archive into a specific directory.
unzip project.zip -d /tmp/extracted/
Count the number of lines in a gzip-compressed file.
zcat access.log.gz | wc -l 48253
Notes
Compression ratio from highest to lowest: xz > bzip2 > gzip; speed is in the reverse order. For everyday tasks like log compression, $ gzip is common. When you need compatibility with Windows, use $ zip.
$ zcat (gzip) and $ bzcat (bzip2) let you read and pipe compressed file contents without extracting them. To archive entire directories, it is standard practice to combine these commands with tar.
If you find any errors or copyright issues, please contact us.