nixCraft Poll

Topics

zcommands: Read gzip compressed text files on fly

Posted by Vivek Gite [Last updated: August 31, 2007]

Both Linux and Unix like oses comes with z commands. It allows you to read gzip compressed text files using zless, zcat and zmore etc. gzip reduces the size of the files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. zcommand has some cool usage - such as display the current time in different zonename.

The old way...

Let us say you have a file called data..txt.gz. To display file you can type:
gzip -d data.txt.gz
cat data.txt
less data.txt

The new way...

Just use zless command:
zless data.txt.gz
zmore data.txt.gz

zcat command

Concatenate compressed files and print on the screen:
zcat file.gz

zdiff / zcmp command

Compare compressed files:
zdiff file1.gz file2.gz
zcmp file1.gz file2.gz

zegrep / zfgrep / zgrep command

Search compressed files for a regular expression:
zegrep -w '^word1|word2' file.gz
zgrep 'word' file.gz

zless / zmore commands

zmore and zless is a filter which allows examination of compressed or plain text files one screenful at a time on a screen. zmore works on files compressed with compress, pack or gzip, and also on uncompressed files. If a file does not exist, zmore looks for a file of the same name with the addition of a .gz, .z or .Z suffix.
zmore file.gz
zless file.gz

znew command

Znew recompresses files from .Z (compress) format to .gz (gzip) format. If you want to recompress a file already in gzip format, rename the file to force a .Z extension then apply znew.
znew file.Z

zdump command

zdump command prints the current time in each zonename named on the command line. Let us say your current time zone is IST (Indian standard time) and like to see time current time for Los Angeles (USA - PDT), enter:
date
Output:

Fri Aug 31 20:51:39 IST 2007

Now display Los Angeles current time :
zdump /usr/share/zoneinfo/America/Los_Angeles
Output:

/usr/share/zoneinfo/America/Los_Angeles  Fri Aug 31 08:20:31 2007 PDT

zipgrep command

Search files in a ZIP archive for lines matching a pattern:
zipgrep *.cpp basesys.zip

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. gregf Says:

    Another good thing to remember is vim can handle gzipped files so if your that anal over space you can still edit the file.

  2. Binny V A Says:

    For reading a BZipped file…
    bzcat file.xml.bz2

  3. raj Says:

    hey thanks for zcommand, really useful :D

  4. vivek Says:

    gregf, oh yes I forgot about vim handling .gz file. Good reminder…

    Binnay, another good reminder

    raj, no problem :D

    Appreciate all of your posts!

  5. Jeff Schroeder Says:

    Or just make sure you are using gnu less because it autodetects and autodecompresses gzip / bz2 files for you.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.