Basic Compression
gzip "path/to/file"After compression, the original file is removed and file.gz is created.
Compress While Keeping the Original File
gzip -k "path/to/file"Compress a Directory Recursively
gzip -r "path/to/dir"Each file inside the directory is compressed individually into .gz files.
Decompress
gzip -d "path/to/file.gz"# orgunzip "path/to/file.gz"gunzip is an alias for gzip -d.
Decompress to a Different File
gzip -cd "path/to/file.gz" > "path/to/file"-c outputs to stdout, and the redirect specifies the destination.
Specify Compression Level
gzip -1 "path/to/file" # Fastest, lowest compressiongzip -9 "path/to/file" # Best compression, slowest| Level | Alias | Characteristics |
|---|---|---|
-1 | --fast | Fast, lower compression ratio |
-6 | (default) | Balanced speed and compression ratio |
-9 | --best | Slow, higher compression ratio |
Show Information About a Compressed File
gzip -l "path/to/file.gz"Displays the original size, compressed size, compression ratio, and filename.
Integrity Check
gzip -t "path/to/file.gz"No output means the file is valid. If corrupted, an error such as gzip: path/to/file.gz: invalid compressed data--crc error will appear.
Archive and Compress a Directory with tar
# Compresstar cf - "path/to/dir" | gzip > archive.tar.gz# ortar czf archive.tar.gz "path/to/dir"
# Extracttar xzf archive.tar.gzUnlike the -r option, this bundles the entire directory structure into a single file.
Comparison with Other Tools
| Tool | Extension | Description |
|---|---|---|
gzip | .gz | Fast, widely supported, standard choice |
bzip2 | .bz2 | Higher compression than gzip, but slower |
xz | .xz | Best compression ratio, but heavy processing |
zstd | .zst | Fast and high compression. Suited for modern use |
hsb.horse