zcat
Print gzip-compressed file contents to stdout
By CMD Script Team · 4 min read · Last updated
zcat [OPTIONS] FILE.gz...Options
| Flag | Description |
|---|---|
-f | Force decompression even if the file doesn't have the expected .gz suffix or is a symlink |
-l | List compressed size, uncompressed size, ratio, and name instead of printing contents |
-v | Verbose: print the filename and compression ratio to stderr as each file is read |
-q | Quiet: suppress warning messages |
-S SUF | Use SUF as the compressed-file suffix instead of the default .gz |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
zcat decompresses one or more gzip-compressed (.gz) files and writes the result
straight to stdout, without creating a decompressed copy on disk and without modifying
the original file. It's functionally identical to gzip -dc, and exists as a convenience
alias so you don't have to remember the -dc flag combination every time you want to
peek inside or pipe a compressed file into another command.
Beginner examples
zcat file.gz— print the decompressed contents to the terminalzcat file.gz | less— page through a compressed file's contentszcat file.gz > file— decompress to a new file while keepingfile.gzzcat file.gz | wc -l— count lines in a compressed file without extracting it
zcat nginx-access.log.gz | less
Advanced examples
- Search across many rotated, compressed logs at once:
zcat /var/log/app/*.log.gz | grep "timeout" - Feed a compressed CSV directly into a processing pipeline:
zcat data.csv.gz | cut -d, -f2 | sort | uniq -c - Concatenate a series of log rotations into a single decompressed archive:
zcat access.log.1.gz access.log.2.gz access.log.3.gz > access-combined.log - Check a compressed file's stats before deciding whether to fully decompress it:
zcat -l big-dataset.gz
zcat /var/log/nginx/access.log.*.gz | awk '{print $1}' | sort | uniq -c | sort -rn | head
Common mistakes
- Trying to run
zcaton a non-gzip file (like plain text or.zip) and getting a "not in gzip format" error — check the file type withfilefirst. - Forgetting
zcatdoesn't remove or modify the source.gzfile, and then separately runningrmunnecessarily, thinking the original needs cleanup. - Redirecting
zcat's output over the compressed file itself (zcat file.gz > file.gz), which corrupts data because the shell truncates the output file beforezcatfinishes reading input. - Using
zcaton huge files and printing straight to a terminal instead of piping tolessorhead, flooding the screen.
Tips
- Use
zgrep,zless,zdiff, andzcmpas siblings ofzcat— they all operate on compressed files by decompressing on the fly, so you rarely need to manually decompress just to inspect content. - Combine with
tar:zcat archive.tar.gz | tar -tvf -lists an archive's contents without extracting (thoughtar tzf archive.tar.gzis more idiomatic for tarballs). - When only checking a compressed log's size stats, use
-linstead of decompressing the whole thing.
Best practices
- Prefer streaming (
zcat file.gz | some-command) over decompressing to a temporary file when you only need to read the data once — it saves disk I/O and cleanup. - Avoid
zcat-ing untrusted files of unknown size directly into memory-hungry commands; check-loutput first so you don't accidentally decompress a multi-gigabyte "gzip bomb." - In scripts, check the exit status of
zcat(or the pipeline viaPIPESTATUS) rather than assuming decompression succeeded, especially with truncated or corrupted files.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Reading rotated log files (
access.log.2.gz,access.log.3.gz) thatlogrotatehas already compressed, without restoring them to disk first. - Feeding compressed data exports (database dumps, CSV extracts) directly into
downstream processing tools like
awk,cut, orjqin a pipeline. - Quickly grepping across years of compressed historical logs during an incident
investigation, one
zcat ... | grep ...pipeline at a time.
Common interview questions
- What is zcat equivalent to?
gzip -dc— decompress to stdout, leaving the source file untouched. - How would you search for a string across multiple compressed log files without
extracting them?
zcat *.gz | grep "pattern", or more directlyzgrep "pattern" *.gz. - Why shouldn't you redirect zcat's output back onto the same .gz file? The shell
truncates the destination file for writing before
zcatreads it, so you'd be reading from a file that's simultaneously being emptied — resulting in data loss.
Frequently Asked Questions
What's the difference between zcat and gunzip -c?
They're equivalent: zcat is literally implemented as `gzip -dc`, decompressing to stdout without touching the original file or writing a decompressed copy to disk.
Can zcat read plain, uncompressed files?
With GNU gzip's zcat, no — it expects gzip-format input and errors out otherwise. Some other implementations (like zcat on macOS/BSD in certain configurations) tolerate `.Z` files too, but don't rely on that; use `cat` for plain files.
How do I search inside a compressed log without decompressing it first?
Pipe zcat into grep: `zcat access.log.gz | grep "500"`. For repeated searches, `zgrep` does the same thing in one command.
Can I concatenate multiple compressed files with zcat?
Yes — `zcat a.gz b.gz c.gz` decompresses each in turn and streams all their contents to stdout as one continuous stream, just like `cat` does for plain files.
Cheat sheet
Download a quick-reference cheat sheet for zcat.