compress

file compressionLinux/Unix
The compress command is one of the most frequently used commands in Linux/Unix-like operating systems. compress Compress files using the Lempel-Ziv coding

Quick Reference

Command Name:

compress

Category:

file compression

Platform:

Linux/Unix

Basic Usage:

compress [options] [arguments]

Common Use Cases

    Syntax

    compress [-cfvd] [-b bits] [file ...]

    Options

    Option Description
    -c Write to standard output; don't change input files
    -d Decompress input instead of compressing it
    -f Force compression even if it doesn't save space or the output file already exists
    -v Verbose mode; display compression ratio
    -b bits Set maximum code size (9 to 16, default is 16)

    Examples

    How to Use These Examples

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

    #

    Basic Examples:

    # Compress a single file (creates file.Z and removes original)
    compress file.txt
    # Compress a file and keep the original compress -c file.txt > file.txt.Z
    # Compress multiple files at once compress file1.txt file2.txt file3.txt
    # Decompress a file (alternative to uncompress) compress -d file.txt.Z

    Advanced Examples:

    # Compress with verbose output showing compression ratio
    compress -v largefile.txt
    # Force compression even if it doesn't save space compress -f smallfile.txt
    # Compress with higher compression level (16-bit) compress -b 16 largefile.txt # Compress and verify data integrity compress -v file.txt && uncompress -c file.txt.Z | cmp - file.txt # Compress all text files in a directory find . -name "*.txt" -type f -exec compress {} \; # Compress stdin and send to stdout
    cat file.txt | compress -c > file.txt.Z
    # Compress and create a tar archive tar cf - directory | compress -c > directory.tar.Z

    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 compress command reduces the size of files using the Lempel-Ziv coding (LZW). It was one of the first widely used Unix compression utilities but has largely been replaced by more efficient algorithms.

    Key points about compress:

    • Creates compressed files with a .Z extension
    • By default, removes the original file after successful compression
    • Typically achieves 50-60% compression for text files
    • Uses the LZW (Lempel-Ziv-Welch) algorithm, which was patented until 2003
    • Has largely been superseded by gzip, bzip2, and xz, which provide better compression ratios

    When to use compress:

    • When you need compatibility with very old systems
    • When working with legacy .Z files
    • In scripts that might run on systems without newer compression tools

    Comparison with other compression tools:

    • gzip: More efficient than compress, creates .gz files, uses the DEFLATE algorithm
    • bzip2: Better compression than gzip, slower but produces smaller files, creates .bz2 files
    • xz: Best compression among these tools, slowest but smallest files, creates .xz files

    Related commands:

    • uncompress: Utility to decompress .Z files (equivalent to compress -d)
    • zcat: View the contents of a .Z file without decompressing it
    • gzip: More modern compression utility that has largely replaced compress

    Most modern systems provide compress mainly for backward compatibility, and it's recommended to use newer compression tools like gzip, bzip2, or xz for better compression efficiency.

    Related Commands

    These commands are frequently used alongside compress 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 compress command works in different scenarios.

    $ compress
    View All Commands