• United States
Unix Dweeb

Viewing compressed file content on Linux without uncompressing

May 24, 20215 mins

You can examine the contents of compressed files on Linux systems without uncompressing the files or changing them in any way. This post explains the commands for doing that and the types of compressed files they work with.

stress test2 linux penguin stress ball hand squeezing by digitalsoul getty images 1136841639
Credit: DigitalSoul / Getty Images / Linux

If you need to check the contents of a compressed text file on Linux, you don’t have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The “cat” in each command name tells you that the command’s purpose is to display content. The “z” tells you that it works with compressed files.

Which of the two commands to use depends on the type of compressed file you are examining. If the file was compressed with gzip or zip, you would use the zcat command. If the file was compressed with bzip2, you would use the bzcat command. On some systems, zcat might be called gzcat.

You can identify the command used to compress a file by the compressed file’s extension.

  • .gz or .tgz = gzipped files, use zcat (or gzcat)
  • .zip = zipped files, use zcat (or gzcat)
  • .bz2 = bzipped files, use bzcat

In the example below, the top 20 lines of H. G. Wells’ “The War of the Worlds” is displayed from a file compressed with bzip2.

$ bzcat The_War.bz2 | head -20
No one would have believed in the last years of the nineteenth century
that this world was being watched keenly and closely by intelligences
greater than man’s and yet as mortal as his own; that as men busied
themselves about their various concerns they were scrutinised and
studied, perhaps almost as narrowly as a man with a microscope might
scrutinise the transient creatures that swarm and multiply in a drop of
water. With infinite complacency men went to and fro over this globe
about their little affairs, serene in their assurance of their empire
over matter. It is possible that the infusoria under the microscope do
the same. No one gave a thought to the older worlds of space as sources
of human danger, or thought of them only to dismiss the idea of life
upon them as impossible or improbable. It is curious to recall some of
the mental habits of those departed days. At most terrestrial men
fancied there might be other men upon Mars, perhaps inferior to
themselves and ready to welcome a missionary enterprise. Yet across the
gulf of space, minds that are to our minds as ours are to those of the
beasts that perish, intellects vast and cool and unsympathetic,
regarded this earth with envious eyes, and slowly and surely drew their
plans against us. And early in the twentieth century came the great

To display only a portion of the text, pass the command output to another command like the head command shown above or to a grep command that includes the text you are searching for. For example:

$ bzcat The_War.bz2 | grep vastness
Haggerston and Hoxton, and, indeed, through all the vastness of London
throughout the inanimate vastness of sidereal space. But that is a

After the bzcat display, the file remains compressed.

$ ls -l The_War.bz2
-rw-r--r--. 1 shs shs 100677 May 23 15:40 The_War.bz2

If you want to uncompress the file instead of just checking its content, use bunzip2.

$ bunzip2 The_War.bz2
$ ls -l The*
-rw-r--r--. 1 shs shs 345315 May 23 15:40 The_War

To compress the file again, use the bzip2 command.

$ ls -l
-rw-r--r--. 1 shs shs 100677 May 23 15:40 The_War.bz2

The zcat command works the same way, but with gzip and zip files.

$ zcat The_War.gz | grep vastness
Haggerston and Hoxton, and, indeed, through all the vastness of London
throughout the inanimate vastness of sidereal space. But that is a
$ ls -l The_War.gz
-rw-r--r--. 1 shs shs 132099 May 23 15:40 The_War.gz

To wrap up, the table below provides a list of the commands used to compress, uncompress and display the contents of compressed files without uncompressing them. Note that the zip command requires that you supply the name of the compressed file to be created and does not compress the file “in place” (i.e., leaving you with only that file) as the other two commands do.

To compress To uncompress To display without uncompressing
bzip2 bunzip2 bzcat
gzip gunzip zcat
zip unzip zcat

Note that the compression commands and formats used are different and compression rates will depend on file content as well as the commands used. The zcat and bzcat commands are surprisingly fast. The results of compressing the same file using the three commands are shown below.

$ bzip2 The_War
$ gzip The_War
$ zip The_War
$ ls -l
total 364
-rw-r--r--. 1 shs shs 100677 May 23 16:47 The_War.bz2
-rw-r--r--. 1 shs shs 132099 May 23 15:40 The_War.gz
-rw-r--r--. 1 shs shs 132245 May 23 16:11

The bzcat and zcat commands provide an easy way to verify the contents of compressed text files without requiring that you uncompress and then recompress the files. Keep in mind that these commands only work with compressed text files. You’ll see a lot of output, but likely nothing of value if you try to look at a compressed image or tar file.

Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author