← All commands
THE COMMAND REFERENCE

zcat

Print the contents of compressed files to the screen.

Last updated

Portable / common

Implementation-level reference; examples are not certified for every installed version.

Overview

zcat decompresses a compressed file and writes the result to standard output, leaving the file alone. It is gzip -dc under a shorter name, so you can read a .gz file, for example a log, without unpacking it on disk. The name goes back to the older compress tool, whose .Z files zcat first read, and that history is why implementations differ.

Background

Today the name covers GNU zcat (part of gzip), the zcat that macOS ships (part of its gzip, with the name gzcat as an alias) and the zcat applet in BusyBox. They agree on reading a compressed file from standard input and writing it to standard output.

Examples

Read a compressed file from standard input; this form works on every implementation:

printf data | gzip > f.gz && zcat < f.gz

Pipe it into another command to look at a compressed log:

printf 'a\nb\n' | gzip > f.gz && zcat < f.gz | wc -l | tr -d ' '

Do the same with gunzip -c, which names the file and works everywhere:

printf data | gzip > f.gz && gunzip -c f.gz

Common mistakes

On macOS, zcat f.gz fails with "can't stat: f.gz (f.gz.Z)", because that zcat looks for a .Z file by default; use zcat < f.gz, gzcat f.gz or gunzip -c f.gz instead. zcat reads gzip data, and on GNU also the old .Z format; it is not for .zip, .bz2 or .xz files, which have their own tools. A file that is not compressed gives an error, not its own text.

Portability

Only the standard-input form and gunzip -c are in the Common examples. The file-name form zcat f.gz works on GNU and BusyBox but not on macOS. The Common examples were executed on GNU gzip 1.14, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.