Inspect gzip Files without decompress: zcat, zless, & zmore
Linux provides several commands to work with compressed files such as zcat, zless, and zmore.
This tutorial will deep dive into the usage of these commands, allowing you to navigate and inspect compressed files without decompressing them.
Here’s a table that summarizes the difference between the 3 tools:
Tool | Description | Primary Use(s) | Interactive | Searching Capabilities |
---|---|---|---|---|
zcat | Displays compressed file content to stdout. | Quick content viewing, Piping data. | No | No (unless piped) |
zless | Interactive viewer for compressed files. | Deep dives into file content with scrolling, searching, and line jumps. | Yes | Yes |
zmore | Simple pager for compressed files. | Basic page-by-page viewing of gzipped content. | Somewhat | No |
Introduction to zcat
zcat
is akin to the standard cat
command but is designed to read gzip compressed files.
By default, zcat
decompresses the data and writes the results to standard output.
$ zcat file.txt.gz
When you run this command, the content of the compressed file.txt.gz
is displayed on the terminal.
If you try to use cat
on a gzipped file, you’ll get gibberish since cat
doesn’t know how to decompress on the fly.
When to use zcat over decompressing a file
Using zcat
has its advantages, especially when working with large compressed files.
- Space-saving: If you’re low on disk space, decompressing a large file might be infeasible. With
zcat
, you can view the content directly without needing extra space for the decompressed version. - Time-saving: Decompressing a file takes time. By using
zcat
, you can directly access the content, skipping the decompression process.
Force the reading of files
zcat
provides an option -f
to force the reading of files even if they aren’t compressed.
This is useful when you have a mixture of compressed and uncompressed files and you want to read them using a single command.
$ zcat -f *.log
If *.log contains a mix of gzipped and plain text log files, this will decompress the gzipped ones and pass through the plain text ones untouched.
Using pipes to filter the output
You can pipe the output of zcat
to other commands for filtering, searching, or other manipulations.
$ zcat file.txt.gz | grep "search_term"
With this command, you’re directly searching within the compressed file for lines that contain “search_term”.
This method is efficient as it allows you to search inside compressed files without needing to decompress them.
Introduction to zless
zless
is an interactive pager for compressed files. It’s quite analogous to the less
command but specializes in reading gzip-compressed files.
$ zless file.txt.gz
This command opens the compressed file.txt.gz
in an interactive viewing mode, allowing you to scroll through its content.
zless
allows you to interactively inspect compressed content.
Scrolling, searching, jumping to a line number
Inside the zless
environment, you have a rich set of controls:
- Scrolling: Use the arrow keys to scroll line-by-line or the space bar to scroll page-by-page.
- Searching: To find a specific term, type
/
followed by your search term. Pressn
to move to the next occurrence andN
for the previous. - Jumping to a line number: Type the desired line number, followed by
G
to jump directly to that line.
These controls mirror those in less
, ensuring a seamless experience when transitioning between viewing compressed and uncompressed files.
Configuration and customization
zless
allows you to configure your viewing experience:
- Changing the display colors: You can set the
LESS_TERMCAP
environment variables to customize the color scheme. For instance, to set the search highlights to blue:
export LESS_TERMCAP_so=$(tput setaf 4)
- Custom key bindings: While
zless
defaults are sufficient for many, you might prefer a custom set of key bindings. By setting theLESSKEY
environment variable to a keybindings file, you can define your personal shortcuts. - Setting default options: If you find yourself always using specific
less
options, you can make them default forzless
by exporting them in theLESS
environment variable. For instance:
export LESS='-N -g'
Here, -N
displays line numbers, and -g
highlights only the last search pattern.
Introduction to zmore
zmore
offers another way to inspect compressed files, functioning as a simple pager. Unlike zless
, zmore
offers basic paging capabilities.
$ zmore file.txt.gz
Executing the above will display portions of the compressed file.txt.gz
on your terminal. When you reach the end of a displayed portion, press space
to view more of the file.
zmore
offers a straightforward “more” functionality for gzipped files, hence its naming convention.
Scrolling, searching, and other essential controls
zmore
is designed to be a simple tool, and as such, its controls are more limited compared to zless
. Here are the primary controls you’ll use:
- Scrolling: Press
space
to scroll to the next portion of the file. - Searching: Unfortunately, direct in-app searching isn’t natively supported in
zmore
like inzless
. However, combining it with other tools (e.g.,zgrep
) can aid in searching. - Exit: To exit from
zmore
, simply pressq
.
While zmore
may seem basic, its simplicity can be an asset when you only need a quick glance at a compressed file without the overhead of additional features.
Using with zcat or zgrep
zmore
can be paired with other utilities to enhance its capabilities:
- Combining with zcat: For quick viewing and subsequent paging, you can combine
zcat
withzmore
.
$ zcat file.txt.gz | zmore
This allows you to first decompress the file to stdout and then page through it using zmore
.
- Searching with zgrep: While
zmore
lacks native searching,zgrep
fills that gap. To search and then page through the results:
$ zgrep "search_term" file.txt.gz | zmore
With this, you’ll see the lines containing “search_term” and can page through them.
Choosing the right tool
Selecting the right tool depends on the task at hand:
- Quick Content Viewing: If all you need is to quickly display the entire content of a compressed file,
zcat
is the tool. - Interactive Exploration: For a deeper, interactive dive into the contents, where you might want to scroll, search, or jump to specific lines,
zless
is the best choice. - Basic Paging: If you’re looking for a simple way to page through a gzipped file without added complexities,
zmore
is the choice.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.