zcommands: Read gzip compressed text files on fly

by Vivek Gite on August 31, 2007 · 7 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 7 comments… read them below or add one }

1 gregf August 31, 2007

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

Reply

2 Binny V A September 1, 2007

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

Reply

3 raj September 1, 2007

hey thanks for zcommand, really useful :D

Reply

4 vivek September 1, 2007

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!

Reply

5 Jeff Schroeder September 5, 2007

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

Reply

6 tarun January 28, 2009

it’s very useful to handle zip file, it’s special in grep format

Reply

7 Danny December 5, 2011

you can also use gunzip -c, this will send file to standard output.. example:

$ sudo gunzip -c /var/log/dmesg.*.gz | grep -i duplex
[ 12.194310] tg3 0000:02:00.0: eth0: Link is up at 100 Mbps, full duplex
[ 12.405853] tg3 0000:02:00.0: eth0: Link is up at 100 Mbps, full duplex
[ 12.223136] tg3 0000:02:00.0: eth0: Link is up at 100 Mbps, full duplex
[ 45.822750] tg3 0000:02:00.0: eth0: Link is up at 100 Mbps, full duplex

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 7 + 3 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: