How to use zcat and zless to read compressed files without removing them in Linux

Introduction

On the day-to-day basis of a system manager or developer, it is common to find gzip-compressed files containing log, configuration or source code. Instead of uncompressing each file to inspect its content, Linux offers tools that allow you to read them directly from the command line. Two of the most useful are zcat and zless, which act as conscious versions of the compression of the classic cat and less. In this article we will explore what they do, how they work and when it is preferable to use each, with practical examples that you can apply immediately.

What are zcat and zless?

zcat is a utility that belongs to the gzip package and works exactly as the cat command, but is designed to read files that have been compressed with gzip (.gz extension). When you invoke zcat over a .gz file, the tool decompresses it to the flight and sends the result to the standard output, without creating any temporary file on the disk. For its part, zless is a less variant that also works directly with compressed files: it allows you to browse forward and back into the content, search patterns and move page to page, all without the need to previously uncompress the file. Both commands retain the same lags and options as their uncompression counterparts, making them intuitive for users used to cat and less.

How they work

Internally, both zcat and zless use the gzip decompression library to read the compressed data and turn them into a real-time legible byte flow. When the kernel receives the reading call, the tool intercepts the compressed blocks, decompresses them in memory and sends them to the standard output (in the case of zcat) or to the less interface (in the case of zless). This process is fully transparent for the user and requires no special privileges, as you only need reading permissions on the .gz. file. In addition, both commands can work with standard inputs: if a compressed data flow is spiked, they will process it in the same way, making them very useful in command chains where, for example, find, grep or awk is combined with .gz. files.

Practical examples of zcat

  • Show the contents of a compressed log: zcat / var / log / syslog.gz | head -20 shows the first twenty lines of the syslog.gz file without creating a decompressed copy; this is ideal to quickly review recent events on production servers.
  • Combine zcat with grep to look for patterns: zcat / usr / share / doc / package / changele.D.gz | grep -i 'fix' allows to locate all the words of the word 'fix' within the compressed changelog, saving time by avoiding complete decompression of the file.
  • Use zcat in awk pipes to extract columns: zcat acceso.log.gz | awk -F '' '{print $1,$4} '> ips _ fechas.txt processes the compressed access log, printing the IP address and time mark in a flat text file, all without creating intermediate files.

Practical examples of zless

  • Browsing a compressed manual: zless / usr / share / man / man1 / grep.1.gz allows you to move up and down, search with / pattern and leave with q, as would be done with less in a file without compressing.
  • Review a compressed configuration file in / etc: zless / etc / nginx / nginx.conf.gz facilitates the reading of directives, allows you to jump to specific sections with: line number and do search forward or back without uncompressing.
  • Use zless in combination with other commands: for example, find a compressed file with find and open it directly: find / var / log -name '*.gz '-exec zless {}; opens each compressed log in less for immediate inspection.

Advantages and limitations

The main advantage of zcat and zless is efficiency: they avoid the creation of temporary files and reduce the use of disk space, which is especially valuable in systems with limited storage or in container environments where each byte counts. In addition, they allow for real-time data flows, integrating without friction into pipes and automation scripts. However, they have some limitations: they cannot modify the compressed content (for that it is necessary to decompress, edit and recompress), and their performance depends on the speed of the CPU to decompress the flight; in very large files or in modestly powerful hardware, latency can be noticeable. Finally, they only work with the gzip format; other algorithms such as bzip2 or xz require their own variants (bzcat, bzless, xzcat, xzless).

Conclusion

In short, zcat and zless are essential tools for any Linux user working with compressed files. They allow you to read, search and navigate the content without the overhead to decompress and without taking up additional disk space. Its syntax is almost identical to that of cat and less, which reduces the learning curve and facilitates its adoption in scripts and daily workflows. If you have not yet incorporated them into your command repertoire, try them today and find out how much you can speed up your administration and cleansing tasks.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish