How to Read Compressed Gzip Logs with zless in Linux

Overview

Inspecting rotated or archived system logs usually introduces unnecessary disk churn when archives must be decompressed before reading. The zless utility solves this bottleneck by providing immediate, full-featured terminal viewing directly on compressed files. In this guide, we explore practical techniques and core flags that streamline incident investigation while minimizing resource overhead on production servers.

Understanding zless and Preparing Sample Compressed Files

To configure an appropriate testing environment with necessary compression utilities, install the requisite packages via apt-get update -qq && apt-get install -y gzip util-linux.

Installing gzip and util-linux packages

Keep the exercise isolated from other projects by setting up a dedicated workspace using mkdir -p ~/zless_demo && cd ~/zless_demo.

Creating and entering the sandbox directory

Generate an initial mock application log containing typical server activities with printf 'Line 1: Server initialized\nLine 2: Connection received from 192.168.1.10\nLine 3: Processing API request: GET /items\nLine 4: Database query completed in 12ms\nLine 5: Response 200 OK sent\n' > app.log.

Writing mock records into app.log

Create a compressed archive while retaining the uncompressed source file by running gzip -k app.log.

Compressing app.log while preserving the original

Verify the coexistence and respective sizes of both the plaintext log and its new .gz archive using ls -lh app.log*.

Listing generated plaintext and compressed logs

Ensure your terminal shell is operating within the demo directory by executing cd ~/zless_demo.

Switching to the working directory

Behind the scenes, zless acts as a wrapper around the standard less pager, dynamically decompressing gzip streams in memory on the fly.
The file opens in an interactive terminal viewport, allowing smooth navigation using the arrow keys or Spacebar, while pressing q safely exits back to the shell prompt.

Viewing app.log.gz interactively with zless

Passing the -N flag instructs the pager to prepend explicit line numbers alongside each output entry.
This capability proves invaluable during incident post-mortems when correlating stack traces or cross-referencing specific log positions with team members.

Displaying line numbers with zless -N

Inspecting Multiple Archives and Jumping Directly to Matches

Return to the practice folder before proceeding with multi-file inspections by running cd ~/zless_demo.

Revisiting the demonstration directory

Simulate a secondary service log by executing printf 'Line 1: Worker process spawned\nLine 2: Warning: High memory utilization 84%%\nLine 3: Task completed\n' > worker.log.

Generating worker.log containing a simulated warning

Archive this second log file while keeping the raw file intact via gzip -k worker.log.

Compressing worker.log with gzip -k

The command zless -p 'Warning' app.log.gz worker.log.gz loads multiple archives sequentially into the pager queue and automatically jumps the screen straight to the first occurrence matching the pattern ‘Warning’.
Within this viewing session, jump ahead to the next archive using :n or step backward to previous files using :p, enabling seamless traversal across batched rotation logs.

Searching patterns across multiple archives with zless -p

Plain Text Compatibility and Production Troubleshooting Tips

Switch once more to the sandbox directory by executing cd ~/zless_demo.

Changing directory to ~/zless_demo

A major convenience of zless is its built-in fallback: standard uncompressed text files are handled just as effortlessly as archives.
Administrators can use identical muscle memory across mixed directories without checking file extensions or compression states beforehand.

Opening uncompressed text files directly in zless

When an interactive viewport is unnecessary and simple line matching will suffice, zcat app.log.gz | grep '200 OK' streams the decompressed output directly into the pipeline.
This approach extracts targeted log signatures immediately without leaving the active shell context.

Streaming compressed content through grep via zcat

After completing the tests, clean up the temporary directory and all generated sample files with rm -rf ~/zless_demo.

Purging temporary tutorial files

By mastering zless, system engineers and DevOps practitioners can dissect archived server telemetry rapidly while avoiding unnecessary decompression cycles and conserving local storage.