Finding a method on how to use zcat command on Linux can help you with file compression and reclaim plenty of disk space. Here’s how it’s done.
In computing, compression may help reduce a file’s size to a smaller size without losing any information. This saves precious space on the disk.gzip
and gunzip
are the utilities used to compress and decompress files in Linux. The compression process includes not only reducing the file size but also bundling together many files in a single archive.
Users may want to uncompress a file for several trivial tasks, such as seeing the file’s contents. However, in various situations, you may want to view the contents without actually uncompressing the file. For instance, you have compressed a very large file, and decompressing it may take a lot of time. To save time, you may want to first peek into the contents of the file.
zcat
is the utility that can be used in such situations. It is a utility that expands a compressed file enabling one to view the contents. It is very much identical to gunzip -c. This article provides an overview of zcat
command along with its various options.
How to Use zcat Command on Linux
Usage of zcat command
Following is the syntax of the zcat
command:
$zcat [options] filename
Where the filename is the name of the file you want to view the content. Note that zcat
doesn’t rename the file, nor does it remove the .gz
extension. The original file remains intact. All it does is write the output to the standard output stream (stdout
).
Viewing the version information of zcat
zcat
may be installed in your system as gzcat
. To view the version of zcat
installed on your system along with its various options, use the following command:
$zcat -V
This will display the version information and compile options to the standard output stream.
Viewing the contents of a zipped file
Suppose you have a file linux.txt
. To view the contents of the file, type the following command:
$cat linux.txt
To compress this file, the following command can be used:
$gzip linux.txt
Now, if you view the compressed file linux.txt.gz
using the cat command, you will see the garbled output, as the file is compressed. Despite the file linux.txt
being compressed, one can still view the content of the file by using the following command:
$zcat linux.txt.gz
The zcat
command doesn’t identify a file based on the extension. In fact, it will uncompress a file that has the correct magic number no matter if the file has a .gz extension or not.
Viewing multiple zipped files
One can view multiple compressed files with a single command with the following:
$zcat file1.gz file2.gz
Here, file1.gz
and file2.gz
are the names of the files whose contents you want to view. The same format can be used to view multiple files. All you need to do is to pass the name of all the files as follows:
$zcat file1.gz file2.gz file3.gz ...
Enable pagination
If the contents of the file are large, one can enable pagination using more or less command. The following command shows how you can enable pagination:
$zcat linux.txt.gz | more
Alternatively, you can view the contents with pagination as follows:
$zcat linux.txt.gz | less
There is also another approach to enable pagination. There are alternate commands zmore
and zless
that can enable pagination. The following lines can be used to enable pagination:
$zmore linux.txt.gz
Alternatively, one can also use:
$zless linux.txt.gz
Getting the properties of a file without uncompressing
You can also get the properties of a file without actually decompressing it. Properties may include the compressed file size, uncompressed size, compression ratio, and uncompressed name. For this purpose, the -l option
may be used. The following command illustrates how we can get the properties of a file:
$gcat linux.txt.gz -l
Suppress all the warnings
To suppress all the warnings that may arise while viewing a file, you can use the following command:
$gcat linux.txt.gz -q
Working with non-compressed file
By default, zcat
doesn’t work with an uncompressed file. If you want to view a non-compressed file, you can cat command. However, if you try to view a simple file with zcat
, it will display an error as there is no magic number associated with the file. However, one can force zcat
to display contents whether the file is compressed or not. For this purpose, one can use the -f flag
. The following command shows how one can force zcat
to display a non-compressed file:
$zcat -f linux.txt
Other options of zcat
There are several other options available for zcat
. For instance:
-n option
is used to omit the compressed file header from the file
-L option
can be used for licensing-related tasks
Tip: zcat
command exits with a status of 1 if the file was not produced by the compress command or the input file can’t be read, or the output file can’t be written
We discussed the zcat command that can be used to view a zipped file without actually uncompressing it. This can save a lot of time if all you want is to just peek into the content of the file. The guide discusses various options such as viewing a file, and its properties, viewing multiple files, and suppressing warnings while viewing a file. This article serves as a tutorial for beginners. For more information, you may consult the Linux manual via using the command: $man gcat
.
If this guide helped you, please share it.