gzip

file managementLinux/Unix
The gzip command is one of the most frequently used commands in Linux/Unix-like operating systems. gzip Compress or decompress files

Quick Reference

Command Name:

gzip

Category:

file management

Platform:

Linux/Unix

Basic Usage:

gzip [options] [arguments]

Common Use Cases

    Syntax

    gzip [options] [files...]

    Options

    Option Description
    -c, --stdout Write output on standard output; keep original files unchanged
    -d, --decompress Decompress files (same as gunzip)
    -f, --force Force compression or decompression even if file has multiple links or output file already exists
    -h, --help Display help information and exit
    -k, --keep Keep (don't delete) input files during compression or decompression
    -l, --list List compressed file contents
    -n, --no-name Do not save or restore the original name and timestamp
    -N, --name Save or restore the original name and timestamp
    -q, --quiet Suppress all warnings
    -r, --recursive Travel the directory structure recursively
    -t, --test Test compressed file integrity
    -v, --verbose Display the name and percentage reduction for each file
    -1, --fast Compress faster (less compression)
    -9, --best Compress better (best compression, slowest)

    Examples

    How to Use These Examples

    The examples below show common ways to use the gzip command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    gzip file.txt
    Compress file.txt, creating file.txt.gz and removing the original.
    gzip -k file.txt
    Compress file.txt to file.txt.gz, keeping the original file.
    gzip -9 file.txt
    Compress file.txt with maximum compression level. # Advanced Examples Advanced gzip -c file.txt > file.txt.gz Compress file.txt to file.txt.gz without removing the original. find . -name "*.log" -exec gzip {} \; Find all .log files in the current directory and compress them. tar cf - directory | gzip > directory.tar.gz Create a compressed tar archive of a directory.
    cat file1 file2 | gzip > combined.gz
    Combine multiple files and compress them to a single file.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The gzip command compresses files using Lempel-Ziv coding (LZ77). It's one of the most commonly used compression utilities in Unix-like operating systems, known for its good balance between compression ratio and speed. Key features of gzip: 1. File Compression: gzip primarily compresses single files. By default, it replaces the original file with a compressed version that has a .gz extension. 2. Compression Levels: gzip offers 9 compression levels (1-9), allowing users to choose between faster compression (level 1) or better compression ratios (level 9). 3. Multiple File Handling: While gzip is designed for single-file compression, it can process multiple files in sequence when provided with multiple arguments. 4. Standard Input/Output Support: gzip can read from standard input and write to standard output, making it versatile for use in pipelines. 5. Metadata Preservation: By default, gzip preserves the original file's name, timestamp, and permissions in the compressed file. 6. Decompression Capability: Although gunzip is typically used for decompression, gzip itself can decompress files with the -d option. 7. Integrity Testing: The -t option allows testing compressed files for integrity without actually decompressing them. gzip is not an archiving tool like tar; it doesn't combine multiple files into one. However, it's commonly used alongside tar to create compressed archives (e.g., tar -cz or tar -czf to create a .tar.gz file). The gzip format has become a standard on the internet, used for software distribution, data transfer, and reducing storage needs. The .gz format is supported by most operating systems and compression utilities, making it highly interoperable.

    Related Commands

    These commands are frequently used alongside gzip or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the gzip command works in different scenarios.

    $ gzip
    View All Commands