opscape

Archives and compression

zcat

Print the decompressed contents of gzip files to standard output, leaving the files compressed.

zcat [OPTION...] [FILE...]

8 of 8 examples run in Debian 13.7 on 2026-10-31 · gzip 1.13-1+deb13u1 · manual: zcat(1)

zcat file.gz is cat for gzip files: it decompresses to standard output and leaves the .gz on disk untouched. It is the same as gzip -dc (or gunzip -c), and it is how you feed a rotated log such as app.log.2.gz into grep, awk or wc without unpacking it.

The gzip package ships a family around it: zgrep (grep inside .gz files), zless and zmore (page through them), zdiff and zcmp. The other compressors have their own versions: bzcat, xzcat, zstdcat.

With -f, zcat passes files that are not compressed through unchanged, so one command reads a mix of current and rotated logs.

Examples

Print a compressed file

Setup run before this example
gzip todo.txt
zcat todo.txt.gz
explain
output
buy milk
fix login bug
write tests
fix logout bug
deploy v2.4

The original text, decompressed on the fly. todo.txt.gz is still compressed on disk afterwards.

Search a compressed log without unpacking it

Setup run before this example
gzip access.log
zcat access.log.gz | grep -c ' 404 '
explain
output
4

The decompressed stream goes straight into grep, which counts four 404 responses. No temporary file is created.

Peek at the start of a big compressed file

Setup run before this example
gzip access.log
zcat access.log.gz | head -n 2
explain
output
192.0.2.10 - - [14/Mar/2026:08:01:12 +0000] "GET / HTTP/1.1" 200 5120 "-" "Mozilla/5.0 (X11; Linux x86_64)"
192.0.2.10 - - [14/Mar/2026:08:01:13 +0000] "GET /static/app.css HTTP/1.1" 200 1840 "https://example.com/" "Mozilla/5.0 (X11; Linux x86_64)"

head stops after two lines, and zcat stops when the pipe closes, so this is quick even on gigabyte logs.

Read current and rotated logs together

Setup run before this example
gzip logs/app.log.1
zcat -f logs/app.log.1.gz logs/app.log | wc -l
explain
output
15

logs/app.log.1.gz is compressed and logs/app.log is not. -f passes uncompressed files through as they are, so the 3 old lines and 12 current ones arrive as one stream of 15.

Concatenate several compressed files

Setup run before this example
gzip todo.txt
gzip data/poem.txt
zcat todo.txt.gz data/poem.txt.gz
explain
output
buy milk
fix login bug
write tests
fix logout bug
deploy v2.4
The quick brown fox
jumps over the lazy dog.
The dog sleeps;
the fox runs on.

Like cat, zcat prints the files one after another with no separator.

grep inside compressed files with zgrep

Setup run before this example
gzip logs/app.log logs/app.log.1
zgrep -H ERROR logs/*.gz
explain
output
logs/app.log.gz:2026-03-14 08:16:40 ERROR health check failed: database timeout after 5000ms
logs/app.log.gz:2026-03-14 08:30:00 ERROR disk usage on /var at 91%

zgrep takes the usual grep options and decompresses each file first. -H prefixes every match with the file name, as grep does with several files.

A file that is not compressed

zcat todo.txt
explain
outputexit 1

gzip: todo.txt: not in gzip format

Without -f, zcat refuses a plain file with not in gzip format and exit status 1.

The longhand form

Setup run before this example
gzip -k notes.txt
gunzip -c notes.txt.gz | head -n 3
explain
output
Meeting notes, March
- Move backups to the new server
- Rotate the API key before Friday

gunzip -c and gzip -dc do exactly what zcat does. Scripts that must also run on systems where zcat means something else use one of these.

Common pitfalls

  • zcat only decodes gzip (and the old .Z and pack formats). For .bz2, .xz and .zst use bzcat, xzcat and zstdcat.

  • zcat *.gz | grep x loses the file names; use zgrep x *.gz to keep them.

  • zcat file.gz > file is fine, but zcat file.gz > file.gz truncates the input before reading it, destroying it.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • On older macOS and some commercial Unix systems, zcat only handled .Z (compress) files and looked for name.Z; gzcat or gzip -dc were the gzip spellings there. gzip -dc is the portable choice.

Options worth knowing

A selection, described in our own words. For the full list, see zcat(1) or zcat --help.

OptionWhat it does
-f, --forcePass files that are not compressed through unchanged.
-q, --quietSuppress warnings.
-v, --verboseReport the name and ratio of each file processed.
-t, --testCheck integrity instead of printing.

Related