gzip Command
Beginner Compression & Archives man(1)Compress files using Lempel-Ziv coding
👁 9 views
📅 Updated: Mar 15, 2026
SYNTAX
gzip [OPTION]... [FILE]...
What Does gzip Do?
gzip compresses files using the Lempel-Ziv coding algorithm. It is the most common compression tool in Linux, typically producing .gz files. gzip replaces the original file with the compressed version by default.
gzip offers a good balance of compression speed and ratio. It is the standard compression for tar.gz archives, HTTP content encoding, and general file compression.
gzip only compresses single files. To compress multiple files or directories, first create a tar archive, then compress it (tar czf).
gzip offers a good balance of compression speed and ratio. It is the standard compression for tar.gz archives, HTTP content encoding, and general file compression.
gzip only compresses single files. To compress multiple files or directories, first create a tar archive, then compress it (tar czf).
Options & Flags
| Option | Description | Example |
|---|---|---|
| -d | Decompress | gzip -d file.gz |
| -k | Keep original file | gzip -k file.txt |
| -r | Recursively compress files in directory | gzip -r /var/log/ |
| -1 to -9 | Compression level (1=fast, 9=best) | gzip -9 large_file.txt |
| -l | List compressed file info | gzip -l file.gz |
| -v | Verbose — show compression ratio | gzip -v file.txt |
| -c | Write to stdout (keep original) | gzip -c file.txt > file.txt.gz |
Practical Examples
#1 Compress a file
Compresses the file, replacing it with access.log.gz.
$ gzip access.log#2 Decompress
Decompresses back to access.log. Or use gunzip.
$ gzip -d access.log.gz#3 Keep original
Compresses while keeping the original file.
$ gzip -k large_file.txt#4 Best compression
Uses maximum compression. Slower but smallest file.
$ gzip -9 database.sql#5 Check compressed file
Shows compressed/uncompressed sizes and ratio.
$ gzip -l backup.sql.gz
Output:
compressed uncompressed ratio name\n 12345 98765 87.5% backup.sql
#6 Compress to stdout
Pipes database dump directly through gzip compression.
$ mysqldump mydb | gzip > backup.sql.gzTips & Best Practices
Original file is replaced: gzip replaces the original file by default. Use -k to keep it, or gzip -c file > file.gz to write to stdout.
Use with pipes: gzip works great in pipes: command | gzip > output.gz for on-the-fly compression.
gzip vs xz vs bzip2: gzip is fastest. xz has best compression ratio. bzip2 is in between. For daily use and logs, gzip is recommended.
Frequently Asked Questions
How do I compress a file?
gzip filename. It replaces the file with filename.gz. Use -k to keep the original.
How do I decompress a .gz file?
gzip -d file.gz or gunzip file.gz. Both restore the original file.
How do I compress a directory?
gzip only compresses single files. For directories: tar czf archive.tar.gz directory/
Related Commands
More Compression & Archives Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →