Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Archives and Compression

Linux zcat, zgrep and zless commands

Reference for zcat, zgrep, zless, zdiff and their bz and xz variants: read and search gzip, bzip2 and xz files without unpacking them, ideal for rotated logs.

Log rotation and package tools leave you with plenty of compressed files such as syslog.2.gz. Instead of unpacking each one, a family of small wrapper commands decompresses on the fly and hands the text to the tool you know. zcat prints a file like cat, zgrep searches like grep, zless pages like less, and zdiff compares like diff.

Each family has variants for the other formats: bzcat, bzgrep, bzless for bzip2 and xzcat, xzgrep, xzless for xz. Nothing is written to disk; the data is streamed through a pipe, so the compressed file stays untouched.

Terminal
zcat /var/log/syslog.2.gz | less
zgrep -i 'error' /var/log/syslog.*.gz

Try it

Bash Search and print rotated logs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Other formats and comparing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
zcat [FILE.gz]...
zgrep [GREP-OPTIONS] PATTERN [FILE...]
zless [FILE...]
zdiff FILE1 [FILE2]
zcmp FILE1 [FILE2]
Command Meaning
zcat Same as gzip -dc: write the decompressed data to standard output.
zgrep Run grep on decompressed data; accepts normal grep options such as -i, -n, -c, -E, -v.
zegrep, zfgrep Old names for zgrep -E and zgrep -F.
zless, zmore Pager for compressed files.
zdiff, zcmp Compare compressed files, or one compressed and one plain.
bzcat, bzgrep, bzless, bzdiff The same for bzip2.
xzcat, xzgrep, xzless, xzdiff The same for xz.
zstdcat, zstdgrep, zstdless The same for zstd where installed.

On many systems zcat accepts .Z (compress) files too; zgrep and zdiff accept uncompressed files as well and simply pass them through.

Returns: zcat returns 0 on success. zgrep follows grep: 0 if a line matched, 1 if none, 2 on errors. zdiff follows diff: 0 identical, 1 different, 2 trouble.

Usage notes

  • Order of rotated logs. app.log.1.gz is newer than app.log.2.gz. Use zcat app.log.{3,2,1}.gz (explicit order) rather than a glob when you need chronological output.
  • File names in results. With several files zgrep prefixes each match with the file name, like grep. Add -h to hide it or -H to force it for a single file.
  • Combine current and rotated logs: zgrep -h error /var/log/app.log* works because plain files pass through; but the glob also matches the uncompressed app.log.1 and similar, which is fine.
  • They are shell scripts. zgrep runs gzip -cdfq and pipes into grep, so complex options are subject to those scripts' own parsing; for special cases write the pipeline yourself: zcat file.gz | grep -P '...'.
  • Performance. Decompression runs at pipe speed, so searching a huge file is usually bound by gzip and grep combined. For frequent searches of large archives, index the data or use a log system.
  • Seeking is impossible. A gzip stream cannot be read from the middle, so zless has to decompress from the start when you jump to the end.
  • macOS and BSD. zcat there may expect .Z files; use gzip -dc or gunzip -c for portable scripts.
  • No -r for directories in zgrep; combine with find ... -name '*.gz' -exec zgrep ... {} +.
  • Write nothing to the terminal. zcat of binary data into a terminal garbles it; pipe to file - or head -c to see what is inside.