Mastering the Linux zcat Command: A Beginner’s Guide with 5 Practical Examples

While compression is generally useful for saving space on your system, command-line users often need to
uncompress files for small tasks, like checking their contents. Fortunately, there’s a command-line utility
called zcat that enables you to view the contents of compressed files effortlessly.

In this tutorial, we will cover the basics of zcat using easy-to-follow examples. Note that all the examples
provided have been tested on Ubuntu 16.04 LTS.

Linux zcat Command

The zcat command allows you to view the contents of a compressed file. Here is the syntax:

zcat FILE ....

The tool’s man page defines it as:

       zcat is identical to gunzip -c. (On some systems, zcat may be
       installed as gzcat to preserve the original link to compress.) zcat
       uncompresses either a list of files on the command line or its standard
       input and writes the uncompressed data on standard output.

The following Q&A examples will provide you with a better understanding of how zcat works:

Q1. How to use zcat?

Suppose you have a compressed text file, like test2.txt.gz. Using the standard cat command to view its contents will result in garbled output:

cat vs. zcat

In this case, zcat is the solution. Simply replace cat with zcat as shown:

zcat test2.txt.gz

How to use zcat

The content of the compressed file is displayed on standard output.

Q2. Does zcat work with multiple inputs?

Yes, it does. You can provide the names of multiple compressed files:

zcat test.txt.gz test2.txt.gz

The command performs as follows:

Does zcat work with multiple inputs

Q3. Does zcat recognize compressed files based on extension?

No, zcat identifies compressed files by their magic number, not by the extension:

zcat will uncompress files that have the correct magic number whether they have a .gz suffix or not.

We removed the .gz extension and tested zcat, which successfully identified the compressed file:

Does zcat recognize compressed file based on extension

The tool was still able to determine it was a compressed file and displayed its content.

Q4. How to handle pagination while using zcat?

You can achieve pagination by using standard more
and less commands as follows:

zcat [FILENAME] | more
zcat [FILENAME] | less

Alternatively, you can use the zmore and zless commands:

zmore [FILENAME]
zless [FILENAME]

Q5. Does zcat work with non-compressed files?

By default, no. Running zcat on a non-compressed file will result in an error, as shown below.

Does zcat work with non-compressed files

To force zcat to display file contents regardless of compression, use the -f option. Here’s how it works:

Zcat -f option

No error occurred in this scenario.

Conclusion

The zcat command can significantly streamline your workflow if you just need to examine the contents of a
compressed file. Regardless of your proficiency with the Linux command line (novice or expert), familiarity with
this command is highly beneficial. We’ve covered some essential aspects of zcat here; for more details, refer to
its man page.

Frequently Asked Questions (FAQ)

What file types does zcat support?

zcat supports gzip-compressed files, identified by their magic number rather than the file extension.

Is there a risk of data loss with zcat?

No, zcat only reads and decompresses files; it does not modify or delete them.

Can zcat decompress files with non-standard file extensions?

Yes, as long as the file conforms to the gzip format, zcat can decompress it regardless of the extension.

How is zcat different from gunzip -c?

zcat is functionally identical to using gunzip -c, both commands decompress files to standard output.