File compression plays an important role in the life of a system administrator and a generic operating system user as it helps us in saving disk space by allowing us to store more data within the same chunk of available storage media.
The Linux operating system provides more than one utilities which allows us to compress files to save disk space.
Some of the popular ones are zip, gzip and bzip. All these utilities are used to compress files with slight differences in the command line options they provide and also the level of compression they provide. The utilities which allow us to compress files also come with complimentary utilities to uncompress the files. But what if we find ourselves in a scenario wherein we need to view the contents of a file without extracting or uncompressing it? In such a situation we can use the zcat command to view the contents of the file without extracting it. In this article we will explain how we can make the best use of the zcat command with some practical examples.

The zcat command should be available on your Linux distribution as part of the base installation.

[root@linuxnix ~]# yum whatprovides zcat
----------------output truncated for brevity
gzip-1.5-10.el7.x86_64 : The GNU data compression program
Repo : base
Matched from:
Filename : /usr/bin/zcat

Here is a description of the zcat command available in its man page:

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.  zcat will uncompress files that have the correct magic number whether they have a .gz suffix or not.

The basic syntax for using the zcat command is as follows:

zcat <file name>

We’ll now demonstrate the usage of the zcat command with the help of some example:

Example 1: View the the contents of a single compressed file.
To view the contents of a compressed file we simply need to type the zcat command followed by the file name and then press enter.

[root@linuxnix ~]# zcat os.txt.gz
[root@linuxnix ~]# ls -l os.txt.gz
-rw-r--r-- 1 root root 65 Aug 25 04:32 os.txt.gz
[root@linuxnix ~]#

In the above example we view the contents of the file os.txt.gz without uncompressing it.

Example 2: View the contents of multiple comressed files.
To view the contens of multiple compressed files we need to type the zcat command followed by the file names separated by spaces.

[root@linuxnix ~]# zcat os.txt.gz users.txt.gz


Example 3: Use zcat like cat command.
Although the zcat command is meant to view the content of compressed files only but we can also view the content of regular files by adding -f option. Given below is an example.

[root@linuxnix ~]# zcat -f hello.txt
Hello World!
I'm Sahil!
[root@linuxnix ~]#

Example 4: View properties of a compressed file.
To view the properties compressed size, uncompressed size,compression ratio, uncompressed_name of a compressed file, use the -l flag. Given below is an example.

[root@linuxnix ~]# zcat -l os.txt.gz
compressed uncompressed ratio uncompressed_name
65 38 -5.3% os.txt
[root@linuxnix ~]#

Example 5: Applying pagination while using the zcat command.
We can pipe of the output of the zcat command to paging command more and less. We could also use the zmore and zless commands.

[root@linuxnix ~]# which zmore
[root@linuxnix ~]# which zless
[root@linuxnix ~]#

zmore <file name> is the same is zcat <file name> | more


This concludes our explanation of the zcat command with examples. We hope that you’ve found this article to be useful and we look forward towards your suggestions and feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.