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.
zcat /var/log/syslog.2.gz | less zgrep -i 'error' /var/log/syslog.*.gz
Try it
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.
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
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.gzis newer thanapp.log.2.gz. Usezcat app.log.{3,2,1}.gz(explicit order) rather than a glob when you need chronological output. - File names in results. With several files
zgrepprefixes each match with the file name, likegrep. Add-hto hide it or-Hto 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 uncompressedapp.log.1and similar, which is fine. - They are shell scripts.
zgreprunsgzip -cdfqand pipes intogrep, 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
gzipandgrepcombined. 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
zlesshas to decompress from the start when you jump to the end. - macOS and BSD.
zcatthere may expect.Zfiles; usegzip -dcorgunzip -cfor portable scripts. - No
-rfor directories inzgrep; combine withfind ... -name '*.gz' -exec zgrep ... {} +. - Write nothing to the terminal.
zcatof binary data into a terminal garbles it; pipe tofile -orhead -cto see what is inside.