LinuxCommandLibrary

gzip

Compress files to reduce their size

TLDR

Compress a file, replacing it with a gzip archive

$ gzip [path/to/file]
copy

Decompress a file, replacing it with the original uncompressed version
$ gzip [[-d|--decompress]] [path/to/file.gz]
copy

Display the name and reduction percentage for each file compressed
$ gzip [[-v|--verbose]] [path/to/file.gz]
copy

Compress a file, keeping the original file
$ gzip [[-k|--keep]] [path/to/file]
copy

Compress a file, specifying the output filename
$ gzip [[-c|--stdout]] [path/to/file] > [path/to/compressed_file.gz]
copy

Decompress a gzip archive specifying the output filename
$ gzip [[-cd|--stdout --decompress]] [path/to/file.gz] > [path/to/uncompressed_file]
copy

Specify the compression level. 1 is the fastest (low compression), 9 is the slowest (high compression), 6 is the default
$ gzip -[1..9] [[-c|--stdout]] [path/to/file] > [path/to/compressed_file.gz]
copy

List the contents of a compressed file
$ gzip [[-l|--list]] [path/to/file.txt.gz]
copy

SYNOPSIS

gzip [options] [file ...]
gzip [-d | -l | -t] [options] [file ...]

PARAMETERS

-1
    Fastest compression method

-2
    Faster compression

-3
    Moderate compression speed

-4
    Slightly better compression

-5
    Moderate compression (default is 6)

-6
    Default compression level

-7
    Good compression

-8
    Higher compression

-9
    Maximum (slowest) compression

-c
    Output to stdout, keep originals

-d
    Decompress

-f
    Force overwrite/compression

-h
    Display help

-k
    Keep original files

-l
    List compressed file info

-L
    Display license

-n
    No name/crc in header

-N
    Use original name/mod time

-q
    Quiet mode

-r
    Recursive on directories

-S suffix
    Use custom suffix (default .gz)

-t
    Test integrity

-v
    Verbose output

-V
    Version info

DESCRIPTION

The gzip command is a standard Unix utility for compressing and decompressing files using the gzip algorithm, which employs the DEFLATE method combining LZ77 and Huffman coding for efficient lossless compression. It replaces the input file with a compressed version ending in .gz, reducing file sizes significantly for text and certain binary data.

Common uses include archiving logs, backups, and web content delivery. When compressing multiple files or directories, pair it with tar (as tar -czf). Decompression is handled via gunzip or gzip -d. It supports levels from 1 (fastest, least compression) to 9 (slowest, best), defaulting to 6.

gzip preserves file permissions and timestamps, making it ideal for backups. It's part of the GNU project, widely available on Linux/Unix systems, and outperforms older compress while being backward-compatible with formats like PKZIP.

CAVEATS

Does not create multi-file archives (use tar); symlinks compressed as files unless -r; high levels (9) CPU-intensive; no encryption.

COMMON USAGE

gzip file.txt → file.txt.gz
gzip -c file.txt > file.txt.gz (stdout)
tar -czf archive.tar.gz dir/ (with tar)

PERFORMANCE TIP

Use -1/-9 for speed vs size; test with gzip -t; decompress faster than compress.

HISTORY

Developed 1992-1993 by Jean-loup Gailly as GNU replacement for Unix compress; uses zlib library; version 1.0 released 1993; evolved with gzip 1.2.4 (2009) adding long options, now standard in POSIX.

SEE ALSO

gunzip(1), zcat(1), zless(1), tar(1), bzip2(1), xz(1)

Copied to clipboard