Linux zcat Command Tutorial for Beginners (5 Examples)
While compression is helpful in general as it allows you to save space on your system, command line users need to uncompress the file for several small tasks, like viewing what all the file contains. However, there exists a command line utility - dubbed zcat - that lets you take a peek inside compressed files without any additional effort.
In this tutorial, we will discuss the basics of zcat using some easy to understand examples. Please note that all examples in this article have been tested on Ubuntu 16.04 LTS.
Linux zcat command
The zcat command lets you view contents of a compressed file. Following is its syntax:
zcat FILE ....
And here's how the tool's man page explains it:
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.
Following are some Q&A-styled examples that should give you a good idea on how the zcat command works.
Q1. How to use zcat?
Suppose you have a compressed text file, say test2.txt.gz. If you try viewing it contents using the standard cat command, you'll see a garbled output:
So in this case, you can use the zcat command. Basic usage is very simple - just replace cat with zcat.
So you can see the content of the compressed file was displayed on standard output.
Q2. Does zcat work with multiple inputs?
Yes, it does. All you have to do is to pass the names of compressed files as input.
zcat test.txt.gz test2.txt.gz
Here's the aforementioned command in action:
Q3. Does zcat recognize compressed file based on extension?
No. That's not the case. Here's what the man page says about this:
zcat will uncompress files that have the correct magic number whether they have a .gz suffix or not.
To verify this, we removed the .gz extension from one of the compressed files, and tried running zcat on it.
So you can see the tool was still able to learn that it's a compressed file, and was able to display its content on STDOUT.
Q4. How to handle pagination while using zcat?
There are two ways to do this. Either you can use the standard more and less commands.
zcat [FILENAME] | more
zcat [FILENAME] | less
Or, use the zmore and zless commands.
Q5. Does zcat work with non-compressed files?
By default no. If you try running the zcat command with a non compressed file as input, you'll see an error like following.
However, if you want to force zcat to display file contents in output whether or not the file is compressed, then use the -f command line option. The following screenshot shows this option in action:
So you can see the tool didn't throw any error this time around.
Undoubtedly, the zcat command saves you time, if all you want is to take a look at the contents of a compressed file. So, regardless of what kind of Linux command line user you are (newbie or pro), you should learn about this command. We've already discussed some useful details related to zcat here. For more, head to its man page.