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 RHEL CENTOS UBUNTU SOLARIS AIX HP-UX [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 RHEL CENTOS UBUNTU SOLARIS AIX HP-UX sahil james clark
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 /usr/bin/zmore [root@linuxnix ~]# which zless /usr/bin/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.
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021