Skip to main content

gzip Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for compressing, listing, testing, and decompressing files with gzip and gunzip in Linux

The `gzip` command compresses files using the DEFLATE algorithm, while `gunzip` restores them. This cheatsheet covers common compression and decompression commands, useful flags, streaming patterns, and quick troubleshooting checks.

Basic Syntax

Core command forms for gzip and gunzip.

CommandDescription
gzip FILECompress a file and replace it with FILE.gz
gzip -k FILECompress and keep original file
gzip -d FILE.gzDecompress a .gz file
gunzip FILE.gzDecompress a .gz file
zcat FILE.gzPrint decompressed content to stdout

Compression Levels

Control speed versus compression ratio.

CommandDescription
gzip -1 FILEFastest compression, larger output
gzip -6 FILEDefault compression level
gzip -9 FILEMaximum compression, slower
gzip -k -9 FILEMax compression while keeping original

Compress Multiple Files

Apply gzip to groups of files.

CommandDescription
gzip *.logCompress all matching log files
gzip -r logs/Recursively compress files in a directory
find . -name '*.txt' -print0 | xargs -0 gzipCompress matching files safely
for f in *.csv; do gzip -k "$f"; doneCompress files and keep originals
gzip -- *.txtCompress files, safe for dash-prefixed names

Decompress and Inspect

Restore and verify compressed files.

CommandDescription
gunzip archive.gzDecompress and remove .gz file
gzip -dk archive.gzDecompress and keep .gz file
gzip -l archive.gzShow compressed/uncompressed sizes
gzip -v FILEShow compression ratio and details
gzip -t archive.gzTest integrity without extracting
zcat archive.gz | lessView content without writing files

Streams and Pipelines

Use gzip without intermediate files.

CommandDescription
mysqldump mydb | gzip > mydb.sql.gzCompress command output directly
gzip -c file.txt > file.txt.gzWrite compressed output to stdout
gunzip -c backup.sql.gz > backup.sqlDecompress to a chosen file
tar -cf - project/ | gzip > project.tar.gzCreate compressed tar stream
gzip -dc access.log.gz | grep ERRORSearch content in compressed logs

Troubleshooting

Quick checks for common gzip issues.

IssueCheck
gzip: command not foundInstall gzip package and verify with gzip --version
Original file disappeared after compressionUse -k to keep source files
not in gzip formatConfirm file type with file filename before decompression
Corrupt archive errorsRun gzip -t file.gz to validate integrity
Unexpected overwrite behaviorUse -k or output redirection with -c to control file writes

Use these guides for full workflows and archive handling.

GuideDescription
gzip Command in LinuxFull gzip tutorial with examples
gunzip Command in LinuxDecompress .gz files in detail
How to Create Tar Gz FileBuild compressed tar archives
Tar Command in LinuxCreate and extract tar archives
Find Large Files in LinuxLocate files before compression cleanup