Reading gz files with zcat

The Debian Policy Manual dictates that all packages should come with documentation. In order to save space in the debian archive these documents need to be compressed with gzip. There are a ton of these files floating around in the /usr/share/doc directory. Recently I wanted to read some of the documentation. If you try to open the file with cat it spits out binary gibberish.

You can of course unzip the file as you normally would and open it up that way, but it turns out there is an easier way. Using zcat you can read the contents of compressed files just like you would with cat.

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.

GZIP(1) man page.

By default, this will put all of the output into your terminal window, which is fine for most files. The other place where this can come in handy is when you are trying to look through compressed log files. In this case, having to scroll around the terminal may not be a great option. You can pipe the output of zcat into other programs such as less in order to be able to page through long files.

For example, if I wanted to read the first 10 lines of a compressed log file, I could do so with the following command:

[email protected]:/var/log$ sudo zcat syslog.2.gz | head -n 10

The output of this command would look like this:

May  2 22:27:43 debvm rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="585" x-info="http://www.rsyslog.com"] start
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x1a] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x1b] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x1c] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x1d] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x1e] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x1f] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x20] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x21] high edge lint[0x1])
May  2 22:27:43 debvm kernel: [    0.000000] ACPI: LAPIC_NMI (acpi_id[0x22] high edge lint[0x1])

This is of course much more useful than the output I would get if I tried to use cat (show below).

[email protected]:/var/log$ sudo cat syslog.2.gz | head -n 10
[
Y�\ks�:����+0�qf-��9���+�*W��ݩlJE���1%꒔m��o7��E�C���o$��C4�}�������gr϶H(�3���<I'�G��Y<��$O�����ٛ��ސ�u��n�|`��S7�������n>�t󁥛,�|`��K7X������n>�t󁥛,�|`��K7X������n>�u󁭛l�|`��[7غ�����n>�u󁭛l�|`��[7غ�����n>pt󁣛�|���G78�����>pt󁣛�|���G78�����>`��������n>`��������n>`��������n>`��������n>���������n>���������n>���������n>��������x�|���D6 M�a��<�w�
                                                     N�x4�s��h
R���y�_�<?����ܐG(?����)�8��cZ�Ԡ�����r��r2^�a��ٟ�j��c?Qo̝
                                                          �q��d�ː�W$�eq(��h�l͒�ZK_�x>Q��I�����Ҍ

 

If you made it this far, you should probably follow me on twitter. 🙂

This entry was posted in Linux and tagged . Bookmark the permalink.

One Response to Reading gz files with zcat

  1. Pingback: Terminal Reader Mode with Pandoc and Less | levlaz

Leave a Reply

Your email address will not be published. Required fields are marked *