LinuxCommandLibrary

compress

Compress files using Lempel-Ziv coding

TLDR

Compress specific files

$ compress [path/to/file1 path/to/file2 ...]
copy

Compress specific files, ignore non-existent ones
$ compress -f [path/to/file1 path/to/file2 ...]
copy

Specify the maximum compression bits (9-16 bits)
$ compress -b [bits]
copy

Write to stdout (no files are changed)
$ compress -c [path/to/file]
copy

Decompress files (functions like uncompress)
$ compress -d [path/to/file]
copy

Display compression percentage
$ compress -v [path/to/file]
copy

SYNOPSIS

compress [options] [file...]

PARAMETERS

-f
    Force compression. Overwrite existing `.Z` files without prompting. Also forces compression of symbolic links and allows compressing files that don't actually shrink.

-v
    Verbose mode. Displays the percentage of reduction for each file compressed.

-c
    Compress to standard output. The original file is not changed. Useful for piping.

-d
    Decompress. This is equivalent to calling `uncompress`.

-r
    Recursive. If any of the file names are directories, compress attempts to compress all files inside them (and their subdirectories).

-b bits
    Maximum bits. Specifies the maximum number of bits (from 9 to 16) to use for LZW codes. The default is 16.

-q
    Quiet mode. Suppress all diagnostic messages, warnings, and prompts.

DESCRIPTION

The compress command reduces the size of named files using adaptive Lempel-Ziv coding (LZW). When `compress` is successful, the original file is replaced by a compressed version with a `.Z` extension. For example, `file.txt` would become `file.txt.Z`. If no files are specified, compress reads from standard input and writes to standard output.

By default, compress removes the original file after successful compression, but this behavior can be altered using options like `-c` to write to standard output without deleting the original. The companion command `uncompress` (often a hard link to `compress -d`) restores files to their original state. Similarly, `zcat` (another hard link) displays the uncompressed content of a `.Z` file to standard output without decompressing it on disk. While historically important, compress has largely been superseded by more efficient compression tools like `gzip`, `bzip2`, and `xz` which offer better compression ratios.

CAVEATS

compress uses the LZW algorithm, which, while historically significant, generally achieves lower compression ratios compared to modern alternatives like `gzip`, `bzip2`, or `xz`. Its primary use today is often for compatibility with older systems or archives. The LZW algorithm was subject to patent issues (Unisys patent) which contributed to its decline in popularity, although these patents have since expired.

EXIT STATUS

compress exits with status 0 if all operations completed successfully, 1 if an error occurred, and 2 if a warning was issued but the operation completed.

<B><I>HARD LINKS</I></B>

The `uncompress` and `zcat` commands are typically hard links to the compress executable, providing convenient shortcuts for decompression and viewing compressed file contents, respectively.

HISTORY

The compress utility emerged in the early days of Unix, becoming a standard tool for file compression. Its core mechanism is the Lempel-Ziv-Welch (LZW) algorithm, developed in 1984. This algorithm was notable for its efficiency at the time, making compress widely adopted across various Unix-like operating systems. However, the commercial licensing of the LZW patent by Unisys in the 1990s led to widespread concerns and the development of royalty-free alternatives, most notably `gzip` (which uses the deflate algorithm). Despite the patent expiration, `gzip` and later `bzip2` and `xz` offered superior compression, leading to compress's diminished usage in new projects, though it remains important for interacting with legacy data.

SEE ALSO

uncompress(1), zcat(1), gzip(1), gunzip(1), bzip2(1), xz(1), tar(1)

Copied to clipboard