Archives and compression
zcat
Print the decompressed contents of gzip files to standard output, leaving the files compressed.
zcat [OPTION...] [FILE...]
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.txtzcat todo.txt.gz
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.logzcat access.log.gz | grep -c ' 404 '
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.logzcat access.log.gz | head -n 2
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.1zcat -f logs/app.log.1.gz logs/app.log | wc -l
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.txtzcat todo.txt.gz data/poem.txt.gz
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.1zgrep -H ERROR logs/*.gz
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
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.txtgunzip -c notes.txt.gz | head -n 3
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
zcatonly decodes gzip (and the old.Zandpackformats). For.bz2,.xzand.zstusebzcat,xzcatandzstdcat.zcat *.gz | grep xloses the file names; usezgrep x *.gzto keep them.zcat file.gz > fileis fine, butzcat file.gz > file.gztruncates 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,
zcatonly handled.Z(compress) files and looked forname.Z;gzcatorgzip -dcwere the gzip spellings there.gzip -dcis the portable choice.
Options worth knowing
A selection, described in our own words. For the full list, see zcat(1) or zcat --help.
| Option | What it does |
|---|---|
-f, --force | Pass files that are not compressed through unchanged. |
-q, --quiet | Suppress warnings. |
-v, --verbose | Report the name and ratio of each file processed. |
-t, --test | Check integrity instead of printing. |
Related
gzipCompress files to .gz (replacing the original by default), or decompress them with -d or gunzip.catConcatenate files and print them to standard output; also the quickest way to see a short file.lessPage through a file or command output one screen at a time, with search and scrolling in both directions.grepPrint the lines of files (or standard input) that match a pattern.bzip2Compress files to .bz2 with the Burrows-Wheeler algorithm, or decompress them with -d, bunzip2 or bzcat.xzCompress files to .xz with LZMA2 for high ratios, or decompress them with -d, unxz or xzcat.zstdCompress files to .zst with Zstandard, fast at any ratio; decompress with -d, unzstd or zstdcat.